pdf week 2 Flashcards

1
Q

What do conditionals allow in programming?

A

Conditionals allow a program to make decisions based on certain conditions, like choosing a path based on user input

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

Give examples of when conditionals are useful.

A

When a user inputs wrong data (e.g., expecting an integer but receiving text).
When reaching the end of a file.
After a sufficient number of actions are performed (e.g., printing numbers from 1 to 10).

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

>

A

greater than

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

<

A

less than

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

> =

A

greater than or equal to

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

<=

A

less than or equal to

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

==

A

equals to

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

!=

A

not equal to

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

What is control flow?

A

Control flow refers to the order in which individual statements, instructions, or function calls are executed in a program.

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

How does elif improve the efficiency of conditional statements?

A

elif allows the program to skip subsequent checks once a true condition is found, reducing unnecessary evaluations.

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

Why use an else statement?

A

An else statement provides a default action when none of the preceding conditions are met, simplifying logic.

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

What does the or operator do?

A

The or operator allows the program to evaluate multiple conditions, executing the code block if at least one condition is true.

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

What does the and operator do?

A

The and operator requires all conditions to be true for the code block to execute.

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

What is short-circuit evaluation?

A

In short-circuit evaluation, if the first condition of an and or or statement determines the outcome, the second condition is not evaluated.

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

What are nested conditionals?

A

Nested conditionals are conditionals placed inside other conditionals, allowing for more complex decision-making.

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

How can conditionals be used with strings?

A

You can compare strings, check for the presence of substrings, and manipulate strings based on conditions (e.g., modifying case, stripping whitespace).

17
Q

What is short-circuit evaluation in programming?

A

Short-circuit evaluation means that in logical expressions, not all parts of the expression need to be evaluated if the result can be determined early.

18
Q

How does short-circuit evaluation work with the and operator?

A

If the first expression connected by and evaluates to False, the entire compound expression is False without evaluating the second expression.

19
Q

How does short-circuit evaluation work with the or operator?

A

If the first expression connected by or evaluates to True, the entire compound expression is True without evaluating the second expression.

20
Q

How do you check for equality in Python?

A

Use == to check if two strings are equal, e.g., “test” == myString.

21
Q

How do you check if two strings are not equal?

A

Use !=, e.g., “test” != myString.

22
Q

What does the equality comparison return?

A

It returns a boolean value: True or False.

23
Q

How does the equality comparison work?

A

It checks one character at a time from left to right.

24
Q

How do you check if a substring exists in a string?

A

Use in, e.g., “test” in myString.

25
Q

How do you check if a substring does not exist in a string?

A

Use not in, e.g., “test” not in myString.

26
Q

How can you find the number of characters in a string?

A

Use len(myString) to get the length.

27
Q

How do you convert a string to upper case?

A

Use .upper(), e.g., myString.upper().

28
Q

How do you convert a string to lower case?

A

Use .lower(), e.g., myString.lower().

29
Q

How do you remove leading and trailing whitespace from a string?

A

Use .strip(), e.g., myString.strip().

30
Q

How do you replace a part of a string with another?

A

Use .replace(“target”, “replacement”), e.g., myString.replace(“target”, “replacement”).

31
Q

How do you start using random number generation in Python?

A

Import the random module at the beginning of your program: import random.

32
Q

How do you generate a random floating point number?

A

Use random.random(), which generates a value between [0.0, 1.0)

33
Q

How do you generate a random integer between two values?

A

Use random.randint(val1, val2), which generates an integer in the range [val1, val2].

34
Q

What type of values are required for random.randint(val1, val2)?

A

Both val1 and val2 must be integer values.

35
Q

How do you generate a random float between two specified values?

A

Use random.uniform(val1, val2), which generates a floating point value between [val1, val2].

36
Q

How do you generate a random integer between 0 and a specified value?

A

Use random.randrange(val1), which generates an integer in the range [0, val1).

37
Q

How do you generate a random integer between two specific values using randrange?

A

Use random.randrange(val1, val2), which generates an integer in the range [val1, val2).

38
Q

How can you generate a random integer with a specific step interval?

A

Use random.randrange(val1, val2, val3), which generates an integer in the range [val1, val2) with a step of val3.