Exercise 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.
Aim: To Write a python program that asks the user for a weight in kilograms and converts it to pounds. There
are 2.2 pounds in a kilogram
Source code:
a=int(input("enter the weight in kilograms"))
a=a*2.2
print("weight=",a)
OUTPUT:
enter the weight in kilograms 100
weight= 220.00000000000003
Exercise 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.
Aim: To Write a python program that asks the user to enter three numbers. 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.
Source code:
a=int(input("enter a value="))
b=int(input("enter b value="))
c=int(input("enter c value="))
total=a+b+c
average=(a+b+c)/3
print("the total value=",total)
print("the average value=",average)
OUTPUT:
enter a value=10
enter b value=20
enter c value=30
the total value= 60
the average value= 20.0
Exercise 3:
Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, . . . , 83,
86, 89.
Aim: To Write a python program that asks the user to that uses a for loop to print the numbers
8, 11, 14, 17, 20, . . . , 83,86, 89.
Source code:
for i in range(8,90,3):
print(i)
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
Exercise 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.
Aim: To Write a python 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.
Source code:
a=input("enter a string=")
n=int(input("enter the range of the string="))
while n>=0:
n=n-1
print(a)
OUTPUT:
enter a string=Rajesh
enter the range of the string=3
Rajesh
Rajesh
Rajesh
Exercise 5:
Use a for loop to print a triangle like the one below. Allow the user to specify how
high the triangle should be.
*
**
***
****
Aim: To Write a python program that asks the Use a for loop to print a triangle
Source code:
for i in range(0,4):
for j in range(0,i+1):
print("*",end="")
print("\r")
OUTPUT:
**
***
****
Exercise 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 no
Aim: To Write a python program that asks the user to 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 no
Source code:
import random
y=int(input("guess a number"))
a=1
b=10
sum=random.randint(a,b)
print("the output number=",sum)
if y==sum:
print("u got it right")
else:
print("oops! better luck next time")
OUTPUT:
guess a number 9
the output number= 10
oops! better luck next time
Exercise 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.
Aim: To Write a python program that asks the user for two numbers and prints Close if the numbers
are within .001 of each other and Not close otherwise.
Source code:
a=int(input("enter first number"))
b=int(input("enter second number"))
if a<.001<b:
print("close the program")
else:
print("the numbers are not in between a and b")
OUTPUT:
enter first number 3
enter second number 4
the numbers are not in between a and b
Exercise 8:
Write a program that asks the user to enter a word and prints out whether that word
contains any vowels.
Aim: To Write a python program that asks the user to enter a word and prints out whether that
word contains any vowels.
Source code:
a=input("enter a word=")
vowels=['a','e','i','o','u']
for i in vowels:
if i in a:
print(i)
OUTPUT:
enter a word Rajesh
a
e
Exercise 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 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.
Aim: To Write a python program that asks the user to enter two strings of the same length.
Source code:
str1=input("enter first string")
str2=input("enter second string")
if len(str1)==len(str2):
print("hi")
result=''
for i in range(len(str1)):
result=result+(str2[i]+str1[i])
print(result)
else:
print("it is an appropriate message")
OUTPUT:
enter first string rajesh
enter second string RAJESH
those has same lengths
RrAaJjEeSsHh
Exercise 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.
Aim: To Write a python 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.
Source code:
s=1000000
print("{:,}".format(s))
OUTPUT:
1,000,000