4. Simple Python Program Implementation Using Conditionals Constructs
4. Simple Python Program Implementation Using Conditionals Constructs
Simple if
1. Write a python program using if statement that displays 'within range' if num is
between 0 and 100, inclusive.
3. Assume that x refers to a number. Write a code segment that prints the number’s
absolute value without using Python’s abs function.
5. Write a program to accept two integers and display the message “Equal” if they are
equal if not no message.
6. Write a series of Python statements that will read three numeric values from the user,
and then print the largest of the three.
7. Write a program to display a number if user enters negative number, if the user enters
positive number, that number should not be displayed.
if… else
1. Write a python program that displays 'within range' if num is between 0 and 100,
inclusive, and displays 'out of range' otherwise.
3. Write a program which takes a character input and checks whether it is vowel or
consonant.
4. Write a program to read the age of a candidate and determine whether it is eligible for
casting his/her own vote. If user is not eligible, display how many years are left to be
eligible.
Provided the denominator ad-cb is not equal to zero. Write a program that will read
the values of constants a,b,c,d,m and n and compute the values of x1 and x2. An
appropriate message should be printed if ad-cb=0.
8. Hourly workers typically earn overtime when they work more than 40 hours per
week. For example, overtime pay might be 150% of the regular salary for the
additional hours. Write a Python program that will ask the user for their hourly wage,
then for the hours they have worked in the past week. With these two values print the
wages earned for the week.
Nested if
1. Write a Python program in which the user enters either ‘A’, 'B', or ‘C’. If 'A' is
entered, the program should display the word ‘Apple’; if 'B' is entered, it displays
‘Banana’; and if 'C' is entered, it displays ‘Coconut’. Use nested if statements.
2. Write a series of Python statements that will read three numeric values from the user,
and then print the largest of the three.
3. Write a series of Python statements that will read three strings from the user, and then
print them in dictionary order. (Note: you can compare two strings using the relational
operators).
4. Remember the formula for the roots of a quadratic equation. For the equation ax2 +
bx + c = 0, the roots are –b +- sqrt(b2 – 4ac) / 2a. If the discriminant (the quantity
under the square root) is positive there are two real roots, if zero there is one double
root, and if negative there are two complex roots. Write a program to read the values
a, b and c and produce a message saying how many roots the equation has and their
form.
if …elif…else.
1. Using elif headers, write a Python program in which the user enters either ‘A’, 'B', or
‘C’. If 'A' is entered, the program should display the word ‘Apple’; if 'B' is entered, it
displays ‘Banana’; and if 'C' is entered, it displays ‘Coconut’.
2. Write a python program that prompts users to enter a character (O, A, B, C, F). Then
using if-elif-else construct display Outstanding, Very Good, Good, Average and Fail
respectively.
3. Write a python program to check whether the given character is alphabet, or digit or
space or special character.
4. Write a series of Python statements that will read three numeric values from the user,
and then print the largest of the three.
5. Write a program to read temperature in centigrade and display a suitable message
according to temperature state below :
i. Temp < 0 then Freezing weather
ii. Temp 0-10 then Very Cold weather
iii. Temp 10-20 then Cold weather
iv. Temp 20-30 then Normal in Temp
v. Temp 30-40 then Its Hot
vi. Temp >=40 then Its Very Hot
6. Write a Python program in which a student enters the number of college credits
earned. If the number of credits is greater than 90, 'Senior Status' is displayed; if
greater than 60, 'Junior Status' is displayed; if greater than 30, 'Sophomore Status' is
displayed; else, 'Freshman Status' is displayed.
8. An electricity board charges the following rates for the use of electricity:
For the first 200 units; 80 P per unit
For the next 100 units; 90 P per unit
Beyond 300 units; Rs. 1 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is
more than Rs. 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the names of users and number of units consumed and printout the charges
with names.
9. The National Earthquake Information Centre has the following criteria to determine
the earthquake damages. Here is the given Richter scale serve as an input data and the
characterization as output information.
One-line if statement
Write a program to check whether the given number is divisible is divisible by 5 using
one line if statement