Eee Lab 2-2 Python 2023
Eee Lab 2-2 Python 2023
Department of:
Electrical and Electronics Engineering
CERTIFICATE
Certified that this is the bonafied record of practical work done by
the year……….
INDEX
SNO DATE NAME OF THE EXPERIMENT Page. REMARKS
NO
Write a program that uses a for loop to print the numbers 8, 11,
14, 17, 20, . . . , 83, 86, 89.
3
Write a program that asks the user for their name and how
many times to print it. The program should print out the user’s
4 name the specified number of times.
Use a for loop to print a triangle like the one below. Allow the
user to specify how high the triangle should be.
5
Generate a random number between 1 and 10. Ask the user to
guess the number and print a message based on whether they
6 get it right or not.
Write a program that asks the user for two numbers and prints
Close if the numbers are within .001 of each other and Not close
7 otherwise.
Write a program that asks the user to enter a word and prints
out whether that word contains any vowels.
8
Write a program that asks the user to enter two strings of the
same length. The program should then check to see if the strings
9 are of the same length. If they are not, the program should print
an appropriate message and exit. If they are of the same length,
the program should alternate the characters of the two strings.
For example, if the user enters abcde and ABCDE the program
should print out AaBbCcDdEe.
Write a program that asks the user for a large integer and
3
1. Write a program that asks the user for a weight in kilograms and converts it to
pounds. There are 2.2 pounds in a kilogram.
4
OUTPUT:
Enter weight in kilograms: 5
5.0 Kilograms is equal to 11.0 Pounds
2.Write a program that asks the user to enter three numbers (use three separate
input statements). Create variables called total and average that hold the sum and
average of the three numbers and print out the values of total and average.
5
OUTPUT:
3.Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, . . . , 83,
86, 89.
6
OUTPUT:
4.Write a program that asks the user for their name and how many times to print it.
The program should print out the user’s name the specified number of times.
7
OUTPUT:
5.Use a for loop to print a triangle like the one below. Allow the user to specify how
high the triangle should be.
8
OUTPUT:
Enter height of triangle: 5
*
**
***
****
*****
6. Generate a random number between 1 and 10. Ask the user to guess the number
and print a message based on whether they get it right or not.
9
import random
sysgen = random.randint(1,10)
num = int(input("Guess a number between 1 and 10: "))
if num==sysgen:
print("You guess is right.")
else:
print("Sorry! System generated number is",sysgen)
OUTPUT:
Guess a number between 1 and 10: 5
Sorry! System generated number is 2
7. Write a program that asks the user for two numbers and prints Close if the
numbers are within .001 of each other and Not close otherwise.
10
OUTPUT:
Enter first number: 1.235
Enter second number: 1.235469
Close
8. Write a program that asks the user to enter a word and prints out whether that
word contains any vowels.
11
OUTPUT:
Enter a word: govardhan
oaa
9. Write a program that asks the user to enter two strings of the same length. The
program should then check to see if the strings are of the same length. If they are
12
not, the program should print an appropriate message and exit. If they are of the
same length, the program should alternate the characters of the two strings. For
example, if the user enters abcde and ABCDE the program should print out
AaBbCcDdEe.
OUTPUT:
10. Write a program that asks the user for a large integer and inserts commas into it
according to the standard American convention for commas in large numbers. For
instance, if the user enters 1000000, the output should be 1,000,000.
num = int(input("Enter large integer: "))
sa = str(num)
for i in range(len(sa)-3,0,-3):
sa = sa[:i] + ',' + sa[i:]
print("Standard American convention is :",sa)
OUTPUT:
11. In algebraic expressions, the symbol for multiplication is often left out, as in
3x+4y or 3(x+5). Computers prefer those expressions to include the multiplication
symbol, like 3*x+4*y or 3*(x+5). Write a program that asks the user for an algebraic
expression and then inserts multiplication symbols where appropriate.
AlgExpr = input("Enter algebric expression: ")
ConvertedExpr = ''
for ch in AlgExpr:
if ch>='0' and ch<='9':
ConvertedExpr = ConvertedExpr + ch
elif ch=='(':
ConvertedExpr = ConvertedExpr + '*' + ch
elif ch>='a' and ch<='z' and ConvertedExpr[-1]!='(':
ConvertedExpr = ConvertedExpr + '*' + ch
else:
ConvertedExpr = ConvertedExpr + ch
print("Converted expression is :",ConvertedExpr)
OUTPUT:
Enter Algebraic expression: 3x+4y
The expression format is: 3*x+4*y
12. Write a program that generates a list of 20 random numbers between 1 and 100.
(a) Print the list.
(b) Print the average of the elements in the list.
(c) Print the largest and smallest values in the list.
(d) Print the second largest and second smallest entries in the list
(e) Print how many even numbers are in the list.
import random
numList=[]
for i in range(20):
numList.append(random.randint(1,100));
print("List is :",numList)
print("Average is :",sum(numList)/20)
print("Largest element is",sorted(numList)[-1],"and smallest element is",sorted(numList)
[0])
print("Second largest element is",sorted(numList)[-2],"and second smallest element
is",sorted(numList)[1])
count=0
for ele in numList:
if ele%2==0:
count+=1
print("Total number of even elements are :",count)
OUTPUT:
List:[35,35,45,67,89,56,23,45,,67,78,98,32]
Average: 56.05
Largest value: 98
Smallest value: 32
16
13. Write a program that asks the user for an integer and creates a list that consists
of the factors of that integer.
num = int(input("Enter a number : "))
factors=[]
for i in range(1,num+1):
if num%i==0:
factors.append(i)
print("Factors of",num,"are :",*factors,end="")
OUTPUT:
14. Write a program that generates 100 random integers that are either 0 or 1. Then
find the longest run of zeros, the largest number of zeros in a row. For instance, the
longest run of zeros in [1,0,1,1,0,0,0,0,1,0,0] is 4.
import random
bitsList=[]
for i in range(100):
bitsList.append(random.randint(0,1))
maxZeroSeq=zeroCount=0
for i in range(100):
if bitsList[i]==0:
zeroCount+=1
continue
else:
if maxZeroSeq<zerocount:
maxZeroSeq=zeroCount
zeroCount=0
print("Longest run of zeros in",bitsList,"is",maxZeroSeq)</zerocount:
OUTPUT:
Random list is:[1,1,1,0,0,0,
1,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,
0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,01,1,1,0,0,0
1,1,1,0,0,01,1,1,0,0,01,1,1,0,0,0]
Longest run of zeros in arrow: 7
18
15. Write a program that removes any repeated items from a list so that each item
appears at most once. For instance, the list [1,1,2,3,4,3,0,0] would become [1,2,3,4,0].
n=int(input("Enter number of elements to be insert: "))
eles=[]
print("Enter {} elements : ".format(n))
for i in range(n):
eles.append(int(input()))
for i in range(n-1):
j=i+1
while j<n:
if eles[i]==eles[j]:
del eles[j]
j-=1
n-=1
j+=1
print("After removing duplicates list is",eles)
OUTPUT:
16. Write a function called sum_digits that is given an integer num and returns the
sum of the digits of num.
def sum_digits(num):
sd=0
while num!=0:
sd=sd+num%10
num=num//10
return sd
OUTPUT: