0% found this document useful (0 votes)
13 views58 pages

Python Programming Practical Guide

Uploaded by

pogako1890
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
0% found this document useful (0 votes)
13 views58 pages

Python Programming Practical Guide

Uploaded by

pogako1890
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

Practical file of python

Submitted To: Submitted By:


[Link] Kaur Manpreet Kaur
(Department of computer science) Class: PGDCA
Roll no:25484
INDEX
SR NO. PROGRAM PAGE NO.
1. Program to print hello world 3
2. Program to print input function 4
3. Program using variables 5
4. Arithmetic operators 6
5. Comparison operators 7
6. Assignment operators 8
7. Logical operators 9
8. Bitwise operators 10
9. Decision making statements 11-14
10. Data types 15-36
11. Data Type Conversion 37-40
12. Loops 41-42
13. Control Statements 43
14. Functions 44-45
15. Function Returning Values 46
16. Recursive Function 47
17. Function Overloading 48

18. Methods of parameter passing to a function 49-50

19. Scope of Variables 51-53


20. Exception Handling 54-57

2
1. Program to print hello world :

INPUT:
print(“hello world”)

OUTPUT:

3
2. Program to print input function :

Input :

a=int(input("Enter first number:"))


b=int(input("Enter second number:"))
c=a+b
print(c)

Output:

4
3. Program to using variables :

Input:

a=20
b=20.5
c="ANU"
print(a)
print(b)
print(c)

Output :

5
4. Arithmetic Operators:

Input:

a,b=20,30
print(a+b)
print(a-b)
print(a/b)
print(a*b)
print(a%b)

Output:

6
5. Comparison operators/Relational operators

Input:

a,b=20,5
print(a==b)
print(a!=b)
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)

Output:

7
6. Assignment operators :

Input :
a,b=20,5
a+=b
print(a)
a-=b
print(a)
b+=a
print(b)
a/=b
print(a)
b*=a
print(b)
a%=b
print(a)
Output:

8
7. Logical operators :

Input :

a,b,c=35,30,25
print(a>b and b>c)
print(a>b or c>b)
print(not(a>b))

Output:

9
8. Bitwise operators :

Input :

a,b=10,2
print(a&b)
print(a|b)
print(a^b)
print(~a)
print(a>>2)
print(a<<2)

Output:

10
9. Decision making statements:

a)​ If statements :

Input:

num=int(input("Enter a number:"))
if num==0:
print("zero")

Output:

11
b)​ If - else statement :

Input:

num=int(input("Enter a number:"))
if num>=0:
print("positive number or zero")
else:
print("negative number")

Output:

12
c)​ If - elif - else statement :

Input :

num=int(input("Enter a number:"))
if num>0:
print("positive number ")
elif num==0:
print("zero")
else:
print("negative number")

Output :

13
d)​ Nested if - else statement :

Input :

num=int(input("Enter a number"))
if num>=0:
if num==0:
print("zero")
else:
print("positive number")
else:
print("negative number")

Output:

14
10. Data type :

a)​ Numbers

Input:

a,b,c,d=5,5.6,2345678,4+3j
print("int=",a)
print("float=",b)
print("long=",c)
print("complex=",d)

Output:

●​

15
b)​ String :

Input :
a="HELLO"
print(a)
print(a[3])
print(a[0:4])

Output :

16
# Built in string functions or method :
a)​ len(string)
Input:
a="hello"
print(a)
print(len(a))
Output :

b)​ lower ()
Input :
a="Hello World"
print([Link]())
Output :

17
c)​ upper()

Input:
a="hello world"
print([Link]())

Output :

d)​ swapcase()

Input:

a="Hello World"
print([Link]())

Output :

18
e)capitalize()

INPUT :

a="Hello World"
print([Link]())

OUTPUT :

f) title()

INPUT :

a="hello world"
print([Link]())

OUTPUT :

19
g) isalpha ()

INPUT :

a="hello world"
print([Link]())

OUTPUT :

h) isdigit()

INPUT :

a="12345"
print([Link]())

OUTPUT :

i) isalnum()

20
INPUT :

a="hello"
print([Link]())

OUTPUT :

j) islower()

INPUT :

a="hello world"
print([Link]())

OUTPUT :

k) isupper()

21
INPUT :

a="HELLO WORLD"
print([Link]())

OUTPUT :

l) istitle()

INPUT :

a="Hello world"
print([Link]())

