Professional Documents
Culture Documents
OVERVIEW
UNIT 3
First Part:
Working with user input, error handling using try and except.
USER INPUTS
Python provides us with two built-in functions to read the input from the
keyboard and this are:
raw_input ( prompt )
User Inputs
This function works in older version (like Python 2.x). This function takes exactly what is typed from
the keyboard, convert it to string and then return it to the variable in which we want to store.
sample code:
input ( prompt )
This function first takes the input from the user and then evaluates the expression, which means
Python automatically identifies whether user entered a string or a number or list. If the input
provided is not correct then either syntax error or exception is raised by python.
sample code:
y = input("Enter your value: ")
print(y)
3 4 By: JMI
IF STATEMENTS
In Python, If Statement is used for decision making. It will run the body of
code only when IF statement is true.
In programming, if statement is known as branching statements
CONDITIONALS
Statement Description
if statements An if statement consists of a boolean expression followed
by one or more statements.
if...else An if statement can be followed by an optional else
statements statement, which executes when the boolean expression is
FALSE.
9 10 By: JMI
IF STATEMENTS IF STATEMENTS
Syntax 1:
CLARIFICATIONS:
if expression:
if code/statement(s)
When you want to justify one condition while the other condition is not true, then you use else:
"if statement". An "if statement" is written by using the if keyword. else code/statement(s)
An else statement can be combined with an if statement. An else statement contains the Syntax 2:
block of code that executes if the conditional expression in the if statement results to 0 or
if expression1:
a FALSE value. The else statement is an optional statement and there could be at most
if code/statement(s)
only one else statement following if.
elif expression2:
The elif statement allows you to check multiple expressions for TRUE and execute a
elif code/statement(s)
block of code as soon as one of the conditions evaluates to TRUE.
elif expression3:
*Similar to the else, the elif statement is optional. However, unlike else, for which
elif code/statement(s)
there can be at most one statement, there can be an arbitrary number of elif statements
else:
following an if.
else code/statement(s)
11 By: JMI 12 By: JMI
IF STATEMENTS IF STATEMENTS
PYTHON CONDITIONS
using an if statement to only run code if the condition is met
If statements are used to test for particular conditions and respond appropriately. sample code:
Python supports the usual logical conditions from mathematics: x, y = 5, 10
if x < y:
print("x is less than y")
A great use for our newly learned conditional statement is for checking user input.
sample code:
ans = int(input("What is 5 + 5? "))
if ans == 10:
print("You got it right!")
else:
print("Your answer is incorrect :(")
These conditions can be used in several ways, most commonly in "if statements" and
loops.
13 By: JMI 14 By: JMI
IF STATEMENTS IF STATEMENTS
using an if , elif, else statements: One line if else statement: checking more than one or multiple “elif” conditional statement:
sample code: sample code:
sample code:
a = int(input("Enter your 1st number: ")) a = int(input("Enter your 1st number: ")) x = int(input("Enter your 1st number: "))
b = int(input("Enter your 2nd number: ")) b = int(input("Enter your 2nd number: ")) y = int(input("Enter your 2nd number: "))
print (f"a={a} and b={b}") print (f"a={a} and b={b}") print (f"x={x} and y={y}")
print("b is greater than a") if b > a else print ("b less than
if b > a: a") if x > y:
print("b is greater than a") print("x is greater")
elif a == b: Multiple else statement on the same line: elif (x + 10) < y: # checking if (x+10) is less than y
print ("a equals b") sample code: print("x is less")
else: elif (x + 5) == y: # checking if (x+5) is equal to y
print(“b is less than a") a = int(input("Enter your 1st number: ")) print("equal")
b = int(input("Enter your 2nd number: ")) else:
print (f"a={a} and b={b}") print("conditions are FALSE")
print("A") if a > b else print("=") if a == b else print("B")
15 By: JMI 16 By: JMI
IF STATEMENTS IF STATEMENTS
sample code:
The “and” logical operator is to ensure that, when you check multiple
#writing a full conditional statement with if, elif, else conditions, both sides of the condition are True. This means that if either the
condition to the left or right of the “and” is False, then the code will not run the
name = "Jeanne" block of code.
if name[0] == "A":
print(f"The Name {name} starts with an A!") using the keyword 'and' in an 'if statement'
elif name[0] == "B":
print(f"The Name {name} starts with a B!") sample code:
elif name[0] == "J": x, y, z = 5, 10, 5
print(f"The Name {name} starts with a J!") print (f"x={x}, y={y}, z={z}")
else: # covers all other possibilities print ("\n")
print(f"The Name {name} starts with a {name[0]}") if x < y and x == z:
print("Both statements were true")
LOGICAL OPERATORS
Membership Operator “Not IN”
if you want to check to see if an object doesn’t include a specific value, you
would use the “not in” operator.
This is essentially just checking the opposite of the “in” operator.
sample code:
word = "Jeanne Imbuido"
if "b" in word:
print(f"{word} contains the character b")
if "r" not in word:
print(f"{word} does not contain the character r")
23 By: JMI