You are on page 1of 13

Practical 3

Code: y=input("Enter the value of y:


")
# a)
usd=float(input("Enter the
currency in USD:")) temp = x
inr=usd*80 x = y
print("The currency in INR y = temp
is",round(inr,2))

print('The value of x after


# b) swapp: {}'.format(x))
import math print('The value of y after
swapp: {}'.format(y))
num = float(input("Enter numeric
value : "))
Output:
squareRoot = math.sqrt(num) Enter the currency in USD:11
print("The Square root of {0} The currency in INR is 880.0
is: {1} ".format(num,
squareRoot))
Enter numeric value : 10
The Square root of 10.0 is:
# c)
3.1622776601683795
width=float(input('Enter the
width:'))
Enter the width:20
height=float(input('Enter the
height:')) Enter the height:6
Area of a Rectangle is:120.00
Area=width*height

Enter the value of x: 8


print("\nArea of a Rectangle Enter the value of y: 9
is:%.2f"%Area)
The value of x after swapp: 9
The value of y after swapp: 8
# d)
x=9
y=11

x=input("Enter the value of x:


")
Practical 4

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

# c) Nested 'if' statement


print("Enter 3 numbers")
n1 = int(input())
n2 = int(input())
n3 = int(input())

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

for j in range(0, 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

List-3 elements after append


operation are
Practical 7

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

tuple5 = tuple2 + tuple3 Elements of tuple-4 are

print(tuple5) 10
20

#can also print tuples using 30


for-loop
40
print("\nElements of tuple-4
67.45
are")
57
for i in tuple4:
Rohit
print(i)

Elements of tuple-5 are


print("\nElements of tuple-5
are") 10
for i in tuple5: 20
print(i) 30
40
#deleting all tuples 67.45
del tuple1 57
del tuple2 Rohit
del tuple3
del tuple4 All tuples are deleted
del tuple5
Practical 8

Code: print("\nset-1 contains:",set1)


#initalizing and creating set print("set-2 contians:",set2)
set1 = {10,20,30,20}
#deleting both sets
#another way to declare set del set1
set2 = set() del set2

#directly printing set elements print("Both sets deleted")


print("\nset-1 contains:",set1)
print("\nset-2 contains:",set2) Output:
set-1 contains: {10, 20, 30}
#adding multiple elements in set
using update() method
set-2 contains: set()
set1.update([15,25,30,35])
set2.update("A","B","C","D")
set-1 contains
35
#printing updated set elments by
10
using loop
15
print("\nset-1 contains")
20
for i in set1:
25
print(i)
30

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

Code: for i in dictionary1:


#creating dictionaries print("At index",i,"value
is",dictionary1[i])
dictionary1 = {}
dictionary2 =
{"Rohit":"11.11.2003","Piyush":" print("\nDictionary-2 contains")
30.05.2004"}
for i in dictionary2:
dictionary3 =
print("At index",i,"value
{"Mr.Anushu":101,"Mr.Akasha":102
is",dictionary2[i])
,"Mr.Aayush":103}

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

print("All Dictionaries are


#updating dictionary-2 by direct deleted")
entry
dictionary2.update({"Bhushan":"1
1.02.2002"})

#displaying dictionary using


loop iterations
print("\nDictionary-1 contains")
Practical 9

Output: At index Mr.Anushu value is 101


Dictionary-1 initially contains: At index Mr.Akasha value is 102
{}
All Dictionaries are deleted
Dictionary-2 initially contains:
{'Rohit': '11.11.2003',
'Piyush': '30.05.2004'}
Dictionary-3 initially contains:
{'Mr.Anushu': 101, 'Mr.Akasha':
102, 'Mr.Aayush': 103}

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

After removing one


element,Dictionary-3 contains
Practical 10

Code: print("Constant value of'e'


is:",math.e)
# Program 1:
print("Constant value of'pi'
import math
is:",math.pi)

print("Enter any value")


Output:
val = int(input())
Enter any value
11
print("Factorial
Factorial is: 39916800
is:",math.factorial(val))
Square root is: 3.3166247903554
print("Square root
is:",math.sqrt(val)) Exponentiation is:
59874.14171519782
print("Exponentiation
is:",math.exp(val)) Exponentiation-1 is:
59873.14171519782
print("Exponentiation-1
is:",math.expm(val)) Sum is: 62.5
Enter value of Base:25
#Program 2: Enter value of Power:5
import math Result is: 9765625
#demonstrating fsum() to obtain Constant value of'e' is:
sum 2.718281828459045
ls = [10,20,30,2.5] Constant value of'pi' is:
3.141592653589793
a = math.fsum(ls)
print("Sum is:",a)

#demonstrating pow() to obtain


power
b = int(input("Enter value of
Base:"))
p = int(input("Enter value of
Power:"))
res = pow(b,p)
print("Result is:",res)

#showing math constant's values


Practical 10

Code: print("\nOriginal String is


:",string3)
#Program 1:
print("Resulting String is
:",result3)
# remember that, the string
objects are immutable in Python
and Java. # demonstrating startswith() and
ends with() for boolean check
#means it will not change
start and end
original value of string object,
but it will return string4= "India Is Great"
#new resulting value instead. print("\nOriginal String is
:",string4)
# demonstrating title() method,
that returns string in title print("Is string4
case startswith('Ind')? :
",string4.startswith("Ind"))
string1 = "apache tomcat server"
print("Is string4
result1= string1.title()
startswith('ind')? :
print("Original String is ",string4.startswith("ind"))
:",string1) # case sensitive

print("Resulting String is print("Is string4


:",result1) startswith('Indi')?:
",string4.startswith("Indi"))
print("Is string4
# demonstrating upper() method, startswith('India Is')? :
that returns string in upper ",string4.startswith("India
case Is"))
string2= "python PROgramming 123 print("Is string4 ends
@# 456" with('eat')? :
result2= string2.upper() ",string4.endswith("eat"))

print("\nOriginal String is print("Is string4 ends with


:",string2) (EAT)?",string4.endswith("EAT"))
# case sensitive
print("Resulting String is
:",result2) print("Is string4 ends with
('Great')?:
",string4.endswith("Great"))
#demonstrating lower() method, print("Is string4 ends with('Is
that returns string in lower Great')? :",string4.endswith("Is
case Great"))
string3= "PYTHON PROgramming 123
@# 456"
result3= string3.lower()
Practical 10

Output:
Original String is : apache
tomcat server
Resulting String is : Apache
Tomcat Server

Original String is : python


PROgramming 123 @# 456
Resulting String is : PYTHON
PROGRAMMING 123 @# 456

Original String is : PYTHON


PROgramming 123 @# 456
Resulting String is : python
programming 123 @# 456

Original String is : India Is


Great
Is string4 startswith('Ind')? :
True
Is string4 startswith('ind')? :
False
Is string4 startswith('Indi')?:
True
Is string4 startswith('India
Is')? : True
Is string4 ends with('eat')? :
True
Is string4 ends with (EAT)?
False
Is string4 ends with ('Great')?:
True
Is string4 ends with('Is
Great')? : True
Practical 10

Code: print("Is string4 in uppercase?


:",string4.isupper())
#Program 2:
print("Is string4 is
#demonstrating capitalize() that
lowercase?:",string4.islower())
capitalizes only first letter
(unlike title() method) #similarly we have many other
boolean functions to check such
string1 = "apache tomcat server"
case
result1= string1.capitalize()
print("\nOriginal String is
Output:
:",string1)
print("Resulting String is
:",result1) Original String is : apache
tomcat server
Resulting String is : Apache
#demonstrating split() to split
tomcat server
the string by specified
character or word nOriginal String is : It was-a-
really lucky day-where-i met you
string2= "It was-a-really lucky
day-where-i met you" Because of Split by we get
resulting list as : ['It was',
lst = string2.split("-")
'a', 'really lucky day',
print("nOriginal String is 'where', 'i met you']
:",string2)
print("Because of Split by we
Original String is : a
get resulting list as : ",lst)
Is string3 in uppercase?: False
Is string3 is lowercase? : True
#demonstring isupper() and
islower() methods to check the
case
Original String is : A
string3="a"
Is string4 in uppercase? : True
string4="A"
Is string4 is lowercase?: False
print("\nOriginal String is
:",string3)
print("Is string3 in
uppercase?:",string3.isupper())
print("Is string3 is lowercase?
:",string3.islower())

print("\nOriginal String is
:",string4)

You might also like