100% found this document useful (1 vote)
2K views29 pages

Python GTU Study Material E-Notes Unit-1 12012021081509AM

The document provides information on Python programming concepts like data types, operators, control flow statements, functions, strings and formatting. It includes examples of built-in data types like integers, floats, complex numbers and booleans. Examples are given for operators, if-else conditional statements, while and for loops. Functions are defined with and without arguments and return values. String methods, formatting and slicing are demonstrated.

Uploaded by

TRISHALA.SWAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views29 pages

Python GTU Study Material E-Notes Unit-1 12012021081509AM

The document provides information on Python programming concepts like data types, operators, control flow statements, functions, strings and formatting. It includes examples of built-in data types like integers, floats, complex numbers and booleans. Examples are given for operators, if-else conditional statements, while and for loops. Functions are defined with and without arguments and return values. String methods, formatting and slicing are demonstrated.

Uploaded by

TRISHALA.SWAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29





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

The type of a <class 'int'>


The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True


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)

x = "darshan Institute, rajkot"


c = x.title()
l = x.lower()
u = x.upper()
print(c)
print(l)
print(u)

Darshan Institute, Rajkot


darshan institute, rajkot
DARSHAN INSTITUTE, RAJKOT

x = 'darshan institute, rajkot'


c = x.istitle()
l = x.islower()
u = x.isupper()
print(c)
print(l)
print(u)

False
True
False

o

x = 'darshan institute, rajkot, india'


f = x.find('in')
print(f)

8 (index of ‘in’ in x)

x = 'darshan institute, rajkot, india'


r = x.rfind('in')
print(r)

27 (last index of ‘in’ in x)

o
x = 'darshan institute, rajkot, india'
r = x.replace('india','INDIA')
print(r)

darshan institute, rajkot, INDIA


o

x = 'darshan institute, rajkot, india'


f = x.index('in')
print(f)

8 (index of ‘in’ in x)

x = 'darshan institute, rajkot, india'


r = x.rindex('in')
print(r)

27 (last index of ‘in’ in x)

o

o

x = 'darshan123'
f = x.isalnum()
print(f)

True

o
o


o

o


x = '{0} institute, rajkot, INDIA'


r = x.format('darshan')
print(r)

darshan institute, rajkot, INDIA


x = '{0} institute, {1}, {2}'
r = x.format('darshan', 'rajkot', 'INDIA')
print(r)

darshan institute, rajkot, INDIA



x = '{0} institute, {1}, {2}, welcome to {0}'
r = x.format('darshan', 'rajkot', 'INDIA')
print(r)

darshan institute, rajkot, INDIA, welcome to darshan

x = '{{{0}}} institute'
r = x.format('darshan')
print(r)

x = 'The price for {0} is ₹{1}'


r = x.format('iphone',50000)
print(r)

The price for iphone is ₹50000


s = '{0} institute of engineering'
print(s.format('darshan')) #default formatting

darshan institute of engineering


s = '{0:25} institute of engineering'
print(s.format('darshan')) #minimum width

darshan institute of engineering

s = '{0:>25} institute of engineering'


print(s.format('darshan')) #right align, minimum width

darshan institute of engineering

s = '{0:^25} institute of engineering'


print(s.format('darshan')) #center align, minimum width

darshan institute of engineering

s = '{0:-^25} institute of engineering'


print(s.format('darshan')) #fill center align, minimum width

---------darshan--------- institute of engineering

s = '{0:.3} institute of engineering' #maximum width of 3


print(s.format('darshan'))

dar institute of engineering


s = 'amount = {0}' # default formatting
print(s.format(123456))

123456

s1 = 'amount = {0:0=10}' # 0 fill, minimum width 12 (technique 1)


print(s1.format(123456))

s2 = 'amount = {0:010}' # 0 pad, minimum width 12 (technique 2)


print(s2.format(123456))
amount = 0000123456
amount = 0000123456