OUTPUT :

m) find (str, start, end )

22
INPUT :

a="hello world"
print([Link]("l",0,7))

OUTPUT :

n) count(str , start , end )

INPUT :

a="hello world"
print([Link]("o",0,8))

OUTPUT :

o) replace(old,new)

23
INPUT :

a="hello world"
print([Link]("world","python"))

OUTPUT :

C) List
24
1)​ INPUT :

list1=[1,2,3,4,5,6]
list2=['Anu','manna']
print(list1)
print(list2)
print(list1[2:4])

OUTPUT :

# Concatenation(+) Operator :

25
INPUT :

a=[1,4.4,4+3j,'Tom']
b=[1,2,3,4,5]
print(a+b)

OUTPUT :

26
# Repetition(*) Operator :

INPUT :

a=[1,2,3,4,]
print(a*2)

OUTPUT :

# Built in the functions or methods :


27
1)​ Max (list)

INPUT :
a=[80,0,35,99,67]
print(max(a))

OUTPUT :

2)​ Min (list)

INPUT :
a=[80,70,58,23,]
print(min(a))

OUTPUT :

3)​ pop()

INPUT :

28
a=[8,3.3,4+7j,'manna']
[Link]()
print(a)

OUTPUT :

4)​ Reverse()

INPUT :
a=[1,2,3,4,5]
[Link]()
print(a)

OUTPUT :

5)​ Updating ()

29
INPUT :
list=['tom','rose','jasmine',200,300]
print(list)
list[2]='lotus'
print(list)
OUTPUT :

6)​ deleting()

INPUT :
list=['tom','rose','jasmine',200,300]
print(list)
del list[2]
print(list)

OUTPUT :

d) Tuple

INPUT:

30
tuple=['tom','rose','jasmine',200,300]
print(tuple)
print(tuple[0:3])
print(tuple[1: ])

OUTPUT :

1)​ len(tuple)

INPUT :
tuple=['tom','rose','jasmine',200,300,400]
print(len(tuple))

OUTPUT :

1)​ max(tuple) and min(tuple)

INPUT :

31
tuple1=[100,200,600,999,455]
print(max(tuple1))
print(min(tuple1))

OUTPUT :

d) Dictionary

INPUT :
dict={'Name':'sukh','Age':24,'Height':5.7}
print(dict)
print(dict['Age'])
OUTPUT :

1)​ updating()

32
INPUT :
Dict={'Name':'sukh','Age':24,'Height':5.7}
dict['Age']=40
print(dict)

OUTPUT :

2)​ inserting()

INPUT :
dict={'Name':'sukh','Age':24,'Height':5.7}
dict['city']='bassi pathana'
print(dict)

OUTPUT :

d) Deleting()

33
INPUT :

dict={'Name':'sukh','Age':24,'Height':5.7}
del dict['Height']
print(dict)

OUTPUT :

# Built in functions in dictionary

34
a)​ [Link]()

INPUT :

dict={'Name':'sukh','Age':24,'Height':5.7}
print([Link]())

OUTPUT :

b)​ [Link]()

INPUT :
dict={'Name':'Sukh','Age':24,'Height':5.7}
print([Link]())

OUTPUT :

c)​ [Link]()

35
INPUT :

dict={'Name':'Sukh','Age':24,'Height':5.7}
a=[Link]()

OUTPUT :

d)​ [Link]()

INPUT :

dict={'Name':'Sukh','Age':24,'Height':5.7}
a=[Link]()
print(dict)

OUTPUT :

11) DATA TYPE CONVERSION / TYPE CASTING :

36
a)​ Implicit type casting :

INPUT :

a=15
b=4.4
result=a+b
print(result)
print(type(result))

OUTPUT :

b)​ Explicit type casting :

37
1.​Conversion to integer data type

INPUT :

print("conversion to integer data type")


a=int(1)
b=int(2.2)
c=int("3")
print(a)
print(b)
print(c)

OUTPUT :

2.​ Conversion to floating point type

INPUT :
38
print("Conversion to floating point type")
a=float(1)
b=float(2.2)
c=float("3.3")
print(a)
print(b)
print(c)

OUTPUT :

3.​ Conversion to string

39
INPUT :

print("Conversion to string")
a=str(1)
b=str(2.2)
c=str("3.3")
print(a)
print(b)
print(c)

OUTPUT :

12) Loops :

a) for loop

