- Dataype: Boolean
- Comparison Operators
- Logical Operators
- Conditionals: if/else
- Random Module
- Flowcharts
- Lab 03: Grading
A boolean can represent one of two values: True
or False
.
a = True
b = False
- Comparison Operators Overview (w3schools)
Operator Description Example == Equal x == y != Not Equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y
x = 3
y = 4
x == 3 # True
x == y # False
x > y # False
x < y # True
x != y # True
x > 3 # False
x >= 3 # True
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are True | x < 5 and x < 10 |
or | Return True if one statement is True | x < 4 or x > 6 |
not | Reverse the result, returns fals if True | not(x < 5) |
and | True | False |
---|---|---|
True | True | False |
False | False | False |
or | True | False |
---|---|---|
True | True | True |
False | True | False |
We can use condtional statements to run code using our comparison operators.
This can be achieved by using the if
keyword.
x = 4
y = 10
if x < y:
print("x is less than y")
Notice how the print
statement is indented? Python relies on indentation to define blocks of code.
We also have access to the keyword elif
. This is Python's way of saying, "if the previous condition was not True, then try this one."
x = 4
y = 10
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
Lastly we have else
. This will catch any case that was not caught by the preceding conditions.
x = 4
y = 10
if x > y:
print("x is greater than y")
elif x == y :
print("x is equal to y")
else:
print("x is less than y")
Notice there is no conditional statement on else
.
Modules are a Python object that are sources of related code; think of if as a library of information. A code library, if you will! It is a file containing a set of functions you want to include in your application. (For a list of all modules, you can check out the offical Python docs).
For this lesson, we will be looking at the random module. And specifically, we will be looking at the randint()
function of the random module: random.randint(). Please see the example below:
# include the random module in our file
import random
# randint(bottom, top)
# generate a random integer between
# the bottom number and the top number
random_number = random.randint(1, 100)
print(f'Random number: {random_number}')
Output
Random number: 56
Random number: 13
Random number: 98
Flow Charts are a great tool for planning the logical flow of a program. Click the link to read more.