s = 'amount = {0: }' # space or - sign


print(s.format(123456))
print(s.format(-123456))

‘ 123456’
‘-123456’

s = 'amount = {0:+}' # force sign


print(s.format(123456))
print(s.format(-123456))

+123456
-123456

s1 = '{0:b} {0:o} {0:x} {0:X}'


s1 = '{0:#b} {0:#o} {0:#x} {0:#X}'
print(s1.format(12))
print(s2.format(12))

1100 14 c C
0b1100 0o14 0xc 0XC


s = 'amount = {0}' # default formatting
print(s.format(12.3456789))

12.3456789

s1 = 'amount = {0:10.2e}' # exponential formatting with two precession


s2 = 'amount = {0:10.2f}' # floating formatting with two precession
print(s1.format(12345.6789))
print(s2.format(12345.6789))

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

for temp in range(5) :


if temp == 2 :
continue
print(temp)

0
1
3
4

for temp in range(5) :


pass




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

Darshan is located in Gujarat



def collegeDetails(collegeName, state='Gujarat') :
print(collegeName + " is located in "+ state)

collegeDetails(collegeName='Darshan')

Darshan is located in Gujarat


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'])

collegeDetail(city='rajkot', state='gujarat', name='Darshan')

College Name is Darshan



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

institute (List index starts with 0)


3 (length of the List)
['darshan', 'institute', 'rajkot'] (Note : spelling of rajkot is updated)
rajkot (-1 represent last element)



my_list = ['darshan', 'institute', 'rajkot','gujarat','INDIA']
print(my_list[1:3])

['institute', 'rajkot'] (Note: end index is not included)

my_list = ['darshan', 'institute', 'rajkot']


my_list.append('gujarat')
print(my_list)

['darshan', 'institute', 'rajkot', 'gujarat']

my_list = ['darshan', 'institute', 'rajkot']


my_list.insert(2,'of')
my_list.insert(3,'engineering')
print(my_list)

['darshan', 'institute', 'of', 'engineering', 'rajkot']

my_list1 = ['darshan', 'institute']


my_list2 = ['rajkot','gujarat']
my_list1.extend(my_list2)
print(my_list1)

['darshan', 'institute', ‘rajkot', ‘gujarat']

o
my_list = ['darshan', 'institute','rajkot']
temp = my_list.pop()
print(temp)
print(my_list)

rajkot
['darshan', 'institute']

my_list = ['darshan', 'institute', 'darshan','rajkot']


my_list.remove('darshan')
print(my_list)

['institute', 'darshan', 'rajkot']

my_list = ['darshan', 'institute', 'darshan','rajkot']


my_list.clear()
print(my_list)

[] (Empty List)

my_list = ['darshan', 'institute', 'darshan','rajkot']


id = my_list.index('institute')
print(id)

o
my_list = ['darshan', 'institute', 'darshan','rajkot']
c = my_list.count('darshan')
print(c)

my_list = ['darshan', 'institute','rajkot']


my_list.reverse()
print(my_list)

['rajkot', ‘institute','darshan']

my_list = ['darshan', 'college','of','enginnering','rajkot']


my_list.sort()
print(my_list)
my_list.sort(reverse=True)
print(my_list)

['college', 'darshan', 'enginnering', 'of', 'rajkot']


['rajkot', 'of', 'enginnering', 'darshan', 'college']



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

('darshan', 'institute', 'of', 'engineering', 'of')


3
2
of


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

['college', 'city', 'type']

my_dict = {'college':"darshan",
'city':"rajkot",
'type':"engineering"}
print(my_dict.values())

['darshan', 'rajkot', 'engineering']

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)

['d', 'a', 'r', 's', 'h', 'a', 'n']



[ expression for item in iterable ]
OR
[ expression for item in iterable if condition ]


mylist = [c for c in 'darshan']
print(mylist)

['d', 'a', 'r', 's', 'h', 'a', 'n']


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

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


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]

You might also like