In this training, you will learn to program with Python using practical examples. In this tutorial, you’ll learn to write conditional tests, which allow you to check any condition of interest. You’ll learn to write simple if statements, and you’ll learn how to create a more complex series of if statements to identify when the exact conditions you want are present.

Introduction

Programming often involves examining a set of conditions and deciding which action to take based on those conditions. Python’s if statement allows you to examine the current state of a program and respond appropriately to that state.

Example:

age = int(input("Enter your age: "))
if age < 4 :
     print("Your admission cost is 0$.")
elif age < 18:
     print("Your admission cost is 25$")
else:
     print("Your admission cost is 40$.")

Indentation is very important in Python because it shows which lines are dependent on others, as shown in the side example. To indent text, you can use the [ Tab ] key or press the [ Space ] key five times. All indented lines after an if statement will be executed if the test passes, and the entire block of indented lines will be ignored if the test does not pass.

Comparison operators

Operators Meaning Example Result
< Less than 7<3 False
> Greater than 7>2 True
<= Less than or equal to 7<=2 False
>= Greater than or equal to 7>=2 True
== Equal to 7==2 False
!= Not equal to 7!=2 True

Logical operators

Operator Meaning Example Result
and Logical and (7<2) and (7>3) False
or Logical or (7<2) or (7>3) True
not Logical not not (7<2) True

Exo II.1

Q. Ask for two numbers. If the first one is larger than the second, display the second number first and then the first number, otherwise show the first number first and then the second.

R.

num1 = int(input("Enter the first number : "))
num2 = int(input("Enter the second number : "))

if num1 > num2 :
    print(num2, "-", num1)
else:
    print(num1, "-", num2)

Exo II.2

Q. Write a program that asks the user to type in an integer and displays WIN if the integer is between 45 and 65 included LOST otherwise.

R.

num = int(input("Enter an integer : "))

if num >= 45 and num <= 65 :
    print("WIN !")
else:
    print("LOST !")

Exo II.3

Q. Ask the user to enter a number that is under 50. If they enter a number that is 50 or more, display the message “Too high”, otherwise display “Thank you”.

 R.

var = int(input("Enter a value less than 50: " ))

if var >= 50 :
    print("To high")
else:
    print("Thank you")

Exo II.4

Q

Write an if - elif - else chain that determines a person’s stage of life. Set a value for the variable age , and then:
• If the person is less than 2 years old, print a message that the person is a baby.

• If the person is at least 2 years old but less than 4, print a message that the person is a toddler.

• If the person is at least 4 years old but less than 13, print a message that the person is a kid.

• If the person is at least 13 years old but less than 20, print a message that the person is a teenager.

• If the person is at least 20 years old but less than 65, print a message that the person is an adult.

• If the person is age 65 or older, print a message that the person is an elder.

 R.

age = int(input("Your age please : "))

if age < 2 : print("You are Baby")
elif age >= 2 and age < 4 : print("You are Toddler")
elif age >= 4 and age < 13 : print("You are Kid")
elif age >= 13 and age < 20 : print("You are Teenager")
elif age >= 20 and age < 65 : print("You are Adult")
else : print("You are Elder")

Exo II.5

Q. Ask the user to enter their favourite colour. If they enter “red”, “RED” or “Red” display the message “I like red too”, otherwise display the message “I don’t like [colour], I prefer red”.

 R.

colour = input("Type in your favourite colour : ")

if colour == "red" or colour == "RED" or colour == "Red":
    print("I like red too")
else:
    print("I don't like that colour. I like red")

Exo II.6

Q. Ask the user to enter 1, 2 or 3. If they enter a 1, display the message “Thank you”, if they enter a 2, display “Well done”, if they enter a 3, display “Correct”. If they enter anything else, display “Error message”.

 R.

var = input("Enter 1, 2 or 3 : ")

if var == "1" : print("Thank you")
elif var == "2" : print("Well dobe")
elif var == "3" : print("Correct")
else : print("Error message")

Exo II.7

Q. Write an if statement that asks for the user's name via input() function and convert their answer to lower case so it doesn’t matter what case they type it in. If the name is "Bond" make it print "Welcome on board 007." Otherwise make it print "Good morning [NAME]".

 R.

name = input("Type in your name : ")
name = str.lower(name)

if name == "bond" :
    print("Welcom on board 007")
else:
    print("Good morning ", name)

Exo II.8

Q. Write a program to calculate the electricity bill (accept number of unit from user) according to the following criteria :
             Unit                                                     Price  
First 100 units                                               no charge
Next 100 units                                              2$ per unit
After 200 units                                              4$ per unit
(For example if input unit is 350 than total bill amount is 100$)

 R.

num = int(input("Enter number of electric unit :"))

if num <= 100 :
    amount = 0
elif num > 100 and num <= 200:
    amount = (num - 100)*2
else:
    amount = 200 + (num - 200)*4

print(f"Amount to pay : {amount} $") 

Exo II.9

Q. Write a program to accept a number from 1 to 7 and display the name of the day like 1 for Sunday , 2 for Monday and so on.

 R.

num = int(input("Enter any number between 1 to 7 :"))

if num == 1 : print("Sunday")
elif num == 2 : print("Monday")
elif num == 3 : print("Tuesday")
elif num == 4 : print("Wednesday")
elif num == 5 : print("Thursday")
elif num == 6 : print("Friday")
elif num == 7 : print("Saturday")
else : print("Please enter number between 1 to 7")

Exo II.10

Q. Ask the user if it is raining and convert their answer to lower case so it doesn’t matter what case they type it in. If they answer “yes”, ask if it is windy. If they answer “yes” to this second question, display the answer “It is too windy for an umbrella”, otherwise display the message “Take an umbrella”. If they did not answer yes to the first question, display the answer “Enjoy your day”.

 R.

rain = input("Is it raining ?")
rain = str.lower(rain)

if rain == "yes":
    windy = input("Is it windy ?")
    windy = str.lower(windy)
    if windy == "yes":
        print("It is too windy for an umberella")
    else:
        print("Take an umberella")
else:
    print("Enjoy your day")

 

0 comment

There are no comments yet.

Log in to leave a reply

Related posts

Developing a Web Application with Django Part 1: Getting Started

1/12/2021

Developing a Web Application with Django Part 2: Templates

15/12/2021

Developing a Web Application with Django Part 3 : Models

7/1/2022