Python Basics, Conditionals, and Loops Flashcards
What are the three types of quotes you can use for strings in Python?
Single (‘ ‘), Double (“ “), and Triple (‘’’ ‘’’ or “”” “””).
When should you use triple quotes for strings?
When the string contains both single and double quotes.
How do you print numerical values in Python?
Using print(), optionally converting numbers to strings using str().
What does print(‘The value of x is: ‘ + str(5.5)) output?
The value of x is: 5.5
What are some common data types in Python?
Integers, floats, characters, strings, and Boolean values.
What is an f-string, and how is it used?
An f-string allows embedding variables directly in a string using {}. Example:
name = “Alice”
print(f”Hello, {name}!”)
What does // do in Python?
It performs integer division, discarding the decimal part.
What does % do in Python?
It finds the remainder of a division.
What does ** do in Python?
It calculates exponents (e.g., 2 ** 3 equals 8).
How do you convert a float to an integer?
Using int(). Example: int(5.5) returns 5.
How do you convert an integer to a float?
Using float(). Example: float(2) returns 2.0.
What function is used to get user input in Python?
name = input(“Enter your name: “)
print(name)
What is an if statement?
A control structure that runs a block of code if a condition is True.
What is the difference between if and elif?
if checks the first condition, and elif checks additional conditions if the previous ones were False.
What does != do in Python?
It checks if two values are not equal.
What does and do in Python?
It returns True only if both conditions are True.
What does or do in Python?
It returns True if at least one of the conditions is True.
What are the two types of loops in Python?
while loops and for loops.
When should you use a while loop?
When the number of repetitions is unknown, and you repeat until a condition is met.
When should you use a for loop?
When looping over a known range
What does range(5) do in a for loop?
It generates numbers from 0 to 4.
What does break do in a loop?
It immediately exits the loop.
What does continue do in a loop?
It skips the current iteration and moves to the next one.