Don't like this style? Click here to change it! blue.css
Our goal was to write a function to display the first n numbers, in binary. Here is my solution:
So I know that we are facing some difficult tasks everyday in class: just jumping, in embracing the discomfort, and flailing around until things make sense. (If I haven't told you my dark room analogy for learning technical material ask me to when you read this line.)
In light of that, here are some resources you might find very helpful in trying to find your way around these magic words:
Now in the last class session we didn't finish the notes. We were up to
for x in [1,2,3]:, so let's
head there now and continue.
When you hear the word Boolean you should think of a "True or False" expression. The word actually is derived from George Boole:
Take a look at this insightful "Boolean" thought:
The basic words you should know are:
In Math we consider a statement as something with a truth value of
False. Then we combine the statements using
or to make bigger
statements which are still
What is the truth value of the following statement:It is Monday.
What is the truth value of this statement:It is Monday OR two is less than one.
What is the truth value of this statement:It is Monday AND two is less than one.
In Python we can build statements which evaluate to
False. Either by using the
False or by asking it true/false questions using
x == y(Hey Python, is
xthe same as
x > y(is
x < y(is
x <= y(less than or equal?)
x >= y(greater than or equal?)
x != y(is
xnot equal to
The result will be
False, and we can mix it all up with
Boolean Task 2: Decide (before hitting run) what the following nine print statements will display. Then run and confirm:
Boolean Task 3: Decide (before hitting run) what the following compound statement will display. Then run and confirm:
The main uses for booleans are to decide when to run blocks of code (and how often to run them). Here is a simple example:
If Task 4: Change the value of
number to trigger the "negative"
message. What is the logical flaw in this program?
When you want to have many possible blocks you can use
elif which is short for
else if (doesn't work in Python but does in many other languages). Here is an example:
elif Task 5: Change the value of number (several times) to trigger each separate code block (sorry for my jokes). Change the structure so that the code does not check for negatives.
There is even an "inline" if which works like this.
value1 if condition else value2
Here is an example: (I use the "modulo" operator
a % m which gives the remainder when
a is divided by
Let's make a die rolling function, an
is_even function, an inline if,
for loop to roll a die 10 times:
Understanding Task 6: Read through that code and understand what is happening. Change the functions and their names so that we generate a random number from -10 to 10 and instead of even and odd we detect negative and non-negative.
DAILY CHALLENGE 3: Build a function which consumes a positive integer
n and adds all of the odd positive numbers less than
n. For example
myfunc(6) will return 9 and myfunc(5) will return 4.