CS50 Python Week 1: Shorts

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/31

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

32 Terms

1
New cards

What are conditionals used for in Python?

To ask questions and make decisions based on the answers.

2
New cards

What Python keyword starts a conditional block?

if

3
New cards

What type of expression does an if statement use?

A Boolean expression (evaluates to True or False)

4
New cards

How do you write a basic if statement in Python?

if condition:

# code

5
New cards

What does the else keyword do in a conditional?

It defines code to run if the if condition is false.

6
New cards

How do you write an if-else statement in Python?

if condition:

# code

else:

# other code

7
New cards

What is a nested conditional?

An if or else statement inside another if or else.

8
New cards

Why use nested conditionals?

To check multiple, dependent conditions in sequence.

9
New cards

What does elif mean in Python?

“Else if” — checks another condition if the first is false.

10
New cards

How do you use elif in a conditional chain?

if cond1:

# code

elif cond2:

# other code

11
New cards

When should you use elif instead of multiple if statements?

When only one condition should be true and run.

12
New cards

Why is input validation important in conditionals?

To handle unexpected or invalid user inputs.

13
New cards

How do you provide feedback for invalid input?

print("Enter a valid option")

14
New cards

What happens if the user input doesn’t match any condition?

The program should inform the user and handle the input safely.

15
New cards

What is a Boolean expression?

A question that returns True or False.

16
New cards

What does == do in a condition?

Compares values for equality.

17
New cards

What does != do in a condition?

Checks if two values are not equal.

18
New cards

What does the or operator do?

Returns True if at least one condition is true.

19
New cards

Syntax example using or in Python?

if x == 1 or x == 2:

20
New cards

What does the and operator do?

Returns True only if both conditions are true.

21
New cards

Syntax example using and in Python?

if x == 1 and y == 2:

22
New cards

What does the not operator do?

Reverses the Boolean value (True becomes False).

23
New cards

Syntax example using not in Python?

if not valid:

24
New cards

How do you check for multiple valid options using or?

if difficulty == "Difficult" or difficulty == "Casual":

25
New cards

How do you validate invalid inputs using not?

if not (condition):

26
New cards

How do you combine conditions using and for accuracy?

if difficulty == "Difficult" and players == "Multiplayer":

27
New cards

How does Boolean logic improve readability?

It simplifies complex conditions into clear expressions.

28
New cards

How does Boolean logic reduce redundancy?

It replaces repeated or nested checks with concise logic.

29
New cards

How does Boolean logic help with input validation?

It enables checking for all valid or invalid options clearly.

30
New cards

Why is testing with valid and invalid inputs important?

To ensure the program handles all scenarios properly.

31
New cards

What should test cases for conditionals include?

All valid paths, all invalid inputs, and edge cases.

32
New cards

What is the goal of testing Boolean logic?

To confirm that each branch executes as expected.