0% found this document useful (0 votes)
4 views

Python Record

The document contains a series of Python programming exercises that cover various topics such as user input, loops, conditionals, and functions. Each exercise includes a description, code implementation, and sample output. The exercises range from basic arithmetic operations to more complex tasks like generating random numbers and manipulating strings.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Record

The document contains a series of Python programming exercises that cover various topics such as user input, loops, conditionals, and functions. Each exercise includes a description, code implementation, and sample output. The exercises range from basic arithmetic operations to more complex tasks like generating random numbers and manipulating strings.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

"""write5 a python program that asks user for a weight in kilo greams and converts it in to
pounds.there are 2.2 pounds in kilo gram"""
# weight in kilo gram is wkg
wkg=float(input("enter the weight in kilo grams"))
### weight in pound is wp
wp=wkg*2.2
#t output statement
print(wp)
output:
enter the weight in kilo grams67
147.4

2. # write a python program that asks the user to enter three numbers (use seperate statements).
create variables called total and average that holds sum and average of three numbers and print out
the values of total and average .

# the frist value is fv

fv=int(input('enter the frist value'))

#the second value is sv and the third is tv

sv=int (input("enter the second value"))

tv=int(input("enter the thrid value"))

#creating variable total and average

total=fv+sv+tv

average=total/3

# the output print statements

print("sum of three numbers is",total,"and average of three numbers is",average)

output:

enter the frist value68

enter the second value79

enter the thrid value45

sum of three numbers is 192 and average of three numbers is 64.0

3. # write a python program that asks the user for loop to print numbers 8,11,14,17,20,......83,86,89

#assinging a variable of value 8

i=8
# using while loop

while(i<=90):

#out put statements

print(i,end=",")

#increment pf value by 3

i=i+3

output:

8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83,86,89,

4. # write a program that asks the user their name and how many times to print it.the program
should print out users name the specified no.of.times

#use variable n for the name of the user

n=input("enter name:")

# use variable t for no.of times to print

t=int(input("enter how many times has to print"))

# define a variable for iteration as i

for i in range(0,t):

print(n)

output:

enter name:sravan

enter how many times has to print5

sravan

sravan

sravan

sravan

sravan

5. # use a for loop to print a triangle with the stars.allow user to secify what is the height of triangle

# height of triangle is h

h=int(input("enter the height of triangle"))


# uses iteration

for i in range(1,h+1):

#out put staements

print("*"*i,end="\n")

output:

enter the height of triangle5

**

***

****

*****

6. # generate a random number between 1 to 10. ask the user to guess the nuber and print
appropriate meassage

# import random operation from library

import random

# define a random integer as n

n= random.randint(1, 10)

# define user guess intger as g

g= int(input("enter your guess number"))

# conditional statements

if(n==g):

#out put statements

print("your guess is right")

else:

#output staements

print("your guess is wrong")

output:

enter your guess number4


your guess is wrong

7. # write a program that asks for two numbers and prints close if the numbers are within 0.001 of
each other and not close other wise

# taking numbers in float type

a=float(input("enter the frist number"))

b=float(input("enter the second number"))

#conditional statements

if((a-b)<=0.001)or((b-a)<=0.001):

print("close")

else:

print("not close")

output:

enter the frist number3.002

enter the second number3.001

close

8. # wroite aprogram to check if the string contains vowels (or) not

# take string from the user as input for variable n

n=input("enter the string")

#conditional staements

for letter in n:

if letter in "aeiouAEIOU":

v=1

if(v==1):

print("vowels are present")

else:

print("vowels are not present")

output:

enter the string sravan


vowels are present

9. # write a program that asks the user to enter two strings of the same length. The program should
check to see ,if the strings 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 alternate the characters
of two strings.

s1=input("enter the frist string")

s2= input("enter the second string")

n=0

if(len(s1)==len(s2)):

print("same lenght")

n=1

else:

print("not same lenght")

new=" "

if(n==1):

for i in range(0,len(s1)):

new=new+s1[i]+s2[i]

print(new)

output:

enter the frist stringsravan

enter the second stringsujay

not same length

10. #Write a program that asks the user for a large integer and insert 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.

def place_value(number):

return ("{:,}".format(number))

num=int(input("enter the number"))

print(place_value(num))
output:

enter the number10000000000000

10,000,000,000,000

11.eq=input("enter the equation")

new=" "

a="-*+/"

for i in range(0,len(eq)-1):

if (eq[i] not in a)and(eq[i+1] not in a):

new=new+eq[i]+"*"+eq[i+1]

else:

if(eq[i+1] in a):

new=new+eq[i+1]

else:

new=new

print(new)

output:

enter the equation2x+3y

2*x+3*y

12. import random

lst=[]

even_count=0

for i in range(0,20):

n=random.randint(1,100)

lst.append(n)

print(lst)

for i in range(100,1,-1):

if i in lst :
larger=i

break

for i in range(larger-1,1,-1):

if i in lst :

slarger=i

break

for i in range(1,100):

if i in lst:

smallest=i

break

for i in range(smallest+1,100):

if i in lst:

ssmallest=i

break

for val in lst:

if(val%2==0):

even_count+=1

print("the largest number in the list is",larger)

print("the second larger number in the list is",slarger)

