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

Darpan Python

The document contains a series of Python programming exercises and their corresponding code solutions, demonstrating various programming concepts such as variable declaration, arithmetic operations, string manipulation, list operations, and user input handling. Each exercise includes the code, expected output, and explanations of the operations performed. The exercises are structured for educational purposes, likely aimed at students learning Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Darpan Python

The document contains a series of Python programming exercises and their corresponding code solutions, demonstrating various programming concepts such as variable declaration, arithmetic operations, string manipulation, list operations, and user input handling. Each exercise includes the code, expected output, and explanations of the operations performed. The exercises are structured for educational purposes, likely aimed at students learning Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan

(AUTONOMOUS)

PRACTICAL NO :- 01

Q.1 write a python code to display a welcome to the world of


python programming.

Code : -

print("welcome to the world of python programming")

Output:-

welcome to the world of python programming.

Q.2 write a python program to create variable and display its


value

Code : -

a=63 print("variable a value is


=",a)

Output : -

Page | 9
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q.3 write a python program to display friends name by using


format specifiers

Code : -

a="darpan" b="leo" print("the


friends name are %s and
%s"%(a,b)

Output :-

Q.1 Write a python code to display 5 variables and type


integer using format specifiers

Code :-

a=10

Page | 10
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
b=20

c=30

d=40

e=50

print("the variable value are %d,%d,%d,%d,%d"%(a,b,c,d,e,))

Output : -

the variable value are 10,20,30,40,50

(PQ). Write a python code to display student information.\

CODE :-

print(""" my name is DARPAN KENI


I am in SYCS
I study in khalsa college""")

Page | 11
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

(PQ). Write a python code to create different variables and display its
values.

CODE:-

name="par
th"
>>>age=18
>>>height=5.9
>>>print("variable values are",name,age,height)

OUTPUT : -

****************************

PRACTICALNUMBER : - 02
Page | 12
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q.1 Arithmetic Operators

CODE : -

a = int(input('Enter val 1: ')) b


= int(input('Enter val 2: '))

# addition print('Sum:
', a + b)

# subtraction print('Subtraction:
', a - b)

# multiplication print('Multiplication:
', a * b)

# division print('Division:
', a / b)

# floor division print('Floor


Division: ', a // b)

# modulo print('Modulo:
', a % b)

# power print('Power:
', a ** b)

OUTPUT : -

Page | 13
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q.2 RELATIONSHIP OPERATOR

CODE : -

a = int(input('Enter val1: ')) b


= int(input('Enter val2: '))

# equal to operator print('a


== b =', a == b)

# not equal to operator print('a


!= b =', a != b)

# greater than operator print('a


> b =', a > b)

# less than operator print('a


< b =', a < b)

# greater than or equal to operator print('a


>= b =', a >= b)

# less than or equal to operator print('a


<= b =', a <= b)

OUTPUT : -

Page | 14
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q.3 ASSIGNMENT OPERATOR

CODE : -

#
a
ss
ig
n
1
0
to
a
a
=
1
0
#
a
ss
ig
n
5 to b b = 5
# assign the sum
of a and b to a a
+= b # a = a + b
print("a+=b

Page | 15
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
is",a)
# assign the subtraction of a and b to a
a -= b # a = a - b print("a-=b is",a)

OUTPUT : -

Q.4 LOGICAL OPERATOR

CODE : -

a=5b=6
print((a >
2) and (b
>= 6))
print((a >
2) or (b >=
6))
print(not(a > 2))

OUTPUT : -

Q.5 ] IDENTITY OPERATOR


Page | 16
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

CODE : -

x1 = 5 y1 = 5 x2 =
'Hello' y2 = 'Hello' x3
= [1,2,3] y3 = [1,2,3]
print(x1 is not y1) #
prints False print(x2
is y2) # prints True
print(x3 is y3) #
prints False

OUTPUT : -

Q.6 MEMBERSHIP OPERATOR

CODE : -

message =
'Hello world'
dict1 = {1:'a',
2:'b'}
# check if 'H' is present in
message string print('H' in
message) # prints True #
check if 'hello' is present in
message string print('hello'
not in message) # prints
True # check if '1' key is
present in dict1 print(1 in
dict1) # prints True # check if

Page | 17
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
'a' key is present in dict1
print('a' in dict1) # prints
False

OUTPUT :-

*************************

PRACTICAL NUMBER : - 03

Q.1 Write a Python program to create a string "My College


Name Is Guru Nanak Khalsa "

CODE : -

str="My College Name Is Khalsa College"


Page | 18
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print("length of str is=",len(str))
print("position from 7 upto 14
is=",str[7:15]) print("string is
lowercase is =",str.lower())
print("string is uppercase is
=",str.upper()) print("split string
are =",str.split()) print("position of
C is=",str.index('C'))
print("position of K
is=",str.index('K')) print("M
Occurs in the string
is=",str.count('M'))

OUTPUT : -

Q.2 Write a python code to create a string which create list of


5 friend Name and perform a following operation

CODE : -

Page | 19
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
str="LEO,CRIS,NEYMAR,SUAREZ,INEASTA"
print("Third friend is",str[10:15])
print("This all are my friend",str)
print("The first letter of 4th friend name
is",str[16:17]) print("The capital A
occurs",str.count("A"))

OUTPUT :-

Q.3 Write a python code to split and join a string .

CODE :-

a = "This is a sample sentence"


words = a.split() print(words)
b = "_".join(words) print(b)

OUTPUT : -

Page | 20
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q.4 write a python code to create a string and perform


operation

CODE : -

my_string = "hello there, how are you feeling sad"

begins_with_hello = my_string.startswith("hello")
print(f"String begins with 'hello': (begins_with_hello)")

ends_with_sad = my_string.endswith("sad")
print(f"String ends with 'sad' : (ends_with_sad)")

string_length = len(my_string) print(f"Length


of the string: (string_length)")

half_index = string_length
// 2 first_half =
my_string[:half_index]
second_half =
my_string[half_index:]

first_word_of_second_half = second_half.split()[0] print (f"First


word of the second half : (first_word_of_second_half)") OUTPUT :-

Page | 21
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Page | 22
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Practical 4

Q1. Create a list with the name List I which contains 4 integer values and list
2 which contains 4 characters values and perform the following operation.
a. Display all the list item from the list 1
b. Display all the list items from the list 2
c. Display third element of the list 1
d. Display last element of the list 2
e. Display sequence of list Items from the list 1 from 1 to 3
CODE :
List1 = [1,2,3,4]
List2 = ["A","B","C","D"]
print(List1)
print(List2)
print("3rd elt :",List1[2])
print("3rd elt :",List2[3])
print("Printing 1 to 3 from List 1",List1[0:3])
OUTPUT :

Q2. Write a python code to create a list which contains the book details. (
Book name, author, price, publication, edition, etc) and
perform the following operation •
a. Display all the book details.
b. Display price of the book
c. Update the price of the book by ₹ 200.
d. Update the edition of the book to the latest edition.
CODE :
Book = ["Python","Leo messi",1500,"Leo messi productions",2022]
print(Book)
print(Book[2])
Book[2] = Book[2] + 200
Page | 23
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print(Book)
Book[4] = 2024
OUTPUT :

Q3. Create a list with five elements and remove element at the index I and
return its value.
CODE :
A = [10,20,30,40,50]
print("Original List",A)
val = A.pop(1)
print("Now my List is ",A)
print("Remove list item ",val)
OUTPUT :

Q4. Create a list with five elements and remove the first occurrence of value
20 .
CODE :

A = [10,20,30,40,50]
print("Original List",A)
val = A.remove(20)
print("Now my List is ",A)
OUTPUT :

Q5. Create a Python program which reads 2 values From the user and display
their multiplication.
CODE :

Page | 24
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
a = int(input("Enter val of a "))
b = int(input("Enter val of b "))
print(" a = ", a ," b = ", b )
print(" a*b = ", a*b )
OUTPUT :

Q6. Write a program to read student data such as Student Name, Student
Age, marks for 3 subjects and to display Average marks.
CODE :
StdName = input("Enter name : ")
StdAge = input("Enter age : ")
S1 = float(input("Enter marks for sub1 : "))
S2 = float(input("Enter marks for sub2 : "))
S3 = float(input("Enter marks for sub3 : "))
avg = (S1+S2+S3)/3
print(StdName)
print(StdAge)
print(avg)
OUTPUT :

Q7. Write a program to read 3 values from the user for creating list and
display their values and their addition.
CODE :

a=[0,0,0]
a[0] = int (input ("Enter value for 1st element of list"))
a[1]= int (input ("Enter value for 2nd element of list"))
a[2] = int (input ("Enter value for 3rd element of list"))
Page | 25
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print ( a[0], a[1], a[2])
add=( a[0] + a[1] + a[2])
print (add)
OUTPUT :

Q8. Write a program to create a list and add n number of item to it.
CODE :

a = []

# Get the number of elements


n = int(input("Enter the number of elements: "))

# Append elements to the list


for i in range(n):
element = input(f"Enter element {i+1}: ")
a.append(element)

print("List:", a)
OUTPUT :

Q9. List.append(obj)
Appends object to list
CODE :

aList=['123', 'xyz' ,'zara', 'abc'];


aList.append('2009');
print("Updated List : ", aList)

Page | 26
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

OUTPUT :

Q10. list.count(obj)
Returns count of how many times obj occurs in list
CODE :

aList=[123, 'xyz', 'zara', 'abc', 123]


print ("Count for 123 : ",aList.count(123))
print ("Count for zara : ",aList.count('zara'))
OUTPUT :

Q11. List.extend(seq)
Appends the contents of seq to list
CODE :

aList = [123, 'xyz', 'zara', 'abc', 123]


bList = [2009, 'manni']
aList.extend (bList)
print ("Extended List : ", aList)
OUTPUT :

Q12. List.index(obj)
Returns the lowest index in list that obj appears
CODE :

aList=[123, 'xyz', 'zara', 'abc'];


print ("Index for xyz: ",aList.index( 'xyz' ))
print("Index for zara ",aList.index( 'zara'))
OUTPUT :

Page | 27
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

Q13. List.insert(index,obj)
Insert object obj into list at offset index.
CODE :

aList = [123, 'xyz', 'zara', 'abc']


aList.insert(3, 2009)
print("Final List : ", aList)
OUTPUT :

Q14. List.pop(obj=list[-1])
Removes and returns last object or obj from list
CODE :

aList = [123, 'xyz', 'zara', 'abc']


print("Popped Element : ", aList.pop())
print("Updated List:")
print(aList)
OUTPUT :

Q15. List.remove(obj)
Removes object obj from list
CODE :

aList = [123, 'xyz', 'zara', 'abc','xyz']


aList.remove('xyz')
print("List : ", aList)
aList.remove('abc')
Page | 28
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print("List : ", aList)
OUTPUT :

Q16. List.revese()
CODE :

str = "malayalam"
listl = list(str)
list2 = listl.copy()
res = listl.reverse()
if listl == list2:
print ("The string is a palindrome")
else:
print ("The string is not a palindrome")

OUTPUT :

Q17 list.sort([func])
Sorts objects of list, use compare func if given
CODE :

aList=['123', 'xyz', 'zara', 'abc', 'xyz']


aList.sort()
print ("List : ",aList)
OUTPUT :

Page | 29
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

PRACTICAL 5

Q1: Write a program to create a tuple with the name T1 with five
values and tuple T2 with 5 values and perform following operations.
CODE:
T1 = (13, 2, 4, 5) T2 = (6, 8, 9, 10)
print("The values of T1 and T2 is:",
T1 + T2) print("The 4th element
is:", T1[3]) print("The 2nd element
is:", T2[1])
print("The addition of 4th and 2nd elements are.",
T1[3] + T2[1]) NewTup = T1 + T2 print(NewTup)

OUTPUT:

Q2: Create a Python program which creates a Tuple with 5 color


names and check whether a particular color belongs to a Tuple or not.
CODE:
colors = ("Red", "Blue", "Orange",
"Pink", "Black") print("The original
values are", colors) print('red' in colors)
print('Black' in colors)

OUTPUT:

Q3 Create a Tuple with the Students data such as studentName


and Marks in 4 subjects. a. Display the result of the students for
all the subjects in percentage form.
CODE:
students = ("Darpan", 90, 10, 80, 70)

Page | 30
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Avg = (students[1] + students[2] + students[3] +
students[4]) / 4 percent = (Avg / 100) * 100) print("The
average percentage of Darpan is:", percent)
OUTPUT:

Q4:Write a program to access tuple elements using negative indexing


CODE:
my_tuple = ("I", "Love", "To", "Code", "In", "Python")

print("Tuple:", my_tuple)

print("Element at index -1:", my_tuple[-1])

print("Element at index -3:", my_tuple[-3])

OUTPUT:

Q5:Python program to show how negative indexing works in Python


tuples.
CODE:
sample_tuple = ("Apple", "Mango", "Banana", "Orange", "Guava", "Berries")

print("Element at -1 index:", sample_tuple[-1])

print("Element at -2 index:", sample_tuple[-2])

print("Element at -3 index:", sample_tuple[-3])

print("Element at -4 index:", sample_tuple[-4])

print("Element at -5 index:", sample_tuple[-5])

print("Element at -6 index:", sample_tuple[-6])

print("Elements between -6 and -1 are:", sample_tuple[-6:-1])

OUTPUT:

Page | 31
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

TYPE BUILT-IN METHODS:-

1. index():-

CODE:
Thistu(1,3,5,7,8,9,6,7,0,8)
x = thistu.index(8)
print(x)

OUTPUT:

2. count():-

CODE:
thistu = (1, 3, 5, 7, 8, 9, 6,
7, 0, 8) x =
thistuple.count(8) print(x)

OUTPUT:

3. all():-
Code:
print(all([True, True, False]))

OUTPUT:

Page | 32
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

4. any():- Code:
L = [False, False, True, False, False]
print(any(L))

OUTPUT:

5. clear():-
Code:
Car = {
"brand": "ford",
"Model": "mustang",
"Year": 1964
}

Car.clear()
print(Car)

OUTPUT:

6. copy():- Code:
Car = {
"brand": "ford",
"Model": "mustang",
"Year": 1964
}

x=
Car.copy()
print(x)

OUTPUT:

Page | 33
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
7.
fromkeys():-
Code:
x = ('key1', 'key2',
'key3')
y=0
thisadict =
dict.fromkeys(x, y)
print(thisadict)

OUTPUT:

8.get(
):-
Code
:
Car = {
"brand": "ford",

"Model":"mustang",
"Year": 1964
}

x=
Car.get("Model")
print(x)

OUTPUT:

Page | 34
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)

PRACTICAL 6

Q1:Write a python program to create a dictionary with the


students' details and display it.
a. Display all the values from the dictionary.
b. Display values of the last element.
c. Update the value of the 3rd element by adding 100 to it.
d. Check whether the second element is greater than fifty(50).
e. Perform delete operation on the fourth element of the dictionary.
f. Display modulo of last element with the value 10.

CODE:
Number = {"Val1":19, "Val2":23, 'Val3':50, "Val4":4,
"Val5":91} print("All the values of dictionary:", Number)
print("The value of last element is:", Number['Val5'])
Number['Val3'] = Number['Val3'] + 100 print("Is the second
element greater than 50?", Number['Val2'] > 50) del
Number["Val4"] print("The updated 3rd element is:",
Number['Val3']) print("The dictionary after deleting the
fourth element:", Number) modulo = Number['Val5'] % 10
print("The modulo of the last element with 10 is:", modulo)
OUTPUT

Q2:Write a python program to display the course details such as


CourseName, No. of student registered for the course, course days,
course fees, etc. Perform the following operations on the dictionary.
a. Display all dictionary values.
b. Display the details of Course days.
c. Display the details of Course fees.
d. Display the course fees with the updated value by 1000.
e. Display how many students have registered for the course.
f. Delete one of the days for the dictionary course days.
CODE:

Page | 35
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CourseDetails = {
'CName': "Python",
'StudReg': 230,
'CDays': ['Fr', 'Sat', 'Sun'],
'CFees': 2500} print("The Course Details are:", CourseDetails) print('The
details of course days are:', CourseDetails['CDays']) print('The details of
Course fees are:', CourseDetails['CFees']) CourseDetails['CFees'] =
CourseDetails['CFees'] + 1000 print('The updated course fees are:',
CourseDetails['CFees']) print('The number of students registered for the
course are:', CourseDetails['StudReg']) del CourseDetails['CDays'][0]
print('The updated course days are:', CourseDetails['CDays'])

OUTPUT:

Q3:Write a Python program to create a dictionary with the six subject marks
for the stude Calculate the percentage of students and display details with
percentage.

CODE:
Sub = {'S1': 30, 'S2': 40, 'S3': 50, 'S4': 60, 'S5': 70, 'S6': 80}

Percent = (Sub['S1'] + Sub['S2'] + Sub['S3'] + Sub['S4'] + Sub['S5'] + Sub['S6']) / 600 *


100

print("The percentage of all 6 subjects is:", Percent)

OUTPUT:

Page | 36
PYTHON PROGRAMMING-I

You might also like