forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
148 lines (118 loc) · 4.47 KB
/
loops.py
File metadata and controls
148 lines (118 loc) · 4.47 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
""" How to do loops in Python """
def for_loop_example():
""" How to use a simple for-loop to go over a list"""
print "For Loop Example"
mylist = [2, 3, 4, 5, 9, 10]
for x in mylist:
print x
print "\n"
def in_xrange_example(start_num, end_num=10):
""" How to use xrange (can pass in up to two arguments) """
#print start_num (passed) up to end_num (default 10, prints up to 9)
print "xrange example given", start_num, "and", end_num
for x in xrange(start_num, end_num):
print x
print "\n"
# Can also loop through a list in reverse
for index in reversed(xrange(1, 10, 2)):
print index # 9 7 5 3 1
def while_loop_example(number):
""" How to use while loops """
print "While Loop example from 0 to", number
count = 0
while count < number:
print count
count += 1
print "\n"
def break_example():
""" How to use break to exit code """
print "Break Example"
count = 0
while True:
print count
count += 1
if count > 5:
break
print "\n"
def continue_example():
""" How to use continue to ignore specific cases """
print "Continue Example"
for x in xrange(10):
# Check if x is even
if (x % 2 == 0):
continue
print x
def iteritems_example():
""" Can loop through dicts (key, values) using iteritems """
# Loop through a dict using iteritems() method
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for index, values in knights.iteritems():
print index, values
# gallahad the pure
# robin the brave
def enumerate_example():
""" How to enumerate across a list to get index and value """
print "Enumerate Example"
for index, value in enumerate(['tic', 'tac', 'toe']):
print index, value
def zip_example():
""" Go through two lists of equal size with zip """
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for qst, ans in zip(questions, answers):
print "What is your {0}? It is {1}.".format(qst, ans)
def list_comprehension_example():
""" A list comprehension is a quick way to generate a list and has the
following format: result = transform, iteration, filter.
"""
simple_range = [i for i in range(5)] # no transform or filter, only iteration
complex_range = [i * i for i in range(5) if i % 2 == 0] #transform, iteration, filter
print simple_range # [0, 1, 2, 3, 4]
print complex_range # [0, 4, 16]
def set_example():
"""
A set is an unordered collection of distinct hashable objects.
Common uses include intersection, union, difference, remove dupes
"""
# To get unique list of items, you can use set()
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in set(basket):
print f # orange, pear, apple, banana
# Can also use sets to compare each other
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
# Get value(s) in both sets
same_set = set(a).intersection(b) # set([5]), Pythonic way
#same_set = set(a) & set(b) # same as above, but not Pythonic, don't use
print type(same_set) # <type 'set'>
print same_set # set([5])
for value in same_set: # Get values out of a set
print value # 5
different_set = set(a).difference(b)
for value in different_set: # Get values out of a set
print value # 1, 2, 3, 4
union_set = set(a).union(b)
for value in union_set: # Get values out of a set
print value # 1,2,3,4,5,6,7,8,9 # Note only one 5 value returns
issubset_set = set(a).issubset(b)
print issubset_set # False; every element of a is not in set b
issuperset_set = set(a).issuperset(b)
print issuperset_set # False; every element of b is not in set a
if __name__ == '__main__':
"""Goes through examples of all the loop variations"""
for_loop_example()
in_xrange_example(4) # prints 4,5,6,7,8,9
in_xrange_example(4, 7) # prints 4,5,6
while_loop_example(5) # prints 0,1,2,3,4
break_example() # prints 0,1,2,3,4
continue_example() # prints 1,3,5,7
enumerate_example()
# prints 0 tic
# 1 tac
# 2 toe
zip_example()
# What is your name? It is lancelot
# What is your quest? It is the holy grail
# What is your favorite color? It is blue
list_comprehension_example() # how to do list comprehensions
set_example() # how to use sets