CS150 - Fall 2011 - Class 7
admin
- CS lunch on Tuesday at Ross Dining Hall
- Keep up with the reading
- Keep up with the practice problems
- Use the notes and the examples from class on the assignment
booleans
- last time we saw some of string methods gave us a True/False answer
- Python has another type called bool
- bool can only take the value True or False
- bool's generally result from asking T/F question
- Does the string start with a particular string?
- Does the string end with a particular string?
- Is the string upper case?
- Is the string lower case?
- What other questions might we want to ask on data we've seen so far (e.g. numbers)?
- comparison operators
- == (equal)
- notice that '=' is the assignment operator while '==' asks whether two things are equal
- != (not equal)
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)
- Using the comparison operators
>>> 10 < 0
False
>>> 11 >= 11
True
>>> 11 > 11.0
False
>>> 11 >= 10.9
True
>>> 10 == 10.1
False
>>> "test" == "test"
True
>>> "test" == "testing"[0:4]
True
>>> "test" == "TEST"
False
>>> 10 != 10
False
>>> 10 != 11
True
>>> "banana" < "apple"
False
combining booleans
- we can also combine boolean expressions to make more complicated expressions
- what kind of connectors might we want?
- and
<bool expression> and <bool expression>
- only returns True if both expressions are True
- otherwise, it returns false
>>> x = 5
>>> x < 10 and x > 0
True
>>> x = -1
>>> x < 10 and x > 0
False
- Truth table
- a truth table gives you a mapping from input to output for boolean functions
A B | A and B
-------------
T T | T
T F | F
F T | F
F F | F
- or
<bool expression> or <bool expression>
- returns True if either expression is True
- False only if they are both False
>>> x = 5
>>> x < 10 or x > 0
True
>>> x = -1
>>> x < 10 or x > 0
True
- Truth table
A B | A or B
-------------
T T | T
T F | T
F T | T
F F | F
- not
not <bool expression>
- negates the expression
- if it's True returns False
- if it's False returns True
>>> not 5 == 5
- Truth table
A | not B
-------------
T | F
F | T
if statement
- the key use of bool's is to make decisions based on the answers
- the "if" statement is another statement (like for loops, etc) that allow us to control the flow of the program based on the result of a boolean expression
if <bool expression>:
# do these statements if the bool is True
statement1
statement2
statement3
- the if statement is called a "control" statement in that it changes how the program flows
- as the program runs, it evaluates the boolean expression
- if it is true, it evaluates all of the statements under the "if" block and then continue on
- it will execute statement1, statement2 and then statement3
- otherwise (i.e. the boolean was false), it will skip these statements and continue on
- it will just execute statement3
- look at simple_if function in
conditionals.py code
- if-else: sometimes we'd also like to do something if the bool is False, in this case, we can include an "else"
if <bool expression>:
# do these statements if the bool is True
statement1
statement2
else:
# do these statements if the bool is False
statement3
statement4
statement5
- if the boolean expression is true
- execute statement1, statement2 then statement 5
- if the boolean expression is false
- execute statement3, statement4 then statement 5
run stupid_name function from
conditionals.py code
- raw_input
- takes a string as a parameter
- it displays the string to the user
- then waits for the user to enter some text. The program doesn't continue until the user hits enter/return
- whatever the user typed will be returned by the raw_input function as a string
- if you want a number you need to use int(...) or float(...)
- first prompts the user for their name
- depending on the input the output of the program partially differs
- if statements allow us to control the flow of the program
look at stupid_name function from
conditionals.py code
- we first use the raw_input function to get the user's name
- raw_input returns the text the user entered
- in the variable "name" will be whatever the users entered
- name == "Dave" checks whether the entered name is "Dave"
- if statement directs the program's behavior depending on the answer
- finally, regardless of the name, we print out "Nice to meet you..."
calculating pi
- if you didn't know pi, how could you calculate it?
- say we draw a 2 by 2 square enclosing the circle
- what is the area of the square?
- 4
- what is the are of the circle?
- pi * r^2, which in this case, is just pi
- let's say I pick a point randomly in the 2 by 2 square. What is the probability it will be inside the circle?
area_of_circle
---------------
area of the square
that is what proportion of the squares area is covered by the circle
- if we plug this into our area equations above, we get that this value is equal to pi/4
- if we could figure out this probability, we could multiply it times 4 and get pi
- how can we get this probability?
- randomly put points down in the square
- count how many inside the square
- use this to estimate the probability
look at
pi-calculator.py code
- generates num_samples random points and counts how many are within the radius of the circle
- What are some things you notice with the code:
- a new way of importing modules
- previously, we said from <module> import * (or some function name)
- this imported the functions into the current namespace
- another way is to just say
import <module>
- the difference is that they do not get brought into the current namespace
- to call them you need to name the module followed by a dot before it
math.sqrt(10)
random.unifrom(0,1)
- why would we do this?
- avoid confusion if multiple modules have the same name
- in general, this is a better approach
- however, it can be convenient to import the other way in some situations. Use your judgement
- random.uniform
- similar to randint, but now gives us a random float between the values (i.e. samples from a uniform distribution)
- what does math.sqrt(x*x + y*y) do?
- calculate the distance from the origin to that point
- if it's less than 1, than we're inside the circle
- We can run it to get an approximation for pi
>>> calculate_pi(10)
3.6
>>> calculate_pi(10)
2.4
>>> calculate_pi(10)
3.6
>>> calculate_pi(100)
3.24
>>> calculate_pi(100)
3.0
>>> calculate_pi(1000)
3.124
>>> calculate_pi(1000)
3.116
>>> calculate_pi(10000)
3.1288
>>> calculate_pi(10000)
3.118
>>> calculate_pi(10000)
3.126
>>> calculate_pi(100000)
3.145
>>> calculate_pi(100000)
3.1438
>>> calculate_pi(1000000)
3.144192
- This technique is called monte carlo sampling
- notice that it's an approximation
- we get different values depending on our random points
- the more points we sample, the better our approximation
- FYI, there are better ways of calculating pi :)
run forecast function in
conditionals.py code
- takes two parameters
- temperature
- amount of rain
- depending on what these values are, gives us different answers
- to start with, let's just look at the temperature. We want to define 4 temperature bands:
- > 80 => "hot"
- 71 - 80 => "warm"
- 51 - 70 => "cool"
- <= 50 => "cold"
- say we have a variable called temperature with the temperature, how would we write the if statements for this?
if temperature > 80:
temp = "hot"
if temperature > 70 and temperature <= 80:
temp = "warm"
...
- if we know the temperature is above 80, do we need to check any of the other ones?
- no!
- Python has another statement that allows us to represent this type of expression: elif
if <bool expression>:
statement1
elif <bool expression>:
statement2
... # we can have as many elif blocks as we want
else:
statement3
statement4