02.2 User Input and Control Flow
02.2 User Input and Control Flow
When you code a tuple or list on the left side of the =, Python pairs objects on the right side with
targets on the left by position and assigns them from left to right.
For example, the name spam is assigned the string 'val', and the name jam is bound to the string
'VAL'.
ASSIGNMENT STATEMENT FORMS
4) Sequence assignment, generalized
Example:
>>> a, b, c, d = 'spam'
In later versions of Python, tuple and list assignments were generalized into instances
of what we now call sequence assignment—any sequence of names can be assigned to
any sequence of values, and Python assigns the items one at a time by position.
For example, pairs a tuple of names with a string of characters:
a is assigned 's’,
b is assigned 'p', and so on.
ASSIGNMENT STATEMENT FORMS
5) Extended sequence unpacking
Example:
>>> a, *b = 'spam'
In Python 3.X (only), a new form of sequence assignment allows us to be more flexible
in how we select portions of a sequence to assign.
For example, matches a with the first character in the string on the right and b with the
rest:
a is assigned 's', and
b is assigned 'pam'.
ASSIGNMENT STATEMENT FORMS
6) Multiple-target assignment
Example:
>>> price += 42
Augmented assignment—a shorthand that combines an expression and an assignment
in a concise way.
For example, price += 42, has the same effect as price = price + 42, but the augmented
form requires, less typing and is generally quicker to run.
There is one augmented assignment statement for every binary expression operator in
Python (such as: +=, -=, *=, /=).
ASSIGNMENT STATEMENT FORMS
EXERCISE 1
Example:
>>> car = 'toyota' >>> car = 'audi'
>>> car == 'toyota' >>> car != 'audi'
True False
USING AND TO CHECK MULTIPLE CONDITIONS
To check whether two conditions are both True simultaneously, use the keyword and to
combine the two conditional tests;
if each test passes, the overall expression evaluates to True.
If either test fails or if both tests fail, the expression evaluates to False.
Example:
Example:
if conditional_test:
do something
You can put any conditional test in the first line and just about any action in the
indented block following the test.
age = int(input("Enter your age: "))
if age >= 18:
print("You are old enough to vote!")
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.
IF-ELSE STATEMENTS
Often, you’ll want to take one action when a conditional test passes and a different
action in all other cases. Python’s if-else syntax makes this possible.
if conditional_test :
do something
else:
Example: do something else
age = int(input("Enter your age: "))
if age < 4: age = int(input("Enter your age: "))
print("Your admission cost is JD0.") if age < 4:
elif age < 18: price = 0
print("Your admission cost is JD25." elif age < 18:
) price = 25
else: else:
print("Your admission cost is JD40." price = 40
) print(f"Admission cost = JD{price}.")
USING MULTIPLE ELIF BLOCKS
You can use as many elif blocks in your
code as you like. age = int(input("Enter your age: "))
For example, if the amusement park if age < 4:
price = 0
were to implement a discount for elif age < 18:
seniors, you could add one more price = 25
conditional test to the code to elif age < 65:
determine whether someone qualified price = 40
for the senior discount. else:
price = 20
Let’s say that anyone 65 or older pays print(f"Admission cost = JD{price}.")
half the regular admission, or JD20:
OMITTING THE ELSE BLOCK
Python does not require an else block at
the end of an if-elif chain. age = int(input("Enter your age: "))
The extra elif block added assigns a price if age < 4:
price = 0
of JD20 when the person is 65 or older, elif age < 18:
which is a bit clearer than the general price = 25
else block. elif age < 65:
price = 40
With this change, every block of code
elif age >= 65:
must pass a specific test in order to be price = 20
executed. print(f"Admission cost = JD{price}.")
IF STATEMENTS
EXERCISE 3