Вы находитесь на странице: 1из 22

Control flow statements:

The if Statement

if boolean-expression1:
suite1
elif boolean_expression2:
suite2
elif Boolean_expressionN:
suiteN
else:
else_suite
Example:

if lines < 1000:


print(“small”)
elif lines <10000:
print(“medium)
else:
print(“large”)
if, elif, and else

>>> disaster = True


>>> if disaster:
>>> print(“Woe”)
>>> else:
>>> print(“Whee”)
Output: Woe
Example
>>> furry = True
>>> small = True
>>> if furry:
>>> if small:
>>> print(“It is a cat”)
>>> else:
>>> print(“ It is a bear”)
>>> else:
>>> if small:
>>> print(“It is a skink”)
>>> else:
>>> print(“It is a human. Or a hairless bear”);
Output: It is a cat
Example
>>> color = ‘puce’
>>> if color == ‘red’:
>>> print(“It is a tomato”)
>>> elif color == ‘green’:
>>> print(“It is a green pepper”)
>>> elif color == ‘bee purple’:
print(“I don’t know what it is, but only bees can see it”)
>>> else:
print(“I have never heard of the color”, color)

Output: I have never heard of the color puce


>>> x, y = 8, 8
>>> if(x < y):
>>> st= "x is less than y"
>>> elif (x = = y):
>>> st= "x is same as y"
>>> else:
>>> st="x is greater than y"
>>> print(st)
Short Hand of If ... Else
>>> a=10
>>> b=5
>>> if a > b: print("a is greater than b")
Output: a is greater than b
>>> print("A") if a > b else print("B")
Output: A
>>> print(“B") if a < b else print(“Equal") if a == b else print(“A")
Output: A
>>> x,y = 10,8
>>> st = "x is less than y" if (x < y) else "x is greater than or equal to y"
>>> print(st)
The while Statement:
while boolean_expression:
suite
Example: Print i as long as i is less than 6
>>> i = 1
>>> while i < 6:
>>> print(i)
>>> i += 1
Output:
1
2
3
4
5
The break Statement

>>> i = 1
>>> while i < 6:
>>> print(i)
>>> if i == 3:
>>> break
>>> i += 1
Output:
1
2
3
The continue Statement
>>> i = 0
>>> while i < 6:
>>> i += 1
>>> if i == 3:
>>> continue
>>> print(i)
Output:
1
2
4
5
6
The for… in Statement:
for variable in iterable:
suite
>>> for country in [“Denmark”, “Finland”,
“Norway”, “Sweden”]:
>>> print(country)
Cont.
>>> countries = [“Denmark”, “Finland”, “Norway”,
“Sweden”]
>>> for country in countries:
>>> print(country)
Cont.
>>> for letter in
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”:
>>> if letter in “AEIOU”:
>>> print(letter, “is a vowel”)
>>> else:
>>> print(letter, “is a consonant”)
Read an integer. If it is odd, then print its square

>>> while True:


>>> value = input(“Enter an integer, q to quit”)
>>> if value == ‘q’:
>>> break;
>>> number = int(value)
>>> if number % 2 == 0;
>>> continue
>>> print(“\n The square of the number”, number, “ is: ” ,
(number*number))
Display the elements of a list, string, and directory
using for loop
>>> alist = [1, 2, 3, 4, 5]
>>> for N in alist;
>>> print(N)
Output: 12345
>>> word = ‘cat’
>>> for c in word;
print(c)
Output: cat
>>> accusation = {‘room’ : ‘ballroom’, ‘weapon’ : ‘lead pipe’,
‘person’ : ‘Col. Mustard’}
>>> for key in accusation:
>>> print(key)
Output: cat
room
weapon
person
>>> accusation = {‘room’ : ‘ballroom’, ‘weapon’ : ‘lead pipe’,
‘person’ : ‘Col. Mustard’)

Display the values of the above dictionary


>>> for value in accusation.values():
>>> print(value)

Output:
Ballroom
Lead pipe
Col. Mustard
>>> accusation = {‘room’ : ‘ballroom’, ‘weapon’ : ‘lead pipe’,
‘person’ : ‘Col. Mustard’)

Return both keys and their respective values


>>> for item in accusation.items();
>>> print(item)

Output:
(‘room’, ‘Ballroom’)
(‘weapon’, ‘Lead pipe’)
(‘person’, ‘Col. Mustard’)
Assigning the keys and the values of a tuple in a
single step
>>> for key, value in accusation.items()
>>> print(“key is ”, key, “ and its values are: ”, value)
Iterate multiple sequences in parallel with zip()
>>> days = [‘Monday’, ‘Tuesday’, ‘Wednesday’]
>>> fruits = [‘banana’, ‘orange’, ‘peach’]
>>> drinks = [‘coffee’, ‘tea’, ‘beer’]
>>> desserts = [‘tiramisu’, ‘ice cream’, ‘pie’, ‘pudding’]
>>> for day, fruit, drink, dessert in zip(days, fruits, drinks, desserts):
>>> print(day, “: drink ”, drink, “ eat ”, fruit, “ enjoy ”, dessert)

Output:
Monday: drink coffee eat banana enjoy tiramisu
Tuesday: drink tea eat orange enjoy ice cream
Wednesday: drink beer eat peach enjoy pie
*Pudding is not displayed
Generate number of sequences with range(start, stop, step)
By default the start is 0
by default the step is 1
>>> for x in range(0, 3):
>>> print(x)
Output:
0
1
2
3
Making a range from 2 down to 0:
>>> for x in range(2, -1, -1)
>>> print(x)
Output:
2
1
0
Cont.
>>> for x in list(range(2, -1, -1))
>>> print(x)
Output:
[2, 1, 0]

Get even numbers from 0 to 10 and then store them into a


list

>>> for x in list(range(0, 10, 2))


print(x)
Output:
[0, 2, 4, 6, 8, 10]

Вам также может понравиться