Professional Documents
Culture Documents
Code: if n2>n1:
# a) 'if' statement if n2>n3:
cp = float(input("Enter Cost print(n2,"is greatest")
Price:"))
sp = float(input("Enter Selling
if n3>n1:
Price:"))
if n3>n2:
print(n3,"is greatest")
if sp>cp:
output:
prft=sp-cp
Enter Cost Price:200
print("The Shopkeeper has
made profit of Rs.",prft) Enter Selling Price:100
if cp>sp: The Shopkeeper has made loss of
Rs. 100.0
los=cp-sp
print("The Shopkeeper has
made loss of Rs.",los) Enter a number:11
11 is odd
# b) 'if...else' statement
num = int(input("Enter a Enter 3 numbers
number:"))
11
if num % 2 == 0:
12
print(num,"is even")
13
else:
13 is great
print(num,"is odd")
if n1>n2:
if n1>n3:
print(n1,"is greatest")
Practical 5
Code:
# a) 'while' loop Output:
num = int(input("Enter a Enter a number:50
number:"))
Sum of digits is 5
sum = 0
while num > 0:
Enter the number:11
r = num % 10
11 x 1 = 11
sum = sum + r
11 x 2 = 22
num =int(num/10)
11 x 3 = 33
11 x 4 = 44
print("Sum of digits is",sum)
11 x 5 = 55
11 x 6 = 66
# b) 'for' loop
11 x 7 = 77
num = int(input("Enter the
11 x 8 = 88
number:"))
11 x 9 = 99
11 x 10 = 110
for i in range(1,11):
m = num*i
Enter the number of rows:5
print(num,"x",i,"=",m)
*
* *
# c) Nested loops
* * *
num_rows = int(input("Enter the
number of rows:")) * * * *
for i in range(0, num_rows): * * * * *
for j in range(0,
num_rows-i-1):
print(end=" ")
print()
Practical 6
Code:
list1 = [] print("\nList-2 elements after
#created an empty list append operation are");
list2 = [10,20,30,40] for i in list2:
#created list with integers
print(1)
list3 = [45,2.65,"Rohit"]
#created list with mixed data-
types print("\nList-3 elements after
append operation are");
for i in list3:
#directly printing lists
print(i)
print("List-1 initially
contains:",list1)
print("List-2 initially #now removing elements from
contains:",list2) lists
print("List-3 initially list.remove(970) #removing
contains:",list3) specified element "970'
list1.pop(1) #removing
element from index '1'
#adding elements in list
list2.pop(2)
list1.append(855)
list3.pop(3)
list1.append(970)
list1.append(275)
#now showing list elements after
list1.append(382)
remove/pop operations
print("\nList-1 elements after
list2.append(50) remove/pop operation are");
list2.append(60) for i in list1:
print(i)
list3.append(412)
list3.append("Rohit") print("\nList-2 elements after
remove/pop operation are");
for i in list2:
#printing list elements using
loop print(i)
print("\nList-1 elements after
append operation are");
print("\nList-3 elements after
for i in list1: remove/pop operation are");
print(i) for i in list3:
Practical 6
print(i) 45
2.65
#deleting all three lists Rohit
del list1 412
del list2 Rohit
del list3
List-1 elements after remove/pop
operation are
print("\nAll lists are deleted")
855
382
Output:
List-1 initially contains: []
List-2 elements after remove/pop
List-2 initially contains: [10,
operation are
20, 30, 40]
10
List-3 initially contains: [45,
2.65, 'Rohit'] 20
40
List-1 elements after append 50
operation are
60
855
970
List-3 elements after remove/pop
275 operation are
382 45
2.65
List-2 elements after append Rohit
operation are
Rohit
1
1
All lists are deleted
1
1
1
1
Code:
tuple1 = () print("\nAll tuples are
deleted")
tuple2 = (10,20,30,40)
tuple3 = (67.45,57,"Rohit")
Output:
()
print(tuple1)
(10, 20, 30, 40)
print(tuple2)
(67.45, 57, 'Rohit')
print(tuple3)
(10, 20, 30, 40, 67.45, 57,
'Rohit')
tuple4 = tuple2 + tuple3
(10, 20, 30, 40, 67.45, 57,
print(tuple4) 'Rohit')
print(tuple5) 10
20
print("\nset-2 contains")
set-2 contains
for i in set2:
B
print(i)
A
C
#adding single elements in set
by using add() method D
set1.add(45)
set2.add("C") set-1 contains: {35, 10, 45, 15,
20, 25, 30}
set-2 contians: {'B', 'A', 'C',
#printing set elements after
'D'}
adding single elements
Both sets deleted
Practical 9
print("\nDictionary-3 contains")
#displaying dictionaries
for i in dictionary3:
print("Dictionary-1 initially
contains:",dictionary1) print("At index",i,"value
is",dictionary3[i])
print("Dictionary-2 initially
contains:",dictionary2)
print("Dictionary-3 initially #removing from an element from
contains:",dictionary3) dictionary-3
dictionary3.popitem()
#updating dictionary-1 from print("\nAfter removing one
user's values element,Dictionary-3 contains")
print("\nUpdating Dictionary-1") for i in dictionary3:
print("Enter person name as print("At index",i,"value
dictionary index") is",dictionary3[i])
ind = input()
print("Enter person weight as del dictionary1
index value")
del dictionary2
val = float(input())
del dictionary3
dictionary1.update({ind:val})
Updating Dictionary-1
Enter person name as dictionary
index
Rohan
Enter person weight as index
value
105
Dictionary-1 contains
At index Rohan value is 105.0
Dictionary-2 contains
At index Rohit value is
11.11.2003
At index Piyush value is
30.05.2004
At index Bhushan value is
11.02.2002
Dictionary-3 contains
At index Mr.Anushu value is 101
At index Mr.Akasha value is 102
At index Mr.Aayush value is 103
Output:
Original String is : apache
tomcat server
Resulting String is : Apache
Tomcat Server
print("\nOriginal String is
:",string4)