PYTHON
character_name = "ravi"
character_age = 56
print("the person name is", character_name)
print("The person age is",
character_age)
character_name = "ravi"
character_age = 56
print("the person name is" + character_name)
print("The person age is", character_age)
character_name = "arjun"
character_age = 45
print("the person name is" + character_name)
print("The person age is", character_age)
print("ravi\nkumar")
a = "kjsfdkj"
print(a " ravi \y kumar")
a = "kjsfdkj"
print(a + " ravi \y kumar")
IN python isupper() gives the feedback weather the
result is true or false
For eq: a = "kjsfdkj"
print(a.isupper())
The above program gives the feedback has false.
Since it is just checking weater it is true or false that’s it.
a = "kjsfdkj"
print(a.upper().isupper())
It gives the result has true since first it converts
into the uppercase and later it checks weather it is true or not
To
find the length of string
a = "kjsfdkj"
print(len(a))
To
find the Individual string
a = "kjsfdkj"
print(a[6])
To
print the characters in the string
a = "kjsfdkj"
k = 0
while k < 6:
k = k+1
print(a[k])
Replacing
the string
a = " sdkfkdsf fdksnnf"
print(a.replace("sdkfkdsf" , "jgsdjhj"))
output is jgsdjgj fdksnnf
here we need to give old string and new string
inside the replace
a = 88
print(a + "is my fa")
it gives an error since we cannot number and a
string.
a = 88
print(str(a) + "is my fa")
it gives as 88is my fav
print(pow(2 ,3))
for power
print(round(2.3))
from math import *
print(floor(6.9))
we are importing the advance math concepts like
floor
output is 6
from math import *
print(sqrt(6.9))
FOR
GIVING INPUT VALUES
input("enter your name")
it gives access to enter your name
input is like print in python but also gives access
to enter
input("how are you")
print("where are you")
output is: how
are you jg
where are you
after printing how are you we have given jg
a =input("enter your name")
print("your name is", a)
a = input("Enter a value: ")
b = input("Enter b value: ")
c = a + b
print(c)
Output is Enter
a value: kdh
Enter b value: uei
kdhuei
It adds strings as well
a = input("Enter a value: ")
b = input("Enter b value: ")
c = a + b
print(c)
output is
Enter a value: 5
Enter b value: 6.5
56.5
Python defaultly takes the user input has string
a = input("Enter a value: ")
b = input("Enter b value: ")
c = int(a) + int(b)
print(c)
Enter a value: 6
Enter b value: 7
13
a = input("Enter a value: ")
b = input("Enter b value: ")
print("jgdjf j" + a+b)
Enter a value: sai
Enter b value: varun
jgdjf jsaivarun
LISTS
a = {"fs", "jdfgd", "jfdf"}
b = {3,5,66,77}
print(a)
print(b)
['fs',
'jdfgd', 'jfdf']
[3,
5, 66, 77]
It
prints all values
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,77]
print(a[0:1])
print(b)
['fs']
[3,
5, 66, 77]
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,77]
a.extend(b)
print(a)
print(b)
['fs',
'jdfgd', 'jfdf', 3, 5, 66, 77]
[3,
5, 66, 77]
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,77]
a.insert (1 , "dgdcg")
print(a)
print(b)
['fs',
'dgdcg', 'jdfgd', 'jfdf']
[3,
5, 66, 77]
WE
can aloso use a.clear
(),
A.remove(), a.pop() etc
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,4,77]
b.sort ()
print(a)
print(b)
['fs',
'jdfgd', 'jfdf']
[3,
4, 5, 66, 77]
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,4,77]
print(a.count("fs"))
print(a)
print(b)
1
['fs',
'jdfgd', 'jfdf']
[3,
5, 66, 4, 77]
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,4,77]
print(a.count("fs"))
print(a)
a.reverse()
print(a)
['fs',
'jdfgd', 'jfdf']
['jfdf',
'jdfgd', 'fs']
a = ["fs", "jdfgd", "jfdf"]
b = [3,5,66,4,77]
c = a.copy()
print(c)
['fs',
'jdfgd', 'jfdf']
TUPPLES
cordinates = (3 ,4)
print(cordinates)
(3, 4)
cordinates = (3 ,4)
cordinates[1] = 4
print(cordinates)
It gives an error since tupples are immutable. It
doesnot support for change
FUNCTIONS
def is the key word for the functions.
Eg: def ….
def hi():
jjfkjdf this line inside the
function
fdjfj
jhdgjdj
this is outside the function
we need to call the function for that type function name and ()
hi()
def hi():
print("how are you")
print("iam good")
print("Good morning")
hi()
def hi():
print("how are you")
print("iam good")
print("Good morning")
hi()
Good morning
how are you
iam good
def hi(name):
print("hello " + name)
hi("sai")
hi("varun")
hello sai
hello varun
def hi(name , age):
print("hello " + name, age)
hi("sai", 87)
hi("varun" , 55)
hello sai 87
hello varun 55
def hi(name , age):
print("hello " + name + age)
hi("sai", 66)
hi("varun" , 44)
File
"C:\Users\Lenovo\PycharmProjects\first\app.py", line 2, in hi
print("hello " + name + age)
TypeError: can only concatenate str (not
"int") to str
def hi(name , age):
print("hello " + name, age)
hi("sai", 66)
hi("varun" , 44)
hello sai 66
hello varun 44
def hi(name , age):
print("hello " + name + str(age))
hi("sai", 66)
hi("varun" , 44)
hello sai66
hello varun44
RETURN IN FUNCTIONS
def cube(num):
num*num*num
print(cube(3))
None
Since it is not returning any thing we got the
output has none.
def cube(num):
return num*num*num
print(cube(3))
27
def cube(num):
return num*num*num
print("good morning")
print(cube(3))
27
AT the return statement the function breaks so the
good morning is not printed .
IF , IF-Else , Else-If Statement
If statement syntax is:
if ….:
……
is_male = True
if is_male:
print("boy")
boy
is_male = False
if is_male:
print("boy")
else:
print("girl")
girl
is_male = False
is_female = False
if is_male:
print("boy")
elif is_female:
print("girl")
else:
print("gay")
gay
else if is written as elif in python.
Comprasion
Operators in Python
def comp(num1,num2,num3):
if num1 >= num2 and num1 >= num3 :
print(num1 , "is grater")
if num2 >= num1 and num2 >= num3 :
print(num2 , "is grater")
if num3 >= num1 and num3 >= num2 :
print(num3 , "is grater")
comp(1,2,3)
3 is grater
def comp(num1,num2,num3):
if num1 >= num2 and num1 >= num3 :
print(num1 , "is grater")
if num2 >= num1 and num2 >= num3 :
print(num2 , "is grater")
if num3 >= num1 and num3 >= num2 :
print(num3 , "is grater")
x = input("Enter your num1 value :")
y = input("Enter your num1 value :")
z = input("Enter your num1 value :")
comp(x,y,z)
Enter your num1 value :9
Enter your num1 value :6
Enter your num1 value :7
9 is grater
def comp(num1,num2,num3):
if num1 >= num2 and num1 >= num3 :
return num1
if num2 >= num1 and num2 >= num3 :
return num2
if num3 >= num1 and num3 >= num2 :
return num3
x = input("Enter your num1 value :")
y = input("Enter your num1 value :")
z = input("Enter your num1 value :")
print("The greater value is :", comp(x,y,z))
Enter your num1 value :7
Enter your num1 value :9
Enter your num1 value :2
The greater value is : 9
CALCULATOR
num1 = float(input("Enter first number: "))
num2 = float(input("Enter Second number: "))
operator = input("Enter your operator: ")
if operator == "+":
print(num1+num2)
elif operator == "-":
print(num1-num2)
elif operator == "*":
print(num1*num2)
elif operator == "/":
print(num1/num2)
else:
print("Invalid OPerator")
Enter first number: 6
Enter Second number: 8
Enter your operator: *
48.0
DICTONARIES
month_convertions = {
"jan": "january",
"Feb": "Feburaray",
"Mar": "March",
1:"april",
3:"july"
}
print(month_convertions.get("Mar"))
print(month_convertions.get(1))
print(month_convertions["jan"])
print(month_convertions.get("yuy"))
print(month_convertions.get("yuy", "Invalid"))
March
april
january
None
Invalid
BASIC WHILE LOOP
k=0
while k<4:
k+=1
print(k)
1
2
3
4
GUSSING GAME
value = "ravi"
guess = ""
while guess != value:
guess = input("Enter your Guess: ")
print("Your Guess is correct")
Enter
your Guess: ghf
Enter
your Guess: vmbn
Enter
your Guess: mb
Enter
your Guess: ravi
Your
Guess is correct
value = "ravi"
guess = ""
print("You have 3 chances to WIN")
val = 0
while (guess != value) and (val < 3):
guess = input("Enter your Guess: ")
if guess != value:
print("Your guess is wrong")
val = val + 1
if val == 3:
print("You loos the Game")
else:
print("Your Guess is correct")
You
have 3 chances to WIN
Enter
your Guess: dd
Your
guess is wrong
Enter
your Guess: dd
Your
guess is wrong
Enter
your Guess: ds
Your
guess is wrong
You
loos the Game
FOR
LOOP
for a in "hi":
print(a)
h
i
peoples = ["ravi" , "sai" , "varun"]
for a in peoples:
print(a)
ravi
sai
varun
peoples = ["ravi" , "sai" , "varun"]
for a in peoples:
print(a[0])
r
s
v
for a in range(10):
print(a)
0
1
2
3
4
5
6
7
8
9
for a in range(3,6):
print(a)
3
4
5
peoples = ["ravi" , "sai" , "varun"]
b = len(peoples)
print(b)
for a in range(b):
print(a)
3
0
1
2
peoples = ["ravi" , "sai" , "varun"]
b = len(peoples)
print(b)
for a in range(b):
print(peoples[a])
3
ravi
sai
varun
peoples = ["ravi" , "sai" , "varun"]
for a in range(len(peoples)):
print(peoples[a])
ravi
sai
varun
def power_base(base_num, power_num):
k=1
for a in range(power_num):
k = k * base_num
return k
print(power_base(2, 3))
8
for a in 5:
print(a)
It gives an error
for this we need to use range(5)
2D Lists
a = [[1,2,3],
[3,4,5],
[6,7,8],
[0]]
print(a[0][1])
2
a = [[1,2,3],
[3,4,5],
[6,7,8],
[0]]
for k in a:
print(k)
[1, 2, 3]
[3, 4, 5]
[6, 7, 8]
[0]
a = [[1,2,3],
[3,4,5],
[6,7,8],
[0]]
for k in a:
for b in k:
print(b)
1
2
3
3
4
5
6
7
8
0
TRANSLATION PROGRAM IMMPORTNT
word = input("Enter your word: ")
def translator(word):
p = ""
for k in word:
if k=="a" or k=="e" or k=="i" or k=="o" or k=="u":
p=p+"g"
else:
p= p+ k
return p
print(translator(word))
Enter your word:
sai
Sgg
word = input("Enter your word: ")
def translator(word):
p = ""
for k in word:
if k in "aeiouAEIOU":
p=p+"g"
else:
p= p+ k
return p
print(translator(word))
Enter your word: saiSAIgg
SggSggg
COMMENTS
IN PYTHON
# This is ravi
print("Good Morning: ")
Comments are denoted by # tag in python
number = int(input(" enter a number: "))
print(number)
Converting string to an Integer.
try:
number = int(input(" enter a number: "))
print(number)
except:
print("Invalid Input:")
enter a number: ghghh
Invalid Input:
Generally without tyr, except it throughs an error
to us when we give invalid input so Instead of throughing an error we are
telling to the user that have entered an invalid input by using try except.
a=10/0
print(a)
File
"C:\Users\Lenovo\PycharmProjects\first\app.py", line 1, in
<module>
a=10/0
ZeroDivisionError: division by zero
It throughs an error to us for this we try use try
and except.
try:
print(int(input("Enter a number: ")))
a=10/0
print(a)
except:
print("Invalid Input")
Enter a number: u
Invalid Input
#We are having 10/0 for this the error is
zeroDivision error but for both we are getting Invalid Input.
try:
a = 10 / 0
except ZeroDivisionError as err: # it prints to the user what the error is
print(err)
try:
print(int(input("Enter a number: ")))
print(a)
except ValueError as val:
print(val)
division by zero
Enter a number: t
invalid literal for int() with base 10: 't'
#Here ZerodivisionEroor and ValueError are bultit
ins in python in place or err and val we can write anything.
READ FILES IN
PYTHON
a = open("emp.txt","w")
it is to create new file, if the file is not there
a=open("emp.txt","w")
a.write("Iam a good boy")
We are opening new file and writing Iam a good boy
a=open("emp.txt","r")
#a.write("Iam a good boy")
print(a.read())
Iam a good boy
We are reading here
a=open("emp.txt","a")
a.write("\n Iam a good girl")
print(a.read())
It gives an error since we cannot readwhen it is
append mode
a=open("emp.txt","a")
a.write("\n Iam a good girl")
b=open("emp.txt","r")
print(b.read())
Iam a good boy
Iam a good
girl
Iam a good
girl
We are appending here and iam a good girl is printed
twice since we run the prgm twice so be careful how many time you run the prgm
then data will be appended
a=open("emp.txt","w")
a.write("\n Iam a good husbend")
a.close()
b=open("emp.txt","r")
print(b.read())
b.close()
Iam a good husband
When we try to write it Overwrites the existed data
and always close the file
a=open("emp.html","w")
a.write("</p>iam ravi</p>")
a.close()
b=open("emp.html","r")
print(b.read())
b.close()
</p>iam ravi</p>
To create an html file
# To Open a file give like this,
# This is for the file located in the same directoary in which we are wring our program
# for this Iam Imagnig file name emp.txt
open("emp.txt","r") # here we are opening the file read formate,
#we can keep w in place of r which indicates write formate
a = open("emp.txt","r") # we are storing opened fiel inside variable a
print(a.readable()) # We are checking weather
# it readble or not it returns boolian value "True" as we opened file in read formate
print(a.read()) # It prints all the lines in the file
print(a.readline())# It prints the first line,
#If we keep print(a.readline()) for 2 times it reads first 2 lines
print(a.readlines())# It prints all the lines in list formate
print(a.readlines()[3])# to print the first line
for b in a.readlines():
print(b) # to print all the line for loop
a.close() # This is we are closing the file It is good practice to close a file when we are opened it
WRITING
# This is for the file located in the same directoary in which we are wring our program
# for this Iam Imagnig file name emp.txt
a = open("emp.txt","w")
a.write("\n ravi") # here we are writing in the existed file as ravi
#all the previous lines will be over writed and only ravi will be printed now.
a.close()
Comments
Post a Comment