You are on page 1of 10

NAME :- Giridhari Kumar jha

Roll No:-1916849
PYTHON PRACTICAL FILE
B.TECH CSE 5TH SEMESTER

# program to add, subtract, multiply and divide


a = 10
b=5
print("a + b : ",a + b)
print("a - b : ",a - b)
print("a * b : ",a * b)
print("a / b : ",a / b)

Output:

# program to perform floor division between two numbers


a = 10
b=5
print("a // b : ", a//b)

Output:

# program to find remainder of division


a = 10
b=3
print("a % b : ",a % b)

Output:

# program to print table of 5 without loop


print(5 * 1)
print(5 * 2)
print(5 * 3)
print(5 * 4)
print(5 * 5)
print(5 * 6)
print(5 * 7)
print(5 * 8)
print(5 * 9)
print(5 * 10)

Output:

# program to find type of variable


a = 10
b = 5.0
c = "Sherlock"
print(type(a))
print(type(b))
print(type(c))

Output:

# program to use sep and end in print function


print('15','12','2021', sep='-')
print('15','12','2021', end=' ')

Output:

# program to add, subtract, multiply and divide user defined numbers


n1 = int(input('Enter number 1 : '))
n2 = int(input('Enter number 2 : '))
print("n1 + n2: ", n1 + n2)
print("n1 - n2: ", n1 - n2)
print("n1 * n2: ", n1 * n2)
print("n1 / n2: ", n1 / n2)

Output:

# program to get name and age from user and print it


name = input('Enter your name : ')
age = int(input('Enter your age : '))
print(name, age, end=' ')

Output:

# program find area of circle and print it


radius = int(input('Enter the radius : '))
area = 3.14 * radius * radius
print(area)

# program find area of square and print it


side_length = int(input('Enter the length of side of square : '))
area = side_length * side_length
print(area)

Output:

# program to find area of rectangle


length = int(input("Enter length of rectangle : "))
breadth = int(input("Enter breadth of rectangle : "))
area = length * breadth
print(area)
Output:

# program to implement all comparison operators


print(3 > 5)
print(3 < 5)
print(3 <= 5)
print(3 >= 5)
print(3 == 5)
print(3 != 5)

Output:
False
True
True
False
False
True

# program to implement all assignment operators


a = 'Sherlock Holmes'
print(a)

Output:

# program to implement logical operators


print(4 == 3 and 6 > 9)
print(4 == 3 or 6 > 9)
x=3
print(not x == 3)

Output:

# program to implement bitwise operators


a = 10
b=4
print("a & b =", a & b)
print("a | b =", a | b)
print("~a =", ~a)
print("a ^ b =", a ^ b)

Output:

# program to implement unary operators


x=100
y=-(x)
print(y)
x = 10
y = +x
print(y)

Output:
-100
10

# program to print address of a variable


a = 12
print(id(a))

Output:

# program to typecast
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)

Output:

# program to use simple if else


num = 5
if num == 5:
print('The number is 5.')
else:
print('The number is not 5.')

Output:

# program to compare two numbers


num1 = 5
num2 = 10
if num1 > num2:
print('1st number is greater')
else:
print('2nd number is greater.')

Output:

# program to find even odd


num1 = int(input('Enter a number : '))
if num1 % 2 == 0:
print('Even')
else:
print('Odd')
Output:

# program to find max of three numbers


def is_greatest(n1, n2, n3):
print(max(n1,n2,n3))

n1 = int(input('Enter number 1: '))


n2 = int(input('Enter number 2: '))
n3 = int(input('Enter number 3: '))
is_greatest(n1,n2,n3)

Output:

# Program to make a menu driven program to find areas of different


shapes
print('Area of different shapes:')
print('1:Square')
print('2:Rectangle')
print('3:Circle')
print('4:Cylinder')

print('Enter your choice : ')


n = int(input())
if n == 1:
print('Enter side of square:')
side = int(input())
print(side * side)

elif n == 2:
length = int(input('Enter length of rectangle : '))
breadth = int(input('Enter breadth of rectangle : '))
print(length * breadth)

elif n == 3:
radius = int(input('Enter radius of circle : '))
print(3.14 * radius * radius)

elif n == 4:
radius = int(input('Enter radius of cylinder : '))
height = int(input('Enter height of cylinder : '))
area = (2 * 3.14 * radius * height) + (2 * 3.14 * radius * radius)
print(area)

else:
print('Invalid Input!')

Output:
# program for menu driven arithmetic operation
a=int(input('Enter number 1: '))
b=int(input('Enter number 2: '))
print('Enter + for Addition, - for Subtraction, * for Multiplication and /
for Division.')
ch=input('Enter your choice: ')
if ch=='+':
print("Sum = ",a + b)

if ch=='-':
print("Difference = ",a - b)

if ch=='*':
print("Product = ",a * b)

if ch=='/':
print("Division = ",a / b)

Output:

# program for is operator


a=10
b=5
c=10

print(id(a))
print(id(b))
print(id(c))

print(a is c)
print(a is b)

Output:
1633999585808
1633999585648
1633999585808
True
False

# program to print first five numbers without range function using for
loop
for i in [1,2,3,4,5]:
print(i)

Output:
1
2
3
4
5

# program to print first five numbers with range function using for loop
for i in range(1,6):
print(i)

Output:
1
2
3
4
5

# sum of 10 natural numbers


sum = 0
for i in range(1,11):
sum = sum + i
print(sum)

Output:
55

# sum of n natural numbers


sum = 0
n = int(input('Enter value of n: '))
for i in range(1,n + 1):
sum = sum + i
print(sum)

Output:
Enter value of n: 15
120

# printing n natural numbers


n = int(input('Enter value of n: '))
for i in range(1,n + 1):
print(i)

Output:
Enter value of n: 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# program to reverse a given number


n = int(input('Enter a number : '))
print('Reversed number is ')
print(str(n)[::-1])
Output:
Enter a number : 12345
Reversed number is
54321

# program to find factorial of a given number


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

for a in range(1,a+1):
b=b*a

print('Factorial of number is: ',b)

Output:
Enter number: 24
Factorial of number is: 620448401733239439360000

# program to check palindrome


a = int(input('Enter number to check: '))
d=a
b=0
c=0

while a != 0:
c = a % 10
b = b * 10 + c
a //= 10

if d==b:
print('\nIt is a Palindrome.')
else:
print('\nIt is not a Palindrome.')

Output:
Enter number to check: 123321
It is a Palindrome.

# program to use break


for a in range (1,10):
print(a)
if (a==4):
break

Output:

# program to find fibonacci


n = int(input("Enter the value of n: "))
a=0
b=1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
print(sum, end = " ")
count = count + 1
a=b
b = sum
sum = a + b

Output:

Enter the value of n: 10

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

# Program to count digits in a number


print("Program to count digits in a number.")
a = int(input('Enter number: '))
count=0

while a != 0:
a //= 10
count=count+1

print('Digits in number are: ',count)


Output:

Program to count digits in a number.

Enter number: 1234567890

Digits in number are: 10

You might also like