02 - Conditional Execution and Functions
02 - Conditional Execution and Functions
LESSON 2
Effect:
1.This is a short-circuit operator, so it only evaluates the second argument if
the first one is false.
2.This is a short-circuit operator, so it only evaluates the second argument if
the first one is true.
3. not has a lower priority than non-Boolean operators, so not a == b is
interpreted as not (a == b), and a == not b is a syntax error.
Copyright © 2015 Walter Wesley All Rights Reserved
Comparison Operators
5
5 == 5
True
x=8
x == 8
True
Operator Meaning
“==” Is Equal To
“!=” Is Not Equal To
“>” Is Greater Than
“<” Is Less Than
“>=” Is Greater Than or Equal To
“<=” Is Less Than or Equal To
“!=”
x = 42
x != 120
True
“>”
x>5
True
“<”
x<5
False
“>=”
x >= 42
True
• Important!
• “==” is not the same as “=”
• “==” is the equals operator.
• “=” is the assignment operator.
• Interchanging one for the other can cause
frustratingly subtle bugs that are difficult to
identify.
yes
x>0
no print(′x is positive′)
yes
x != 0
try:
fraction = float(numerator) / denominator
print('The result of division is ' + str(fraction))
except:
print('The numerator cannot be zero!')
min('spam')
'a'
max and min are built-in and ready to use whenever they are
needed.
max and min will work with any type of sequence. It just
depends on what it means for one list item to be greater than
or less than another item.
len('Monty')
5
s = 'spam'
print(s[2])
'a'
All lists are sequences, but not all sequences are lists.
The seven sequence types are:
strings
unicode strings
lists
tuples
bytearrays
buffers
xrange objects.
Lists are constructed by means of square
brackets.
n = [5, 30, 62, 11, 42]
n[3] = 77
print(n[3])
77
s = 'spam'
print s[2]
a
s[2] = 'i'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-3728c4c73307> in <module>()
----> 1 s[2] = 'i'
ans = '42'
int(ans)
42
print(int(ans) / 2)
21
int(name)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-30-3627f499edfd> in <module>()
----> 1 int(name)
ValueError: invalid literal for int() with base 10: 'Roger the
Shrubber'
import random
for i in range(5):
num = random.random()
print(num)
0.201086994053
0.176631734158
0.305493930365
0.582269675033
0.143482947831
import random
for i in range(5):
num = random.randint(80, 85)
print(num)
81
81
84
80
85
import random
three
import random
import math
print(math)
<module 'math' (built-in)>
print(random)
<module 'random' from
'C:\Users\SDCCD_User\AppData\Local\Enthought\Canopy\App\appdata\canopy-
1.5.2.2785.win-x86_64\lib\random.pyc'>
import math
signal_power = 50.1
noise_power = 1.7
decibels = 10 * math.log10(ratio)
print(decibels)
14.6938880449
Notice how the variable names use an underscore to separate multiple words with the
name (e.g., signal_power).
degrees = 30
radians = degrees / 360.0 * 2 * math.pi
sine_value = math.sin(radians)
print(sine_value)
0.5
def function_name():
statement1
statement2
.
.
.
def dead_parrot():
print('''He's not pinin'!''')
print('''He's passed on!''')
print('''This parrot is no more!''')
print('''He has ceased to be!''')
Notice the use of the triple single quotes (''') that allow
for the embedding of single quotes within the string.
def dead_parrot():
print(dead_parrot)
<function dead_parrot at 0x0000000009AEB898>
You can verify the type of the variable with the type
function.
print(type(dead_parrot))
<type 'function'>
dead_parrot()
He's not pinin'!
He's passed on!
This parrot is no more!
He has ceased to be!
dead_parrot()
He's not pinin'!
He's passed on!
This parrot is no more!
He has ceased to be!
def dead_parrot_refrain():
dead_parrot()
dead_parrot()
dead_parrot_refrain()
He's not pinin'!
He's passed on!
This parrot is no more!
He has ceased to be!
He's not pinin'!
He's passed on!
This parrot is no more!
He has ceased to be!
def echo(something_to_say):
print(something_to_say)
print(something_to_say)
print(something_to_say)
echo('The Larch!')
The Larch!
The Larch!
The Larch!
def half(some_number):
return some_number / 2.0
print(half(7))
3.5
print(average_three_numbers(3, 5, 7))
4.33333333333
print(total)
---------------------------------------------------------------------------
NameError Traceback (most recent call
last)
<ipython-input-90-bc4e21da9abf> in <module>()
----> 1 print(total)