You are on page 1of 9

CONDITIOANAL AND

ITERTAITVE
STATTEMENTS

1. Write a Python script that asks the user to enter a length in

centimetres. If the user enters a negative length, the program

should tell the user that the entry is invalid. Otherwise, the

program should convert the length to inches and print out the

result. There are 2.54 centimetres in an inch.

#CODING:

print("Input your height: ")

h_ft = int(input("Feet: "))

h_inch = int(input("Inches: "))

h_inch += h_ft * 12

h_cm = round(h_inch * 2.54, 1)

print("Your height is : %d cm." % h_cm)

#OUTPUT:

Input your height:

Feet: 5

Inches: 3

Your height is : 160 cm.


2. A store charges ? 120 per item if you bu^ less than
10 items. If you buy between 10 and 99 items, the
cost is ? 100 per item. If you buy 100 or more items,
the cost is ^ 70 per item. Write a program that asks
the user how many items they

#coding
item = int(input("enter the total number of
item = "))
if item < 10 :
print("total cost = ",item * 120 , "rs")
elif 10 < item and item < 99 :
print("total cost = ",item * 100, "rs")
else :
print("total cost = ",item * 70 , "rs")

#ouput :

enter the total number of item = 101

total cost = 7070 rs


3. 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.

#coding:
num1 = int(input("Enter number 1 : "))
num2 = int(input("Enter number 2 : "))
if num1 + 0.01 == num2:
print("close")
elif num2 + 0.01 == num1:
print("close")
else:
print("Not Close")
Output:
Enter number 1 : 15.01
Enter number 2 : 15.02
close
4. A year is a leap year if it is divisible by 4, except
that years divisible by 100 are not leap years unless
they are also divisible by 400. Write a program that
#coding
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
#output:
RESTART: C:/Users/HP/AppData/Local/Programs/Python/Python37-
32/KKKKKKKKK.PYY.py
Enter a year: 2019
2019 is not a leap year
>>>
5. Write a program to input length of three sides of a

triangle. Then check if these sides will form a triangle or

not. (Rule is : a + b >,c ; b + c > a ; c + a > b )


#coding
def checkValidity(a, b, c):
if (a + b <= c) or (a + c <= b) or (b + c <= a) :
return False
else:
return True
a=7
b = 10
c=5
if checkValidity(a, b, c):
print("Valid")
else:
print("Invalid")
output:
RESTART: C:/Users/HP/AppData/Local/Programs/Python/Python37-
32/KKKKKKKKK.PYY.py
Valid
1. Write a program that prompts for a
phone number of 10 digits and two
dashes, with dashes after the area code
and the next three numbers.

#coding

phoneNo=input('Please enter a phone number e.g,017-555-1212: ')

if len(phoneNo)==12 and (phoneNo[3] and phoneNo[7])=='-':

print('The entered phone number is valid')

else:

print('The entered phone number is invalid')

output:

Please enter a phone number e.g,017-555-1212: 013-444-4444

The entered phone number is valid


LISTS
1 .WAP that reverses an array of integers.

#coding:
A = [1, 2, 3, 4, 5, 6]
print(A)
reverseList(A, 0, 5)
print("Reversed list is")
print(A)

#output :
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1

2. Ask the user to enter a list of strings. Create a new list that

consists of those strings with their first characters removed.

#coding:

#output:
RESTART:
C:/Users/HP/AppData/Local/Programs/Python/Pyth
on37-32/KKKKKKKKK.PYY.py
enter a list of string:[1,3,5]
['1,3,5]']
TUPLES
1.Write a Python program that creates a tuple
storing first 9 terms of Fibonacci series.
#coding

#output:
Fibonacci sequence upto 10 :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21, 34
2. Write a program that inputs two tuples and
creates a third, that contains all elements of the first
followed by all elements of the second.

#OUTPUT
t1=(‘a’,’b’)
t2=(‘c’,’d’)
t3=(‘a’,’b’,’c’,’d’)
DICTIONARIES
1. Given the dictionary x = {‘k1’: ‘v1’, ‘k2’:
‘v2’, ‘k3’: ‘v3’}, create a dictionary with the
opposite mapping i.e., write a program to
create the dictionary as : inverted_x = { ‘
v1’ : ‘ k1’ , ‘ v2 ‘ : ‘ k2 ‘ , ‘ v3 ‘ : ‘ k3 ‘ }

#coding

# output

RESTART:
C:/Users/HP/AppData/Local/Programs/Pyt
hon/Python37-32/KKKKKKKKK.PYY.py
{'V1': 'K1', 'V2': 'K2', 'V3': 'K3'}

You might also like