You are on page 1of 2

Beginner's Python

Numerical comparisons If statements


Testing numerical values is similar to testing string values. Several kinds of if statements exist. Your choice of which to
use depends on the number of conditions you need to test.
Testing equality and inequality
Cheat Sheet - >>> age = 18
>>> age == 18
You can have as many elif blocks as you need, and the
else block is always optional.
Simple if statement

If Statements
True
>>> age != 18 age = 19
False

and While Loops


if age >= 18:
Comparison operators print("You're old enough to vote!")
>>> age = 19
>>> age < 21 If-else statements
True age = 17
What are if statements? What are while >>> age <= 21
loops? True if age >= 18:
>>> age > 21 print("You're old enough to vote!")
Python's if statements allow you to examine the False else:
current state of a program and respond appropriately >>> age >= 21 print("You can't vote yet.")
to that state. You can write a simple if statement that False
checks one condition, or you can create a complex The if-elif-else chain
series of statements that identify the exact conditions Checking multiple conditions age = 12
you're interested in. You can check multiple conditions at the same time. The and
While loops run as long as certain conditions remain operator returns True if all the conditions listed are true. The if age < 4:
price = 0
true. You can use while loops to let your programs run or operator returns True if any condition is true.
elif age < 18:
as long as your users want them to. Using and to check multiple conditions price = 25
else:
Conditional Tests >>> age_0 = 22
price = 40
>>> age_1 = 18
A conditional test is an expression that can be evaluated
>>> age_0 >= 21 and age_1 >= 21
as true or false. Python uses the values True and False print(f"Your cost is ${price}.")
False
to decide whether the code in an if statement should be >>> age_1 = 23
executed. >>> age_0 >= 21 and age_1 >= 21 Conditional tests with lists
Checking for equality True You can easily test whether a certain value is in a list. You
A single equal sign assigns a value to a variable. A double equal can also test whether a list is empty before trying to loop
Using or to check multiple conditions
sign checks whether two values are equal. through the list.
If your conditional tests aren't doing what you expect them to, >>> age_0 = 22
make sure you're not accidentally using a single equal sign. >>> age_1 = 18 Testing if a value is in a list
>>> car = 'bmw' >>> age_0 >= 21 or age_1 >= 21 >>> players = ['al', 'bea', 'cyn', 'dale']
>>> car == 'bmw' True >>> 'al' in players
True >>> age_0 = 18 True
>>> car = 'audi' >>> age_0 >= 21 or age_1 >= 21 >>> 'eric' in players
>>> car == 'bmw' False False
False Testing if two values are in a list
Boolean values
Ignoring case when making a comparison >>> 'al' in players and 'cyn' in players
A boolean value is either True or False. Variables with
>>> car = 'Audi' boolean values are often used to keep track of certain
>>> car.lower() == 'audi'
True
conditions within a program.
Simple boolean values
Python Crash Course
Checking for inequality A Hands-on, Project-Based
game_active = True Introduction to Programming
>>> topping = 'mushrooms' is_valid = True
>>> topping != 'anchovies' can_edit = False nostarch.com/pythoncrashcourse2e
True
Conditional tests with lists (cont.) While loops (cont.) While loops (cont.)
Testing if a value is not in a list Letting the user choose when to quit Using continue in a loop
banned_users = ['ann', 'chad', 'dee'] prompt = "\nTell me something, and I'll " banned_users = ['eve', 'fred', 'gary', 'helen']
user = 'erin' prompt += "repeat it back to you."
prompt += "\nEnter 'quit' to end the program. " prompt = "\nAdd a player to your team."
if user not in banned_users: prompt += "\nEnter 'quit' when you're done. "
print("You can play!") message = ""
while message != 'quit': players = []
Checking if a list is empty message = input(prompt) while True:
An empty list evaluates as False in an if statement. player = input(prompt)
players = [] if message != 'quit':
print(message) if player == 'quit':
if players: break
Using a flag elif player in banned_users:
for player in players:
Flags are most useful in long-running programs where code from print(f"{player} is banned!")
print(f"Player: {player.title()}")
other parts of the program might need to end the loop. continue
else:
print("We have no players yet!") prompt = "\nTell me something, and I'll " else:
prompt += "repeat it back to you." players.append(player)
prompt += "\nEnter 'quit' to end the program. "
Accepting input print("\nYour team:")
You can allow your users to enter input using the input() active = True for player in players:
function. All input is initially stored as a string. If you want to while active: print(player)
accept numerical input, you'll need to convert the input string message = input(prompt)
value to a numerical type. Avoiding infinite loops
Simple input if message == 'quit':
Every while loop needs a way to stop running so it won't
active = False
name = input("What's your name? ") else: continue to run forever. If there's no way for the condition
print(f"Hello, {name}.") print(message) to become false, the loop will never stop running. You can
usually press Ctrl-C to stop an infinite loop.
Accepting numerical input using int() Using break to exit a loop
An infinite loop
age = input("How old are you? ") prompt = "\nWhat cities have you visited?"
age = int(age) while True:
prompt += "\nEnter 'quit' when you're done. "
name = input("\nWho are you? ")
if age >= 18: print(f"Nice to meet you, {name}!")
while True:
print("\nYou can vote!") city = input(prompt)
else: Removing all instances of a value from a list
print("\nSorry, you can't vote yet.") if city == 'quit': The remove() method removes a specific value from a
break list, but it only removes the first instance of the value you
Accepting numerical input using float() else: provide. You can use a while loop to remove all instances of
tip = input("How much do you want to tip? ") print(f"I've been to {city}!") a particular value.
tip = float(tip)
print(f"Tipped ${tip}.") Removing all cats from a list of pets
Accepting input with Sublime Text
Sublime Text, and a number of other text editors can't run pets = ['dog', 'cat', 'dog', 'fish', 'cat',
While loops programs that prompt the user for input. You can use these 'rabbit', 'cat']
A while loop repeats a block of code as long as a condition editors to write programs that prompt for input, but you'll print(pets)
is true. need to run them from a terminal.
while 'cat' in pets:
Counting to 5 pets.remove('cat')
Breaking out of loops
current_number = 1
You can use the break statement and the continue print(pets)
while current_number <= 5: statement with any of Python's loops. For example you can
print(current_number) use break to quit a for loop that's working through a list or a
current_number += 1 dictionary. You can use continue to skip over certain items More cheat sheets available at
when looping through a list or dictionary as well. ehmatthes.github.io/pcc_2e/

You might also like