-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path08_if.py
More file actions
65 lines (53 loc) · 1.53 KB
/
08_if.py
File metadata and controls
65 lines (53 loc) · 1.53 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
x = 1
if x > 0:
# Note the indentation: that tells Python to execute the indented "block" of code if the condition is true
print 'x is greater than 0'
print 'No longer in the block, so this will always be executed'
if x == 0:
print 'x equals 0'
else:
print 'x does not equal 0'
# Python will not let you write:
# if x = 0:
# == is comparison for equality
# = is assignment
if x == 0:
print 'x equals 0'
elif x == 1:
print 'x equals 1'
else:
print 'x does not equal 1 or 0'
if True:
print 'This will always execute'
y = x > 0
print type(x) # This will be?
# Comparison operators
print 'x > 0', x > 0
print 'x < 0', x < 0
print 'x >= 0', x >= 0 # Greater than or equals
print 'x <= 0', x <= 0 # Less than or equals
print 'x == 0', x == 0 # Equals
print 'x != 0', x != 0 # Not equals
print 'not (x == 0)', not (x == 0) # Negation
# Test if a list or tuple contains an element
a_list = ['x', 'y']
if 'x' in a_list:
print 'x is in the list'
# Test if a dict contains a key
a_dict = {'key1': 'value1', 'key2': 'value2'}
if 'key1' in a_dict:
print 'key1 is in the dict'
# Same as
if 'key1' in a_dict.keys():
print 'key1 is in the dict'
# (The first form is more common and more efficient.)
if 'value1' in a_dict:
print "This won't work"
if 'value1' in a_dict.values():
print 'This will'
# Be careful with types
if '1' == 1:
print "It looks true, but it isn't"
else:
print "A string will never equal an integer -- one of the disadvantages of having types."
# We'll see how to deal with this later.