Python GTU Study Material E-Notes Unit-1 12012021081509AM
Python GTU Study Material E-Notes Unit-1 12012021081509AM
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
print("Hello World from python")
o
o
o
o
o
o
o
o
o
o
1. a = 5
2. print("The type of a", type(a))
3.
4. b = 40.5
5. print("The type of b", type(b))
6.
7. c = 1+3j
8. print("The type of c", type(c))
9. print(" c is a complex number", isinstance(1+3j,complex))
o
o
o
o
1. # Python program to check the boolean type
2. print(type(True))
3. print(type(False))
4. print(false)
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
o
o
o
o
o
o
રાજકોટ”
x = " D a r s h a n "
index = 0 1 2 3 4 5 6
Reverse index = 0 -6 -5 -4 -3 -2 -1
x = 'our string'
subx1 = x[startindex]
subx2 = x[startindex:endindex]
subx3 = x[startindex:endindex:steps]
x = 'darshan institute of engineering and technology, rajkot,
gujarat, INDIA'
subx1 = x[0:7]
subx2 = x[49:55]
subx3 = x[66:]
subx4 = x[::2]
subx5 = x[::-1]
print(subx1)
print(subx2)
print(subx3)
print(subx4)
print(subx5)
darshan
rajkot
INDIA
drhnisiueo niern n ehooy akt uaa,IDA
AIDNI ,tarajug ,tokjar ,ygolonhcet dna gnireenigne fo etutitsni nahsrad
x = "Darshan"
print(len(x))
x = "Darshan"
ca = x.count('a')
print(ca)
False
True
False
o
8 (index of ‘in’ in x)
o
x = 'darshan institute, rajkot, india'
r = x.replace('india','INDIA')
print(r)
o
8 (index of ‘in’ in x)
o
o
x = 'darshan123'
f = x.isalnum()
print(f)
True
o
o
o
o
x = '{0} institute, {1}, {2}'
r = x.format('darshan', 'rajkot', 'INDIA')
print(r)
x = '{0} institute, {1}, {2}, welcome to {0}'
r = x.format('darshan', 'rajkot', 'INDIA')
print(r)
x = '{{{0}}} institute'
r = x.format('darshan')
print(r)
s = '{0} institute of engineering'
print(s.format('darshan')) #default formatting
s = 'amount = {0}' # default formatting
print(s.format(123456))
123456
‘ 123456’
‘-123456’
+123456
-123456
1100 14 c C
0b1100 0o14 0xc 0XC
s = 'amount = {0}' # default formatting
print(s.format(12.3456789))
12.3456789
amount = 1.23e+04
amount = 12345.68
o
o
o
o
o
o
o
if some_condition :
# Code to execute when condition is true
x = 10
if x > 5 :
print("X is greater than 5")
X is greater than 5
x = 3
if x > 5 :
print("X is greater than 5")
else :
print("X is less than 5")
X is less than 5
x = 10
if x > 12 :
print("X is greater than 12")
elif x > 5 :
print("X is greater than 5")
else :
print("X is less than 5")
X is greater than 5
o
o
o
o
o
while some_condition :
# Code to execute in loop
x = 0
while x < 3 :
print(x)
x += 1 # x++ is invalid in python
0
1
2
x = 5
while x < 3 :
print(x)
x += 1 # x++ is invalid in python
else :
print("X is greater than 3")
X is greater than 3
for temp_item in iterable_object :
# Code to execute for each object in iterable
my_list = [1, 2, 3, 4]
for list_item in my_list :
print(list_item)
1
2
3
4
my_list = range(0,5)
for list_item in my_list :
print(list_item)
0
1
2
3
4
my_list = ["darshan","institute","rajkot","gujarat"]
for list_item in my_list :
print(list_item)
darshan
institute
rajkot
gujarat
for temp in range(5) :
if temp == 2 :
break
print(temp)
0
1
0
1
3
4
def function_name(arguments) :
''' docstring ''' #optional
#code to execute when function is called
def seperator() :
print('==============================')
print("hello world")
seperator()
print("from darshan college")
seperator()
print("rajkot")
hello world
==============================
from darshan college
==============================
rajkot
def seperator(type) :
if(type=='=') :
print('==============================')
elif(type=='*') :
print('******************************')
else :
print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&')
print("hello world")
seperator('=')
print("from darshan college")
seperator('*')
print("rajkot")
hello world
==============================
from darshan college
******************************
rajkot
def setName(name) :
name[0] = "arjun bala"
name = ["Default"]
setName(name)
print(name)
['arjun bala']
def collegeDetails(collegeName, state) :
print(collegeName + " is located in "+ state)
collegeDetails(state='Gujarat', collegeName='Darshan')
def collegeDetails(collegeName, state='Gujarat') :
print(collegeName + " is located in "+ state)
collegeDetails(collegeName='Darshan')
def collegeList(*colleges):
print(colleges[0])
# we can also loop through all the arguments
# for c in colleges :
# print(c)
collegeList('darshan','nirma','vvp')
darshan
def collegeDetail(**college) :
print('College Name is '+college['name'])
def addition(n1,n2):
return n1 + n2
ans = addition(10,5)
print(ans)
15
o
o
o
o
my_list = ['darshan', 'institute', 'rkot']
print(my_list[1])
print(len(my_list))
my_list[2] = "rajkot"
print(my_list)
print(my_list[-1])
my_list = ['darshan', 'institute', 'rajkot','gujarat','INDIA']
print(my_list[1:3])
o
my_list = ['darshan', 'institute','rajkot']
temp = my_list.pop()
print(temp)
print(my_list)
rajkot
['darshan', 'institute']
[] (Empty List)
o
my_list = ['darshan', 'institute', 'darshan','rajkot']
c = my_list.count('darshan')
print(c)
['rajkot', ‘institute','darshan']
o
o
my_set = {1,1,1,2,2,5,3,9}
print(my_set)
{1, 2, 3, 5, 9}
o
o
my_tuple = ('darshan','institute','of','engineering','of')
print(my_tuple)
print(my_tuple.index('engineering'))
print(my_tuple.count('of'))
print(my_tuple[-1])
my_dict = { 'key1':'value1', 'key2':'value2' }
my_dict = {'college':"darshan", 'city':"rajkot",'type':"enginee
ring"}
print(my_dict['college'])
print(my_dict.get('city'))
darshan
rajkot
my_dict = {'college':"darshan",
'city':"rajkot",
'type':"engineering"}
print(my_dict.keys())
my_dict = {'college':"darshan",
'city':"rajkot",
'type':"engineering"}
print(my_dict.values())
my_dict = {'college':"darshan",
'city':"rajkot",
'type':"engineering"}
print(my_dict.items())
[('college', 'darshan'), ('city', 'rajkot'), ('type', 'engineering')]
mystr = 'darshan'
mylist = []
for c in mystr:
mylist.append(c)
print(mylist)
[ expression for item in iterable ]
OR
[ expression for item in iterable if condition ]
mylist = [c for c in 'darshan']
print(mylist)
# list of square from 1 to 10
mylist = []
for i in range(1,11):
mylist.append(i**2)
print(mylist)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# list of square from 1 to 10
mylist = [ i**2 for i in range(1,11) ]
print(mylist)
leapYears = [i for i in range(1980,2021) if i%4==0]
print(leapYears)
[1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020]