EPR Python Lab programs:
1. Python program to add two numbers.
2. Python program to add two numbers provided by the user
3. Python program to add two numbers using function
4. Python program to multiply three number
5. Python program to multiply three numbers using function
6. Python program to find average of five numbers
7. Python program to find square root of the number
8. python program to find area of circle
9. python program to find the area of the right-angle triangle
10. Python program to calculate simple interest using function
11. Python Program to get the first digit of number
12. Python Program to reverse number
13. Python Program to swap two numbers without using temporary variable
14. Python program to make a simple calculator
15. Python program to print fibonacci series up to nth term
16. Python program to print multiplication table
17. Python program to find factors of a number
18. Python program to find the factorial of a number
19. Python program to convert decimal to binary
20. Python program to convert decimal to hexadecimal
21. Python program to print numbers from 1 to 10 using while loop
22. Python program to print numbers from 1 to 10 using for loop.
23. Python program to create list, edit the list, copy the list, and access items from the
list.
24. Python program to create nested list, edit the list, copy the list, and access items
from the list.
25. Python program to create tuple and access items from the tuple. Also write code for
tuple unpacking.
26. Python program to split an email address into user name and domain using Tuple.
27. Python program to create list slicing and tuple slicing.
28. Python program to create set, add and remove items from the set.
29. Python program to explain various set operations.
30. Python program to create dictionary using various methods.
31. Python program to add and update dictionary and merge two dictionaries.
32. Python program to create nested dictionary.
33. Python program for linear searching.
34. Python program for binary searching.
35. Python program for list comprehension and dictionary comprehension.
Python programs:
1.
# python program to add two numbers
# take inputs
num1 = 5
num2 = 10
# add two numbers
sum = num1 + num2
# displaying the addition result
print('{0} + {1} = {2}'.format(num1, num2, sum))
2.
# python program to add two numbers provided by the user
# store input numbers
num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')
# add two numbers
# User might also enter float numbers
sum = float(num1) + float(num2)
# displaying the adding result
# value will print in float
print('The sum of numbers {0} and {1} is {2}'.format(num1, num2, sum))
3.
# Python program to add two numbers using function
def add_num(a,b): #user-defined function
sum = a + b #adding numbers
return sum #return value
#taking input from the user
num1 = float(input('Enter first number : '))
num2 = float(input('Enter second number : '))
#function call
print('The sum of numbers {0} and {1} is {2}'.format(
num1, num2, add_num(num1, num2)))
4.
# Python program to add two numbers in one line
# Without using any variables
print('The sum is %.2f' %(float(input('Enter First Number: '))
+ float(input('Enter Second Number: '))))
5.
# Python program to multiply three number
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
# calculate product
product = num1 * num2 * num3
# print multiplication value
print("The product of number: %0.2f" %product)
6.
# Python program to multiply three numbers using function
def product_num(num1, num2, num3): #user-defind function
num = (num1 * num2 * num3) #calculate product
return num #return value
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
# function call
product = product_num(num1, num2, num3)
# print multiplication value
print("The product of number: %0.2f" %product)
7.
# Python program to find average of five numbers
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
num4 = float(input('Enter four number: '))
num5 = float(input('Enter fifth number: '))
# calculate average
avg = (num1 + num2 + num3 + num4 + num5) / 5
# print average value
print('The average of numbers = %0.2f' %avg)
8.
# Python program to find the average of five numbers
# denotes total sum of five numbers
total_sum = 0
for n in range (5):
# take inputs
num = float(input('Enter number: '))
# calculate total sum of numbers
total_sum += num
# calculate average of numbers
avg = total_sum / 5
# print average value
print('Average of numbers =', avg)
9.
# Python program to find square root of the number
# take inputs
num = float(input('Enter the number: '))
# calculate square root
sqrt = num ** 0.5
# display result
print('Square root of %0.2f is %0.2f '%(num, sqrt))
10.
# python program to find area of circle
# store input
r = float(input('Enter the radius of the circle: '))
# calculate area of circle
area = 3.14 * r * r
# display result
print('Area of circle = %.2f ' %area)
11.
# python program to find the area of the right-angle triangle
# take inputs
base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))
# calculate area of triangle
area = (1/2) * base * height
# display result
print('Area of triangle = ',area)
12.
# Python program to calculate simple interest using function
def calculate_simple_interest(P, R, T):
# calculate simple interest
SI = (P * R * T) / 100
return SI;
if __name__ == '__main__':
# store the inputs
P = float(input('Enter principal amount: '))
R = float(input('Enter the interest rate: '))
T = float(input('Enter time: '))
# calling function
simple_interest = calculate_simple_interest(P, R, T)
# display result
print('Simple interest = %.2f' %simple_interest)
print('Total amount = %.2f' %(P + simple_interest))
13.
What does the if __name__ == "__main__": do in Python?
When the Python interpreter reads a source file, it executes all of the code found in it.
Before executing the code, it will define a few special variables. For example, if the python
interpreter is running that module (the source file) as the main program, it sets the special
__name__ variable to have a value "__main__". If this file is being imported from another
module, __name__ will be set to the module's name.
One reason for doing this is that sometimes you write a module (a .py file) where it can be
executed directly. Alternatively, it can also be imported and used in another module. By
doing the main check, you can have that code only execute when you want to run the
module as a program and not have it execute when someone just wants to import your
module and call your functions themselves.
For example, if you have 2 files one.py and two.py with the following code:
one.py:
def func():
print("func() in one.py")
print("Root of one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported")
Two.py:
import one
print("Root of two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported")
Now if you run,
$ python one.py
You will get the output:
Root of one.py
one.py is being run directly
But if you run,
$ python two.py
You will get the output:
Root of in one.py
one.py is being imported
Root of in two.py
func() in one.py
two.py is being run directly
14.
# Python program to calculate compound interest using function
def compound_interest(principal, rate, time, number):
# calculate total amount
amount = principal * pow( 1+(rate/number), number*time)
return amount;
# store the inputs
principal = float(input('Enter principal amount: '))
rate = float(input('Enter the interest rate: '))
time = float(input('Enter time (in years): '))
number = float(input('Enter the number of times that
interest is compounded per year: '))
# convert rate
rate = rate/100
# calling function
amount = compound_interest(principal, rate, time, number)
# calculate compound interest
ci = amount - principal
# display result
print('Compound interest = %.2f' %ci)
print('Total amount = %.2f' %amount)
15.
# Python Program to get the first digit of number
# take input
num = int(input('Enter any Number: '))
# get the first digit
while (num >= 10):
num = num // 10
# printing first digit of number
print('The first digit of number:', num)
16.
# Python program to reverse a number
# take inputs
num = int(input('Enter an integer number: '))
# calculate reverse of number
reverse = 0
while(num > 0):
last_digit = num % 10
reverse = reverse * 10 + last_digit
num = num // 10
# display result
print('The reverse number is = ', reverse)
17.
# Python program to make a simple calculator
# take inputs
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# choise operation
print("Operation: +, -, *, /")
select = input("Select operations: ")
# check operations and display result
# add(+) two numbers
if select == "+":
print(num1, "+", num2, "=", num1+num2)
# subtract(-) two numbers
elif select == "-":
print(num1, "-", num2, "=", num1-num2)
# multiplies(*) two numbers
elif select == "*":
print(num1, "*", num2, "=", num1*num2)
# divides(/) two numbers
elif select == "/":
print(num1, "/", num2, "=", num1/num2)
else:
print("Invalid input")
18.
# Python program to print fibonacci series up to n-th term
# take input
num = int(input('Enter number of terms: '))
# print fibonacci series
a, b = 0, 1
i = 0
# check if the number of terms is valid
if num <= 0:
print('Please enter a positive integer.')
elif num == 1:
print('The Fibonacci series: ')
print(a)
else:
print('The Fibonacci series: ')
while i < num:
print(a, end=' ')
c = a + b
a = b
b = c
i = i+1
19.
# Python program to print fibonacci series up to n-th term
# take input
num = int(input('Enter number of terms: '))
# print fibonacci series
a, b = 0, 1
# check if the number of terms is valid
if num <= 0:
print('Please enter a positive integer.')
elif num == 1:
print('The Fibonacci series: ')
print(a)
else:
print('The Fibonacci series: ')
for i in range (1, num+1):
print(a, end=' ')
c = a + b
a = b
b = c
20.
# Python program to print multiplication table
# take inputs
num = int(input('Display multiplication table of: '))
# print multiplication table
for i in range(1, 11):
print ("%d * %d = %d" % (num, i, num * i))
21.
# Python program to print multiplication table
# take inputs
num = int(input('Display multiplication table of: '))
# print multiplication table
i = 1
while i <= 10:
print ("%d * %d = %d" %(num, i, num * i))
i = i+1
22.
# Python program to find factors of a number
# take inputs
num = int(input('Enter number: '))
# find factor of number
print('The factors of', num, 'are:')
for i in range(1, num+1):
if(num % i) == 0:
print(i, end=' ')
23.
# Python program to find the factorial of a number
# take input
num = int(input("Enter number: "))
# check number is positive, negative, or zero
if num < 0:
print('Factorial does not exist for negative numbers')
elif num == 0:
print('The factorial of 0 is 1')
else:
# find factorial of a number
fact = 1
for i in range(1,num + 1):
fact = fact*i
print('The factorial of',num,'is',fact)
24.
# Python program to convert decimal to binary
# take input
num = int(input('Enter any decimal number: '))
# display result
print('Binary value:', bin(num))
25.
# Python program to convert decimal to hexadecimal
# take inputs
num = int(input('Enter a decimal number: '))
# display result
print('HexaDecimal value = ', hex(num))
26.
# Python program to convert decimal to hexadecimal
# take inputs
num = int(input('Enter a decimal number: '))
# display result
print('HexaDecimal value = ', hex(num))
27.
# Python program to print numbers from 1 to 10
print('Numbers from 1 to 10:')
for n in range(1, 11):
print(n, end=' ')
28.
# Python program to print numbers from 1 to 10
print('Numbers from 1 to 10:')
n = 1
while n <= 10:
print(n, end=' ')
n = n+1