Python With Syntax
Python With Syntax
Python With Syntax
x=int(input("Enter the value of x = ")) #we are type casting here as it take
string input before making it integer.
y=int(input("Enter the value of y = "))
z = x+y
print(z)
-----------------------------------------------------------------------------------
-----------------------
myList = ['abcd',1,3.5,'hello']
print(myList)
print(type(myList))
myTup = ('yash',8,3.9,'hello')
print(myTup)
print(type(myTup))
#Set (Unsorted)
mySet = {'efgh',10,1.5,'okay'}
print(mySet)
print(type(mySet))
myDict = {
"key":"value",
"car":"Audi",
"fruit":"mango",
"number":20
}
print(myDict)
print(type(myDict))
-----------------------------------------------------------------------------------
-----------------------
-----------------------------------------------------------------------------------
--------------------------
x=10
y=20
if x>y:
print("x is greater than y")
else:
print("y is greater than x")
-----------------------------------------------------------------------------------
-------------------------------
marks = 83
if marks>=90:
grade = 'A+'
elif marks>=80 and marks<90:
grade = 'A'
elif marks>=70 and marks<80:
grade = 'B+'
elif marks>=60 and marks<70:
grade = 'B'
elif marks>=50 and marks<60:
grade = 'C'
elif marks>=20 and marks<30:
grade = 'Fail'
print(grade)
-----------------------------------------------------------------------------------
-------------------------------
#For loop
https://www.w3schools.com/python/python_for_loops.asp
#While loop
https://www.w3schools.com/python/python_while_loops.asp
-----------------------------------------------------------------------------------
-------------------------------
#INBUILT FUNCTIONS
#replace
myString = " Hello, my name is Yash "
modifiedString = myString.replace('a','b')
print(modifiedString)
#Strip - it removes all the spaces from front and back of the string
modifiedString2 = myString.strip()
print(modifiedString2)
modifiedString3 = myString.lower()
print(modifiedString3)
modifiedString4 = myString.upper()
print(modifiedString4)
-----------------------------------------------------------------------------------
-------------------------------
#FORMAT
mangoes = 50
oranges = 30
cherry = 20
-----------------------------------------------------------------------------------
-------------------------------
#SORT A LIST
#accending
myList = ["apple","cherry","zebra","lion"]
myList.sort()
print(myList)
myList2 = [1,4,2,6,9,7]
myList2.sort()
print(myList2)
#Decending
myList.sort(reverse=True) #in true, T should be in capital
print(myList)
myList2.sort(reverse=True)
print(myList2)
-----------------------------------------------------------------------------------
-------------------------------
#APPEND
myList = [1,100,200,23,10,9]
myList2 = [99,11,33]
for x in myList2:
myList.append(x)
print(myList)
-----------------------------------------------------------------------------------
-------------------------------
myList = ["car","bikes"]
myList.insert(1,"spaceship") #here 1 is index value at where we have to insert
print(myList)
-----------------------------------------------------------------------------------
-------------------------------
#COPY A LIST
myList = ["car","bikes"]
copyList = myList.copy()
print(copyList)
-----------------------------------------------------------------------------------
-------------------------------
#FUNCTIONS
greetings("ani",19)
greetings("vijay",18)
greetings()
-----------------------------------------------------------------------------------
-------------------------------
class person:
name = "Yash"
age = 19
obj = person()
print(obj.name)
print(obj.age)
-----------------------------------------------------------------------------------
-------------------------------
class vehicle:
def __init__ (self,name,tyre): #here init means initial
self.name = name
self.tyre = tyre
def funct2(self):
print("This is my "+self.name)
obj1 = vehicle("CAR",4)
obj2 = vehicle("SCOOTER",2)
print(obj1.name)
print(obj2.tyre)
obj1.funct2()
#We should always delete the objects at the end of the program
del obj1
del obj2
-----------------------------------------------------------------------------------
-------------------------------
#FILE HANDLING
"""
#Commands or instructions
r means read
w means write
x means create
a means append
#Modes
b means binary
t means text
"""
-----------------------------------------------------------------------------------
-------------------------------
#ERROR HANDLING
try:
x=12
y="hello"
print(x+y)
except TypeError:
print("Type mismatch")
except NameError:
print("No such variable exists!!!")
except: #Default error
print("An unknown error occured")
-----------------------------------------------------------------------------------
-------------------------------
#ERROR HANDLING
try:
x=12/0
print(x)
except TypeError:
print("Type mismatch")
except NameError:
print("No such variable exists!!!")
except: #Default error
print("An unknown error occured")
-----------------------------------------------------------------------------------
-------------------------------
#ERROR HANDLING
try:
x=12
print(z)
except TypeError:
print("Type mismatch")
except NameError:
print("No such variable exists!!!")
except: #Default error
print("An unknown error occured")