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

4. Simple Python Program Implementation Using Conditionals Constructs

The document provides a series of Python programming exercises focused on implementing conditionals, including simple if statements, if...else structures, nested if statements, and if...elif...else constructs. Each section contains specific tasks such as checking number ranges, determining even or odd status, and calculating electricity charges based on usage. The exercises aim to enhance understanding of conditional logic in Python programming.

Uploaded by

j.rithikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

4. Simple Python Program Implementation Using Conditionals Constructs

The document provides a series of Python programming exercises focused on implementing conditionals, including simple if statements, if...else structures, nested if statements, and if...elif...else constructs. Each section contains specific tasks such as checking number ranges, determining even or odd status, and calculating electricity charges based on usage. The exercises aim to enhance understanding of conditional logic in Python programming.

Uploaded by

j.rithikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

2. Write a Python program to input an integer number. Decrement the number if it is


Negative.

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.

4. Write a program to print the square of a number if it is less than 10.

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.

2. Write a program to check whether the given number is even or odd.

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.

5. Three numbers, a, b and c, are called a Pythagorean triple if a2 + b2 = c2. An example


is the triple 3, 4 and 5, since 9 + 16 = 25. Pythagorean triples can represent, for
example, the length of sides in a right triangle. Write a series of Python statements
that will read three numbers into variables named a, b and c and then print a message
saying whether or not they are a Pythagorean triple
6. Write a program to check given year is leap year or not. (In the Gregorian calendar
three criteria must be taken into account to identify leap years:
 The year can be evenly divided by 4
 If the year can be evenly divided by 100, it is NOT a leap year, unless;
 The year is also evenly divisible by 400. Then it is a leap year.)

7. A set of 2 linear equations with 2 unknown value x1 and x2 is given below,


ax1+bx2=m
cx1+dx2=n

The set has a unique solution,

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.

7. Write a program to find the value of y using

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.

RICHTER NUMBER (N).........................CHARACTERIZATION


N<5.0-----------------------------little or no damage
5.0<=N<5.5----------------------some damage
5.5<=N<6.5. ---------------------serious damage
6.5<=N<7.5---------------------Disaster
Higher ---------------------------Catastrophe

10. Write a program to check whether a triangle is Equilateral, Isosceles or Scalene.

One-line if statement

Write a program to check whether the given number is divisible is divisible by 5 using
one line if statement

Conditional Expressions (Python’s Ternary Operator)

Write a program to check whether the given number is positive or negative.

You might also like