0% found this document useful (0 votes)
2 views13 pages

If Statements

The document explains the use of the if statement in Python for executing code based on certain conditions, including handling true and false cases with else and elif. It also covers conditional and logical operators, inline if statements, and provides examples of programs that utilize these concepts, such as a guessing game and a simple calculator. Additionally, it includes tasks for practicing user input validation and categorizing students based on credit hours.

Uploaded by

gumbobuhle14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

If Statements

The document explains the use of the if statement in Python for executing code based on certain conditions, including handling true and false cases with else and elif. It also covers conditional and logical operators, inline if statements, and provides examples of programs that utilize these concepts, such as a guessing game and a simple calculator. Additionally, it includes tasks for practicing user input validation and categorizing students based on credit hours.

Uploaded by

gumbobuhle14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

If statement

 To do something, only when a certain


condition is true.

 Python’s if statement is what we need.

If condition:
Statements executed if condition is true
Statements executed if condition is not or no
longer true.

 Indented lines will be executed only if


the condition is true.

 Once the indentation is done with, the if


block is concluded.
Example
guess-a-number program.
 The computer picks a random number,
 the player guesses,
 and the program tells if the guess is
correct.

 We need an if statement that checks if


the guess matches the random number
generated.
Handling the other condition
(i.e. when the condition is not true)
 The previous example works but,
o When the player guesses wrong,
o the program does nothing.

 To handle if the condition is not true,


o we can add an else:
Conditional Operators (Reminder)

 ==, >, <, >=, <=, and !=


Logical operators (Reminder)

and
 Returns True if both conditions are
True.
 E.g. Grade P is only printed if and only if
o mark>=45 and mark<55.
or
 Returns True if at least one of the
conditions is True.
 E.g. prints ‘Invalid entry’ if;
o either mark > 100 or
o mark < 0
not
 Returns True if the condition is NOT
met.

 E.g. ‘Valid entry’ is printed if condition


(mark>100) or (mark<0) is not met.
Handling more than two conditions – elif
 To test more than two conditions,
o We use the elif.
Example 1
 75 - 100 = 1, 65-74 = 2.1, 55-64 = 2.2,
 45-54 = P, 0 – 44 = F
Example 2
 Program to write a simple calculator
with 4 basic arithmetic operations. i.e. +,
-, * and /

NB error on the last elif (operation==4)


Inline If Statement
 A simpler form of an if statement.

 More convenient if you only need to


perform a simple task.

 The syntax is:


do Task A if a condition is true else do Task
B

 E.g,

 this statement assigns 12 to num1 (Task


A) if a is equal to 10 (condition),
else it assigns 2 to num1 (Task B).
E.g. 2
Tasks
1. Write a program that asks the user
to enter a length in centimeters. 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
centimeters in an inch.

2. Write a program that asks the user


how many credits they have taken. If
they have taken 23 or less, print that the
student is a freshman. If they have taken
between 24 and 53, print that they are a
sophomore. The range for juniors is 54
to 83, and for seniors it is 84 and over.

You might also like