0% found this document useful (0 votes)
45 views

Python With Syntax

The document provides tutorials and examples on various Python programming concepts such as getting user input, data types like lists, tuples, sets and dictionaries, conditional statements, loops, functions, classes and objects, file handling, and error handling. It includes code snippets demonstrating how to work with lists, sort lists, add items to lists, define functions, create classes and objects, open and write to files, and handle different types of errors.

Uploaded by

Yash sample
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python With Syntax

The document provides tutorials and examples on various Python programming concepts such as getting user input, data types like lists, tuples, sets and dictionaries, conditional statements, loops, functions, classes and objects, file handling, and error handling. It includes code snippets demonstrating how to work with lists, sort lists, add items to lists, define functions, create classes and objects, open and write to files, and handle different types of errors.

Uploaded by

Yash sample
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#How to get values from user in python

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)

-----------------------------------------------------------------------------------
-----------------------

#List (Sorted and we can append(add) a list)

myList = ['abcd',1,3.5,'hello']
print(myList)
print(type(myList))

#Tuple (Sorted and we cannot append(add) a tuple)

myTup = ('yash',8,3.9,'hello')
print(myTup)
print(type(myTup))

#Set (Unsorted)

mySet = {'efgh',10,1.5,'okay'}
print(mySet)
print(type(mySet))

#Dictionary (No duplicate values allowed)

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)

#lower case and upper case


myString2 = "HELLO, I AM CAPITAL"
myString3 = "hello, i am small"

modifiedString3 = myString.lower()
print(modifiedString3)

modifiedString4 = myString.upper()
print(modifiedString4)

-----------------------------------------------------------------------------------
-------------------------------

#FORMAT

mangoes = 50
oranges = 30
cherry = 20

str1 = "I want {} mangoes, {} oranges and {} cherry"


print(str1.format(mangoes,oranges,cherry))
str2 = "I want {0} mangoes, {2} oranges and {1} cherry"
print(str2.format(mangoes,cherry,oranges))

myorder = "I have a {carname}, it is a {model}."


print(myorder.format(carname = "Ford", model = "Mustang"))

-----------------------------------------------------------------------------------
-------------------------------

#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)

-----------------------------------------------------------------------------------
-------------------------------

#INSERT A ITEM IN A LIST

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

def greetings(name="yash", age=20): #yash and 20 are the default values.


print("Hello there!! " + name + " and age is " + str(age))

greetings("ani",19)
greetings("vijay",18)
greetings()

-----------------------------------------------------------------------------------
-------------------------------

#CLASSES AND OBJECTS SIMPLE EXAMPLE

class person:
name = "Yash"
age = 19

obj = person()
print(obj.name)
print(obj.age)

-----------------------------------------------------------------------------------
-------------------------------

#CLASSES AND OBJECTS SECOND EXAMPLE

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")

OUTPUT:- Type mismatch

-----------------------------------------------------------------------------------
-------------------------------

#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")

OUTPUT:- 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")

OUTPUT:- No such variable exists!!!


-----------------------------------------------------------------------------------
-------------------------------

You might also like