print("the smallest number in the list is ",smallest)

print("the second smallest number in list is",ssmallest)

print("the no of even numbers in the list is",even_count)

output:

[1, 66, 29, 97, 18, 89, 98, 1, 71, 16, 67, 38, 56, 56, 25, 11, 98, 99, 11, 9]

the largest number in the list is 99

the second larger number in the list is 98

the smallest number in the list is 1


the second smallest number in list is 9

the no of even numbers in the list is 8

13. n=int(input("enter the number"))

flist=[]

for i in range(1,n+1):

if(n%i==0):

flist.append(i)

print(flist)

output:

enter the number55

[1, 5, 11, 55]

14. import random

list=[]

for i in range(0,100):

x=random.randint(0,1)

list.append(x)

print(list)

c=0

max_count=0

for i in list:

if i==0:

c=c+1

else:

if c>max_count:

max_count=c

else:

c=0
print("maximum consective zeros count is:",max_count)

output:

[0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1,
0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1]

maximum consective zeros count is: 11

15. n=eval(input("enter the list"))

list=[]

newlist=[]

list.append(n)

for i in n:

if i not in newlist:

newlist.append(i)

print(newlist)

output:

enter the list 2,3,4,5,6,7,8

[2, 3, 4, 5, 6, 7, 8]

16. l_feet=int(input("enter the lenght in feet"))

print("1.inches\n2.yard\n3.mile\n4.millimeters\n5.centi meters\n6.meters")

c=int(input("enter your choice"))

if(c<=6):

if(c==1):

l_inch=l_feet*12

print(l_inch)

if(c==2):

l_yard=l_feet*(1/3)

print(l_yard)

if(c==3):
l_mile=l_feet*0.000189394

print(l_mile)

if(c==4):

l_mm=l_feet*304.8

print(l_mm)

if(c==5):

l_cm=l_feet*30.48

print(l_cm)

if(c==6):

l_m=l_feet*0.3048

print(l_m)

else:

print("enter the choice between 1 to 6")

output:

enter the lenght in feet 5

1.inches

2.yard

3.mile

4.millimeters

5.centi meters

6.meters

enter your choice1

60

17. def sodn(n):

sum=0

while(n>0):

sum=sum+n%10
n=n//10

return sum

n=int(input("enter the number"))

print("the sum of digits in number is",sodn(n))

output:

enter the number25

the sum of digits in number is 7

18. s1=input("enter the string 1")

s2=input("enter the string 2")

def frist_diff(s1,s2):

f=0

n=0

if(len(s1)>=len(s2)):

n=len(s2)

else:

n=len(s1)

for i in range(0,n):

if(s1[i]!=s2[i]):

f=1

return i

break

if(f==0):

return -1

print("the frist difference is at location",frist_diff(s1,s2)+1)

output:

enter the string 1sravan


enter the string 2sujay

the frist difference is at location 2

19. def nof(n):

factors=0

for i in range(1,n+1):

if(n%i==0):

factors+=1

return factors

n=int(input("enter the number"))

print("the number of factors for",n,"is",nof(n))

output:

enter the number45

the number of factors for 45 is 6

20. def is_sorted(l):

for i in range(0,len(l)-1):

if(l[i]>l[i+1]):

break

else:

continue

if(i==len(l)-2):

return True

else:

return False

list=[1,2,2,3,3,4]

print(is_sorted(list))

output:

True
21.

def root(n,r):

a=0

if(r==0):

r=2

a=pow(n,1/r)

else:

a=pow(n,1/r)

return a

n=int(input("enter the number"))

r=int(input("enter the root number"))

print(root(n,r))

output;

enter the number54

enter the root number6

1.9441612972396656

22. def prime(n):

f=0

for i in range (1,n):

if(n%i==0):

f=1

break

if(f==0):

return n

def count_prime(N):

i=2

lst=[]
c=0

while(c<=N):

if(prime(i)==i):

lst.append(i)

c+=1

return lst

N=int(input("enter the number of prime numbers"))

print(count_prime(N))

output:

see I the lab manual

23. # case a

a=[]

b=[]

n1=int(input("enter the lenght of list 1"))

for i in range(0,n1):

n=int(input("enter the value"))

a.append(n)

n2=int(input("enter the lenght of list 2"))

for i in range(0,n2):

n=int(input("enter the value"))

b.append(n)

new=a+b

print(new)

output:

enter the lenght of list 15

enter the value1

enter the value2


enter the value32

enter the value3

enter the value4

enter the lenght of list 25

enter the value12

enter the value3

enter the value4

enter the value5

enter the value6

[1, 2, 32, 3, 4, 12, 3, 4, 5, 6]

25. import itertools

s=input("enter the word")

t=list(itertools.permutations(s,len(s)))

for i in range(0,len(t)):

print("".join(t[i]),end=" ")

output:

enter the wordbook

book boko book boko bkoo bkoo obok obko oobk ookb okbo okob obok obko oobk ookb okbo okob
kboo kboo kobo koob kobo koob

26. f=open("gmail.txt","r")

b=f.readlines()

for i in range(0,len(b)):

print(b[i],end=";")

output:

sravan2207@gmail.com

;sujayraghavendra@gmail.com

;rohit679@gmail.com

You might also like