40
INPUT :
numbers=[2,4,6,8,10]
for i in numbers:
print(i)
OUTPUT :

# Range() function

a) INPUT :
numbers=[2,4,6,8,10]
for i in range(5):
print(numbers[i])
OUTPUT:

b) INPUT:

for i in range(2,10,2):
print(i)

41
OUTPUT:

b) while loop

INPUT:

i=1
while i<=5:
print(i)
i=i+1

OUTPUT :

13. Control Statements :

a) break Statement

42
INPUT :
for i in range(2,10,2):
if i==6:
break
print(i)

OUTPUT:

a) continue Statement

INPUT:
for i in range(2,10,2):
if i==6:
continue
print(i)

OUTPUT:

14. Functions:

Function Calling(using no parameter)

43
1) INPUT:
def my_function():
print("Hello")
my_function()

OUTPUT:

using one parameter

2) INPUT:
def my_function(fname):
print(fname+"Kaur")
my_function("Amanjot")
my_function("Navleen")
my_function("Sukhman")

OUTPUT:

using two parameter

3) INPUT:
def my_function(fname,lname):

44
print(fname+lname)
my_function("Aman","Kaur")
my_function("Navleen","Kaur")
my_function("Sukhman","Kaur")

OUTPUT:

# Default Parameter

INPUT :
def my_function(Country="India"):
print("My Country is",Country)
my_function("England")
my_function()
my_function("Italy")
my_function("US")

OUTPUT:

15. Function Returning Values :

INPUT :

45
def my_function(x):
return 5*x
print(my_function(2))
print(my_function(3))
print(my_function(4))

OUTPUT :

16. Recursive Function :

INPUT :

46
def factorial(x):
if x==1:
return 1
else:
return(x*factorial(x-1))
print("The factorial of 5 is",factorial(5))

OUTPUT :

17. Function Overloading :

INPUT :

47
def my_func(x,y):
a=x+y
print(a)
def my_func(x,y,z):
a=x+y+z
print(a)
my_func(2,3,4)

OUTPUT :

18. Methods of parameter passing to a function:

a.) Pass by Value

INPUT :
48
def my_func(num):
print("Original number:",num)
num+=10
print("Inside function:",num)
x=5
my_func(x)
print("Outside function:",x)

OUTPUT :

b.) Pass by Reference

INPUT:

def my_func(my_list):

49
print("Original list:",my_list)
my_list.append(5)
print("Inside function:",my_list)
x=[1,2,3,4]
my_func(x)
print("Outside function:",x)

OUTPUT :

19. Scope of Variables:

a.) Local Scope

50
1. INPUT:
def my_func():
x=200
print(x)
my_func()

OUTPUT :

2. INPUT:
def my_func():
x=200
def my_innerfunc():
print(x)
my_innerfunc()
my_func()

OUTPUT :

b.) Global Scope

INPUT:
x=500
def my_func():
print(x)

51
my_func()
print(x)

OUTPUT :

# Naming Variable

INPUT :
x=200
def my_func():
x=300
print(x)
my_func()
print(x)

OUTPUT :

# Global Keyword

1. INPUT:
def my_func():
global x
x=300

52
my_func()
print(x)

OUTPUT:

2. INPUT:
x=200
def my_func():
global x
x=300
my_func()
print(x)

OUTPUT :

20. Exception Handling :

a.) try,except and else Block

INPUT :
try:

53
a=int(input("Enter a number:"))
b=int(input("Enter second number:"))
c=a/b
print(c)
except ZeroDivisionError:
print("Division By Zero")
else:
print("Successful Division")

OUTPUT :

b.) except clause with no exception

INPUT :
try:
a=int(input("Enter a number:"))
b=int(input("Enter second number:"))
c=a/b

54
print(c)
except:
print("Error occured")
else:
print("Successful Division")

OUTPUT :

c.) except clause with multiple exceptions

INPUT :

try:
a=int(input("Enter a number:"))
b=int(input("Enter second number:"))

55
c=a/b
print(c)
except(ZeroDivisionError,TypeError):
print("Error occured")
else:
print("Successful Division")

OUTPUT :

d.) final clause

INPUT :

try:
a=int(input("Enter a number:"))

56
b=int(input("Enter second number:"))
c=a/b
print(c)
except:
print("Error occured")
else:
print("Successful Division")
finally:
print("Executed Always")

OUTPUT :

57
58

You might also like