WEEK 2, python deel 1 Flashcards
(43 cards)
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.