Flow control Flashcards

1
Q

What is “Flow control”

A

Flow control statements can decide which Python instructions to execute under which conditions.

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

What is the Boolean Value?

A

True or False and they always start with a capital T and F

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

When do you get error while using boolean values?

A

➊. If you don’t use the proper case

➋ or you try to use True and False for variable names

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

what do comparison operators do?

A

compare two values and evaluate down to a single Boolean value.

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

What is the other name of comparison operators?

A

relational operators,

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

What is the comparison operators for

“Equal to”

A

==

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

What is the comparison operators for

Not equal to

A

!=

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

What is the comparison operators for

Less than

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

What is the comparison operators for

Greater than

A

>

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

What is the comparison operators for

Less than or equal to

A

<=

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

What is the comparison operators for

Greater than or equal to

A

> =

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

What is the output of 42 == ‘42’ ? Why?

A

False, because and int or float are not equal to string

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

what is the output of 42 == 42.0?

A

True

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

what is the name and difference of == and =

A

The == operator (equal to) asks whether two values are the same as each other.
The = operator (assignment) puts the value on the right into the variable on the left.

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

What are the Boolean Operators?

A

and
or
no

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

Why are the and and or binary operators?

A

The and and or operators always take two Boolean values (or expressions), so they’re considered binary operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
The and and or Operator’s Truth Table:
True and True ?
True and False ?
False and True ? 
False and False ?
True or True ?
True or False ?
False or True ?
False or False ?
A
True
False
False
False
True
True
True
False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is not operator a unary operator

A

Unlike and and or, the not operator operates on only one Boolean value (or expression). This makes it a unary operator

19
Q

The not Operator’s Truth Table:
Expression Evaluates to . . .

not True ?

not False ?

A

False

True

20
Q

Example of Mixing Boolean and Comparison Operators

A

> > > (4 < 5) and (5 < 6)
True
2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
True

21
Q

what is the order of operations for the Boolean operators

A
math
comparison operators
not
and
or
22
Q

What are the Elements of Flow Control?

A

condition and block of code (clause)

23
Q

What is a block of code?

A

Blocks begin when the indentation increases.
Blocks can contain other blocks.
Blocks end when the indentation decreases to zero or to a containing block’s indentation.

24
Q

Example of an if statement? Explain the component

A

if name == ‘Alice’:
print(‘Hi, Alice.’)
1. The if keyword
2. A condition (that is, an expression that evaluates to True or False)
3. A colon
4. Starting on the next line, an indented block of code (called the if clause)

25
Q

what is an else statement?

A

An if clause can optionally be followed by an else statement
An else statement doesn’t have a condition, and in code, an else statement always consists of the following:

The else keyword
A colon
Starting on the next line, an indented block of code (called the else clause)

26
Q

What is elif statement?

A

The elif statement is an “else if” statement that always follows an if or another elif statement. It provides another condition that is checked only if all of the previous conditions were False

27
Q

In code, an elif statement always consists of the following:

A
  1. The elif keyword
  2. A condition (that is, an expression that evaluates to True or False)
  3. A colon
  4. Starting on the next line, an indented block of code (called the elif clause)
28
Q

what is the limitation of using elif statement?

A

Once one of the statements’ conditions is found to be True, the rest of the elif clauses are automatically skipped.

29
Q

What do a while Loop Statements do?

A

You can make a block of code execute over and over again using a while statement. The code in a while clause will be executed as long as the while statement’s condition is True.

30
Q

what does a while statement consist of?

A

The while keyword
A condition (that is, an expression that evaluates to True or False)
A colon
Starting on the next line, an indented block of code (called the while clause)

31
Q

The while clause is often called the —- or just —– .

A

while loop

the loop

32
Q

give an example of a while loop printing “Hello, world!” five times?

A

spam = 0
if spam < 5:
print(‘Hello, world.’)
spam = spam + 1

33
Q

what is a break statement?

A
There is a shortcut to getting the program execution to break out of a while loop’s clause early. If the execution reaches a break statement, it immediately exits the while loop’s clause.
➊ while True:
       print('Please type your name.')
    ➋ name = input()
    ➌ if name == 'your name':
        ➍ break
➎ print('Thank you!')
34
Q

How to terminate a program execution immediately?

A

Ctrl + C

35
Q

what is a continue statement?

A
Like break statements, continue statements are used inside loops. When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop and reevaluates the loop’s condition.
  while True:
      print('Who are you?')
      name = input()
    ➊ if name != 'Joe':
        ➋ continue
       print('Hello, Joe. What is the password? (It is a fish.)')
    ➌ password = input()
       if password == 'swordfish':
        ➍ break
➎ print('Access granted.')
36
Q

How to create a condition with and empty variable?

A

An empty variable is False,
name=””
while not name: # execute while until False, or until name is not empty any more

➋ if numOfGuests:
➌ print(‘Be sure to have enough room for all your guests.’)

37
Q

what if you want to execute a block of code only a certain number of times?

A

You can do this with a for loop statement and the range() function.

print(‘My name is’)
for i in range(5):
print(‘Jimmy Five Times (‘ + str(i) + ‘)’)

38
Q

What is the limitation of continue and break function usage?

A

We only can use them inside for and while loop

39
Q

What are the range() arguments?

A
range(start, stop, step)
range(5,-1,-1)
5
4
3
2
1
0
40
Q

What is importing a module?

A

If a function is not a built-in function, we need to import it
import random
import random, math, ….

41
Q

What if we name a python file similar to a function name?

A

then if we import a module, python import our file instead of the module !!!!

42
Q

How to get random numbers in a range?

A

import random
for i in range(5):
print(random.randint(1,10))

43
Q

How to end a program with sys.exit()

A

import sys

while True:
    print('Type exit to exit.')
    response = input()
    if response == 'exit':
        sys.exit()
    print('You typed ' + response + '.')