WEEK 2, python deel 1 Flashcards
python has two boolean VALUES
true and false
what is special about boolean values compared to other quotes
they do not need quotes
what does != mean
not equal to
explain the comparisons with boolean values
a = 10
b = 20
print (a == b) #false
print (a != b) #true
print (a < b) #true
etc
whats the difference between == and =
== is used for comparison and = is used for assignment
explain x = 5
you assign 5 to x
will an integer and string ever be equal
no
print (42 == ‘42’)
Python has three Boolean OPERATORS
and, or, not
Explain the ‘and’ operator
returns true only if BOTH values are true
explain the ‘or’ operator
returns true if at least one of the value is true
explain the ‘not’ operator
flips the boolean (true and false) value, not true: false, not false: true
what are the python boolean expressions in order:
+,-,*,/ then ==,!=,<,>,<=,>= then ‘not’ then ‘and’ then ‘or’
flow control statements
they have a condition and a clause, a block of code only runs if the condition is true
block
group of lines that belong together and are intended at the same level.
python has three main flow control types
- Conditional Statements (if, elif, else)
- Loops (for, while)
- Break and Continue Statements
if, elif and else
if: checks the first condition
elif: checks the next condition only if the first condition is false
else: runs only if none of the if or elif conditions are true
loops in flow control
help in executing a block of code multiple times
FOR i in range(3):
print(“Hello”, i)
Hello 0
Hello 1
Hello 2
WHILE:
count = 0
while count < 3:
print(“Count is”, count)
count += 1
Count is 0
Count is 1
Count is 2
program execution
the process of running the instructions in the code one by one
What happens if none of the if or elif conditions are True, and there is no else?
Nothing prints because both conditions were False
Can you have more than one elif statement?
Yes! You can have multiple elif statements.
Can you put an if inside another if?
Yes, this is called nested if statements.
What is the difference between if-elif-else and multiple if statements?
if-elif-else → Only one block runs (once a condition is True, it stops checking).
Multiple if → Each if is checked separately, even if one is already True.
Can you have multiple elif statements?
Yes! You can have multiple elif statements, but only one condition will execute.
What is a while loop used for?
A while loop repeats a block of code as long as a condition is True.
What happens inside a while loop?
- The condition is checked before each iteration.
- If True, the code inside runs.
- The condition is checked again.
- If False, the loop exits.
x = 3
while x > 0:
print(x)
x = x - 1
What happens if a while loop condition is always True?
The loop never stops (Infinite Loop).
What does BREAK do inside a loop?
break stops the loop immediately.
Execution jumps to the first line after the loop.
What does CONTINUE do inside a loop?
continue skips the rest of the current iteration and jumps back to the loop condition.
What’s the difference between BREAK and CONTINUE
break: Exits the loop immediately
continue: Skips to the next iteration
Using = instead of == in conditions
always == because comparison
Which values are considered True or False in conditions?
falsy values: 0, 0.0, none, false, [], {}
truth values: non-zero numbers, non-empty lists/dictionairies
What does while not name: do in this code?
name = ‘’
while not name:
print(‘Enter your name:’)
name = input()
print(‘Welcome,’, name)
The loop keeps asking for input until the user enters a name (not empty).
If name is an empty string (‘’ → Falsy), the loop continues.
What does range(2, 10, 2) generate?
Start: 2
Stop BEFORE: 10
Step by: 2
Sequence: [2, 4, 6, 8]
range(2)
Generates [0, 1] (because it starts from 0 and stops before 2)
What is a module in Python?
module is a Python file containing a collection of functions and variables that can be reused in programs
example of important a module
import math # Imports the math module
print(math.sqrt(16)) # Uses a function from math module
output: 4.0
What is the difference between built-in functions and functions from modules?
Built-in functions (available by default) → print(), input(), len()
Modules (need to be imported) → math.sqrt(), random.randint(), sys.exit()
What does random.randint(1, 10) do?
Returns a random integer between 1 and 10 (inclusive).
Every time you run it, you get a different number.
What does sys.exit() do?
Immediately stops the program.
but sys is part of the module so you must import sys first