WEEK 2, python deel 1 Flashcards

(43 cards)

1
Q

python has two boolean VALUES

A

true and false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is special about boolean values compared to other quotes

A

they do not need quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what does != mean

A

not equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

explain the comparisons with boolean values

A

a = 10
b = 20

print (a == b) #false
print (a != b) #true
print (a < b) #true
etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

whats the difference between == and =

A

== is used for comparison and = is used for assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

explain x = 5

A

you assign 5 to x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

will an integer and string ever be equal

A

no
print (42 == ‘42’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Python has three Boolean OPERATORS

A

and, or, not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the ‘and’ operator

A

returns true only if BOTH values are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

explain the ‘or’ operator

A

returns true if at least one of the value is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

explain the ‘not’ operator

A

flips the boolean (true and false) value, not true: false, not false: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what are the python boolean expressions in order:

A

+,-,*,/ then ==,!=,<,>,<=,>= then ‘not’ then ‘and’ then ‘or’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

flow control statements

A

they have a condition and a clause, a block of code only runs if the condition is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

block

A

group of lines that belong together and are intended at the same level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

python has three main flow control types

A
  1. Conditional Statements (if, elif, else)
  2. Loops (for, while)
  3. Break and Continue Statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

if, elif and else

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

loops in flow control

A

help in executing a block of code multiple times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

FOR i in range(3):
print(“Hello”, i)

A

Hello 0
Hello 1
Hello 2

19
Q

WHILE:
count = 0
while count < 3:
print(“Count is”, count)
count += 1

A

Count is 0
Count is 1
Count is 2

20
Q

program execution

A

the process of running the instructions in the code one by one

21
Q

What happens if none of the if or elif conditions are True, and there is no else?

A

Nothing prints because both conditions were False

22
Q

Can you have more than one elif statement?

A

Yes! You can have multiple elif statements.

23
Q

Can you put an if inside another if?

A

Yes, this is called nested if statements.

24
Q

What is the difference between if-elif-else and multiple if statements?

A

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.

25
Can you have multiple elif statements?
Yes! You can have multiple elif statements, but only one condition will execute.
26
What is a while loop used for?
A while loop repeats a block of code as long as a condition is True.
27
What happens inside a while loop?
1. The condition is checked before each iteration. 2. If True, the code inside runs. 3. The condition is checked again. 4. If False, the loop exits. x = 3 while x > 0: print(x) x = x - 1
28
What happens if a while loop condition is always True?
The loop never stops (Infinite Loop).
29
What does BREAK do inside a loop?
break stops the loop immediately. Execution jumps to the first line after the loop.
30
What does CONTINUE do inside a loop?
continue skips the rest of the current iteration and jumps back to the loop condition.
31
What's the difference between BREAK and CONTINUE
break: Exits the loop immediately continue: Skips to the next iteration
32
Using = instead of == in conditions
always == because comparison
33
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
34
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.
35
What does range(2, 10, 2) generate?
Start: 2 Stop BEFORE: 10 Step by: 2 Sequence: [2, 4, 6, 8]
36
range(2)
Generates [0, 1] (because it starts from 0 and stops before 2)
37
What is a module in Python?
module is a Python file containing a collection of functions and variables that can be reused in programs
38
example of important a module
import math # Imports the math module print(math.sqrt(16)) # Uses a function from math module output: 4.0
39
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()
40
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.
41
What does sys.exit() do?
Immediately stops the program. but sys is part of the module so you must import sys first
42
43