-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrol_flow.py
More file actions
39 lines (32 loc) · 894 Bytes
/
control_flow.py
File metadata and controls
39 lines (32 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# if statement
# x = int(raw_input("Please enter an integer: "))
x = 10
if x < 0:
x = 0
print("Negative value changed to zero")
elif x == 0:
print(0)
else:
print("positive value: %d" % x)
print ("-----------------")
# for statement
words = ['cat', 'dog', "bird"]
for w in words:
print(w, len(w))
print ("-----------------")
# range(start, stop, step) or range(stop):
# iterate over a sequence of number from "start" to "stop-1" with "step"
# range() returns a LIST object
# xrange() returns a xrange() object [yielding technique], and it is more efficient
print(range(0, 10, 2))
print(xrange(10))
song = ['Mary', 'has', 'a', 'little', 'lamb']
for i in range(len(song)):
print(i, song[i])
print ("-----------------")
# break and continue Statements
for i in range(10):
if i >= 5:
break
else:
print i