11/26/22, 10:02 AM elevenprograms lab.
htm
# 1.a)Python program to swap two variables Using a temporary variable
# To take inputs from the user
x = int(input('Enter value of x: '))
y = int(input('Enter value of y: '))
# create a temporary variable and swap the values
temp = x
x=y
y = temp
print('The value of x after swapping:',x)
print('The value of y after swapping:',y)
output
Enter value of x: 36
Enter value of y: 63
The value of x after swapping: 63
The value of y after swapping: 36
#1.b) Python program to swap two variables without using temporary variable
x = 50
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
output
x = 10
y = 50
2."""Write a program to compute simple interest and compound interest """
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = int(input("Enter time: "))
si = (p * r * t) / 100
ci = p * ((1 + (r / 100 ))** t) - p
print("Simple interest = ", si)
print("Compound interest = ", ci)
output
Enter principal: 10000
Enter rate: 3
Enter time: 2
Simple interest = 600.0
Compound interest = 609.0
3. """ Write Python program that accepts marks in 5 subjects (max marks for each subject is 60)
and outputs average marks &
percentage """
[Link] [Link] 1/7
11/26/22, 10:02 AM elevenprograms [Link]
m1 = int(input("Enter first subject marks: "))
m2 = int(input("Enter second subject marks: "))
m3 = int(input("Enter third subject marks: "))
m4 = int(input("Enter fourth subject marks: "))
m5 = int(input("Enter fifth subject marks: "))
avg = (m1 + m2+ m3+ m4 + m5) / 5;
perc= ((m1 + m2+ m3+ m4 + m5)*100)/(60*5)
print("Average Marks = ",avg, " and Percentage =",perc)
output:
Enter first subject marks: 12
Enter second subject marks: 36
Enter third subject marks: 32
Enter fourth subject marks: 36
Enter fifth subject marks: 51
Average Marks = 33.4 and Percentage = 55.666666666666664
4. ''' Write a Python program to convert temperatures to and from Celsius, Fahrenheit,
in option based mechanism.
Formula: T (°Fahrenheit) = (T(°Celsius)*9/5)+32 '''
print("Options are \n")
print("[Link] temperatures from Celsius to Fahrenheit \n")
print("[Link] temperatures from Fahrenheit to Celsius \n")
opt=int(input("Choose any Option(1 or 2) : "))
if opt == 1:
print("Convert temperatures from Celsius to Fahrenheit \n")
cel = float(input("Enter Temperature in Celsius: "))
fahr = (cel*9/5)+32
print("Temperature in Fahrenheit =",fahr)
elif opt == 2:
print("Convert temperatures from Fahrenheit to Celsius \n")
fahr = float(input("Enter Temperature in Fahrenheit: "))
cel=(fahr-32)*5/9;
print("Temperature in Celsius =",cel)
else:
print("Invalid Option")
output:
Options are
[Link] temperatures from Celsius to Fahrenheit
[Link] temperatures from Fahrenheit to Celsius
Choose any Option(1 or 2) : 2
Convert temperatures from Fahrenheit to Celsius
Enter Temperature in Fahrenheit: 325
Temperature in Celsius = 162.77777777777777
[Link] [Link] 2/7
11/26/22, 10:02 AM elevenprograms [Link]
5. ''' Write a python program to find largest of three numbers '''
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3
print("The largest number is",largest)
output:
Enter first number: 32
Enter second number: 21
Enter third number: 236
The largest number is 236
6. ''' Write a python program to find factorial of a number '''
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
output:
Enter a number: 6
The factorial of 6 is 720
Enter a number: -9
Factorial does not exist for negative numbers
7. # Write a python program for Multiplication table (from 1 to 10) in Python
num = int(input("Enter a number: "))# To take input from the user
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
[Link] [Link] 3/7
11/26/22, 10:02 AM elevenprograms [Link]
output:
Enter a number: 3
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
8. Write a python program to add”n” natural numbers
#logic is sum = 1+2+3+...+n
n = 10
# initialize sum and counter
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter
print("The sum is", sum)
output: The sum is 55
9.#Program to add first even numbers up to n
# logic is: sum = 2+4+6...+n
n = int(input("enter n value:"))
# initialize sum and counter
sum = 0
i=2
while i <= n:
sum+= i
i+=2 # update counter
# print the sum
print("The sum is", sum)
output
enter n value:25
[Link] [Link] 4/7
11/26/22, 10:02 AM elevenprograms [Link]
The sum is 156
10. # Python program to find sum of elements in list
total = 0
list1 = [11, 345, 786, 180, 209,903]
for i in range(0, len(list1)):
total = total + list1[i]
# printing total value
print("Sum of all elements in given list: ", total)
output: Sum of all elements in given list: 2434
11. Write a python program on linear search using list elements
list1=eval(input("Enter the list elements:"))
length=len(list1)
element=int(input("Enter the element to e searched for:"))
for i in range(0,length):
if element==list1[i]:
print(element,"found at index",i)
break
else:
print(element,"not present in the given list")
output:
Enter the list elements:10,20,30,2,96,36
Enter the element to e searched for:39
39 not present in the given list
Enter the list elements:12,23,34,56,96,63
Enter the element to e searched for:56
56 found at index 3
12. program to input “n” [Link] employees and their salaries, display the output
num=int(input("Enter the number of employees: "))
count = 1
employee = dict()
for count in range(num):
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary:"))
employee[name] = salary
print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
[Link] [Link] 5/7
11/26/22, 10:02 AM elevenprograms [Link]
output:
Enter the number of employees: 3
Enter the name of the Employee: akshara
Enter the salary:12000
Enter the name of the Employee: anusha
Enter the salary:18000
Enter the name of the Employee: ridhi
Enter the salary:18500
EMPLOYEE_NAME SALARY
akshara 12000
anusha 18000
ridhi 18500
13. Write a program to create a dictionary with roll number, name and
#marks of 'n' students in a class and display the names of students
who have scored marks above 75
n=int(input ("enter number of students"))
result={}
for i in range (n):
print("enter details of students:",i+1)
rno=int(input("roll number:"))
name=input("name:")
eng=float(input ("enter english marks:"))
math=float(input("enter math score :"))
com=float(input("enter computer marks:"))
phy=float(input("enter physics marks:"))
chem=float(input("enter chemistry marks:"))
total=eng+math+com+phy+chem
per=(total/500)*100
result[rno]=[name,per]
print(result)
for s in result:
if result[s][1]>75:
print("following students having more than 75 marks:")
print(result[s][0],result[s][1])
OUTPUT
enter number of students3
enter details of students: 1
roll number:100
name:satyanarayana
enter english marks:89
enter math score :77
enter computer marks:67
enter physics marks:99
enter chemistry marks:99
[Link] [Link] 6/7
11/26/22, 10:02 AM elevenprograms [Link]
{100: ['satyanarayana', 86.2]}
following students having more than 75 marks:
satyanarayana 86.2
enter details of students: 2
roll number:101
name:pranitha
enter english marks:78
enter math score :76
enter computer marks:67
enter physics marks:66
enter chemistry marks:66
{100: ['satyanarayana', 86.2], 101: ['pranitha', 70.6]}
following students having more than 75 marks:
satyanarayana 86.2
enter details of students: 3
roll number:103
name:anand
enter english marks:88
enter math score :78
enter computer marks:76
enter physics marks:88
enter chemistry marks:78
{100: ['satyanarayana', 86.2], 101: ['pranitha', 70.6], 103: ['anand', 81.6]}
following students having more than 75 marks:
satyanarayana 86.2
following students having more than 75 marks:
anand 81.6
[Link] [Link] 7/7