Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

ASSIGNMENTS & CONTROL FLOW

13777 SPECIAL TOPICS IN HEALTH TECHNOLOGY


DR. AHMAD ALTAMIMI
OUTLINES

ASSIGNMENT STATEMENTS BOOLEAN EXPRESSION IF STATEMENTS


ASSIGNMENT STATEMENTS
ASSIGNMENT STATEMENT FORMS
1) Basic Form:
 Example:

>>> spam = 'Spam'


 This basic form is by far the most common: binding a name (or data structure
component) to a single object.
 In fact, you could get all your work done with this basic form alone.
ASSIGNMENT STATEMENT FORMS
2) Tuple assignment (positional)
 Example:
>>> spam, jam = 'val', 'VAL'

3) List assignment (positional)


 Example:
>>> [spam, jam] = ['val', 'VAL']

 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:

>>> spam = jam = 'lunch'


 In this form, Python assigns a reference to the same object (the object farthest to the
right) to all the targets on the left. The names spam and jam are both assigned
references to the same string object, 'lunch’.
 For example, the effect is the same as if we had coded
 jam = 'lunch' followed by
 spam = jam
ASSIGNMENT STATEMENT FORMS
7) Augmented 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

 Write a separate program to accomplish this exercise.


Save the program with a filename name_cases.py.
 Use a variable to represent a person’s name, and
then print that person’s name in lowercase,
uppercase, and title case.
IF STATEMENTS
CONTROL FLOW
CONDITIONAL TEST / BOOLEAN EXPRESSION
 At the heart of every if statement is an expression that can be evaluated as True or
False and is called a conditional test or Boolean expression.
 Python uses the values True and False to decide whether the code in an if statement
should be executed.
 If a conditional test evaluates to True, Python executes the code following the if statement.
 If the test evaluates to False, Python ignores the code following the if statement.

 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:

>>> age = int(input("Enter your age: "))


Enter your age: 18
>>> nat = input("Enter your country of nationality: ")
Enter your country of nationality: Jordan
>>> age >= 18 and nat.title() == 'Jordan'
True
USING OR TO CHECK MULTIPLE CONDITIONS
 The keyword or allows you to check multiple conditions as well, but it passes when
either or both of the individual tests pass.

 Example:

>>> gender = input("Enter your gender: ")


Enter your gender: Male
>>> gender.upper() == 'MALE' or gender.lower() == 'male'
True
SIMPLE IF STATEMENTS
 The simplest kind of if statement has one test and one action:

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 >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
THE IF-ELIF-ELSE CHAIN
 Often, you’ll need to test more than two possible situations, and to evaluate these you
can use Python’s if-elif-else syntax.
 Many real-world situations involve more than two possible conditions.
 For example, consider an amusement park that charges different rates for different age
groups:
 Admission for anyone under age 4 is free.
 Admission for anyone between the ages of 4 and 17 is JD25.
 Admission for anyone age 18 or older is JD40.

 So, How can we use an if statement to determine a person’s admission rate?


THE IF-ELIF-ELSE CHAIN
 The following code tests for the age group of a person and then prints an admission
price message:

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

 Write a separate program to accomplish this exercise.


Save the program with a filename alien_colors.py.
 Imagine an alien was just shot down in a game. Create a
variable called alien_color and assign it a value of 'green',
'yellow', or 'red'.
 Write a simple-if statement to test whether the alien’s color
is green. If it is, print a message that the player just earned 5
points.
 Write one version of this program that passes the if test and
another that fails. (The version that fails will have no
output.)
EXERCISE 4

 Write a separate program to accomplish this exercise.


Save the program with a filename alien_colors2.py.
 Choose a color for an alien as you did in Exercise 3 and write
an if-else chain.
 If the alien’s color is green, print a statement that the player
just earned 5 points for shooting the alien.
 If the alien’s color isn’t green, print a statement that the
player just earned 10 points.
 Write one version of this program that runs the if block and
another that runs the else block.
EXERCISE 5

 Write a separate program to accomplish this exercise.


Save the program with a filename alien_colors3.py.
 Turn your if-else chain from Exercise 4 into an if-elif-else
chain.
 If the alien is green, print a message that the player earned 5
points.
 If the alien is yellow, print a message that the player earned 10
points.
 If the alien is red, print a message that the player earned 15
points.
 Write three versions of this program, making sure each
message is printed for the appropriate color alien.

You might also like