import sandbox_widget13 Values, operators, logic
This chapter is about values and variables, the two most central concepts in programming.
Values
You have already met the string value we used to represent text. E.g. "Hello world" or 'Hello world'. To work with numeric values, you just write the numbers. Internally, Python distinguishes between two different types of numeric values, integers (int) and floating point numbers (float), but Python automatically converts between, so no need to worry about that.
%%sandbox
print(4)%%sandbox
print(3.15)Operators
%%sandbox
print("Four times two is", 4 * 2)Run the code above. Notice how you can print more than one thing at a time if you put commas between the values you want to print? We can group computations using parentheses, just like in normal math. Try this:
%%sandbox
print("10 / (2 + 3) is", 10 / (2 + 3))
print("(10 / 2) + 3 is", (10 / 2) + 3)In addition to the regular math operators, there are a few extra operators that we call comparison operators because they are used to compare two values, e.g., two numbers.
| Operator | Operation |
|---|---|
< |
less-than |
> |
greater-than |
<= |
less-than-or-equal |
>= |
greater-than-or-equal |
== |
equal |
!= |
not equal |
Try this:
%%sandbox
print("Is 5 greater than -2?", 5 > -2)
print("Is 5 greater or equal to -2?", 5 >= -2)
print("Is 5 less or equal to -2?", 5 <= -2)
print("Is 5 less than 7 - 2?", 5 < 7 - 2)
print("Is 5 equal to 7 - 2?", 5 == 7 - 2)As you may have noticed, running this code and comparing things using these operators, we always produce either True or False. E.g., the following
%%sandbox
print(5 < 7)prints the value True because 5 is smaller than 7. True and False are special values in Python that we can use (and print if we like) just like any other Python value:
%%sandbox
print(True)
print(False)Exercise 13-1
Try to read and run the code below. Compare each line to what is printed when you run the code. Make sure you understand why.
%%sandbox
print("I have", 25 + 30 / 6, "of something")
print("I have", 100 - 25 * 3 % 4, "of something else")
print("Is it true that 3 + 2 < 5 - 7?")
print(3 + 2.1 < 5.4 - 7)
print("3 + 2.1 is", 3 + 2.1)
print("5.4 - 7 is", 5.4 - 7)
print("Oh, that's why it's False.")Exercise 13-2
An additional comparison operator even tests if something is a part of something else. That operator is called in. One use of it is to test if one string is part of another string. Try this to figure out how it works:
%%sandbox
print("Hell" in "Hello world")
print("Hello world" in "Hello world")
print("Hello world" in "Hell")
print("lo wo" in "Hello world")
print("Artichoke" in "Hello world")Exercise 13-3
Say the supermarket has chocolate bars for 7 kr. Write a small Python program (in a file called chocolate.py) that prints how many chocolate bars you can get for your 30 kr. For example, it could output something like this:
I can buy 4.285714285714286 chocolate bars!
Exercise 13-4
We mentioned a special operator called modulo. Google it if you do not remember what it does. How about integer division. Explain both to a fellow student (or to yourself out loud).
Then ask the assistant to explain modulo and integer division to a first-week student, and compare its explanation to yours. Where the two differ, settle it by writing the calculation in Python and running it: 17 % 5 and 17 // 5 take three seconds to check, and no amount of confident prose beats them.
Exercise 13-5
You obviously cannot go buy 4.3 chocolate bars in a store. You will have to settle for 4. Can you change the program you made in Section 13.0.0.3 to print the number of bars you can buy and the change you then have left? Use the modulo and integer division operators. Something like:
I can buy 4 chocolate bars, leaving me with 2 kr in change.
Exercise 13-6
What happens if you try to run the following?
print( 1 / 0 )Did you get an error? What kind of error? Why do you think you get that error? Do you think it makes sense?
Exercise 13-7
You probably know the Pythagorean theorem for computing a right-angled triangle’s hypotenuse (the longest side). The Pythagorean theorem looks like this: \(a^2 + b^2 = c^2\). Here \(c\) is the length of the hypotenuse, and \(a\) and \(b\) are the lengths of the triangle’s two legs. So if we have a triangle where \(a = 5\) and \(b = 2\) and we want to find \(c^2\) we can do this in Python:
%%sandbox
print("The squared length of the hypotenuse is:", 5**2 + 2**2)Exercise 13-8
However, we are rarely interested in the squared length of the hypotenuse. Can you modify the code you read in Section 13.0.0.7 so you compute \(c\) instead of \(c^2\)? Taking the square root of a number is the same as number to the power of 0.5, so the square root of \(x\) is \(x^{0.5}\). Do you know of a Python operator that does exponentiation?
Logic
Now you know how to use the comparison operations to produce a True or False value. There are three additional operators that let you express more elaborate “True/False” statements than with the comparison operators alone. These are the logical operators: and, or and not.
Exercise 13-9
Before you run the code below, through each line see if you can figure out what each line does. Then, run it to see if you were right.
%%sandbox
print(2 < 3)
print(10 < 12)
print(8 > 100)
print(2 < 3 and 10 < 12)
print(8 > 100 and 2 < 3)
print(8 > 100 or 2 < 3)
print(not 8 > 100 and 2 < 3)
print(not 8 > 100 and not 2 > 3)Did it do what you expected?
Exercise 13-10
When exposed to the operators and, or and not, some values are considered true and others are considered false. What happens when you put not in front of something that is considered true or false? Decide what you think and why before you run the code.
%%sandbox
print(not True)
print(not False)
print(not 0)
print(not -4)
print(not 0.0000000)
print(not 3.14159265359)
print(not "apple")
print(not "")From the code above, try to find out which values Python considers true and which it considers false. Can you come up with a rule?
Exercise 13-11
The logical operator and takes two values (the one to the left of the operator and the one to the right) and figures out whether both the left and the right expression are true. It boils down to this:
| Left expression | Right expression | Result |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
Write some code to confirm that the table above is correct using Python. For example, to test the first case, do this:
%%sandbox
print(True and True)Exercise 13-12
Python will only do the necessary work to determine if a logical expression is true. That means that if the value left of and is considered false by Python, then there is no reason to look at the right value since it is already established that they are not both considered true. In this case the expression reduces to the left value. I.e. False and True reduces to False.
If Python considers the value left of and true, then It needs to look at the right value, too, to establish if they are both considered true. In this case, the expression reduces to the value on the right. I.e., True and False reduces to False.
A rule of thumb is that the whole expression reduces to the last value that Python needs to consider to decide if the whole expression is true or false. Use that rationale to explain how the two last combinations in Section 13.0.0.11 are evaluated.
Exercise 13-13
Like the and operator, the or operator also takes two values. However, the or operator determines whether one of the two values is true. Thus, the or operator boils down to this:
| Left expression | Right expression | Result |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
Write some code using Python to confirm that the table above is correct. For example, to test the first case, do this:
%%sandbox
print(True or True)Exercise 13-14
As with the and operator, Python will not do any more work than absolutely necessary when evaluating an expression with ‘or’. So if the value left of or is considered true by Python, then there is no reason to look at the right value since it is already established that at least one of them is considered true. In this case the expression reduces to the left value. I.e. True or False reduces to True.
If the value left of or is considered false by Python, then Python still needs to look at the right value to establish if at least one of them is considered true. In this case, the expression reduces to the right value. I.e., False or True reduces to True.
Again, the whole expression reduces to the last value that Python needs to consider to decide if the whole expression is true or false. Use that same rationale to explain to yourself how the two last combinations in Section 13.0.0.13 are evaluated.
Exercise 13-15
Remember what you learned in Section 13.0.0.10 about which values are considered true and which are considered false. Combine that with what you learned in Section 13.0.0.11 and Section 13.0.0.13 about what logical expressions reduce to and see if you can figure out what is printed below and why. Use the rule-of-thumb from Section 13.0.0.14. Decide what you think before you run the code and try it out.
%%sandbox
print(True and 4)
print(0 and 7)
print(-27 and 0.5)
print(42 and 0)
print("apple" and "orange")
print("apple" and "")
print(42 or 0)
print("apple" and "")
print("apple" or "")If you were surprised by what was printed, maybe go back and look at Section 13.0.0.11 and Section 13.0.0.13 again.
Exercise 13-16
Recall the in operator from Section 13.0.0.2? There is also an operator called not in. I guess you can imagine what that tests. Try it out.
By now, you probably feel the first signs of brain overload. If you do not take breaks, your brain may overheat and explode - we have seen that happen. One of the nice things about the brain is that it works when you rest. Archiving and understanding a lot of new information takes time, and force-feeding your brain will not help. The last part of this chapter is very important so now might be a good time for a good long break.