Programming Part 1 Flashcards
(27 cards)
What are the basic data types in Python?
int, float, str, bool
What is a list in Python?
An ordered, mutable collection of items.
What is a tuple in Python?
An ordered, immutable collection of items.
What is a dictionary in Python?
A collection of key-value pairs.
What is a set in Python?
An unordered collection of unique items.
What is an if-statement used for?
To execute code conditionally based on a boolean expression.
What is a for loop used for?
To iterate over a sequence or iterable.
What is a while loop used for?
To repeat code while a condition is true.
What does the ‘break’ statement do?
It exits a loop immediately.
What does the ‘continue’ statement do?
It skips to the next iteration of the loop.
How do you define a function in Python?
Using the ‘def’ keyword.
What is the difference between a parameter and an argument?
A parameter is in the function definition; an argument is the value passed in.
What is a return statement?
It sends a result back from a function to the caller.
What is a lambda function?
An anonymous function defined with the ‘lambda’ keyword.
What is a docstring?
A string that documents a function’s purpose, inputs, and outputs.
What is list comprehension?
A concise way to create lists using a loop in a single line.
How do you write a list comprehension that squares numbers?
[x**2 for x in numbers]
What is dictionary comprehension?
Creating dictionaries using key-value pairs in a single line.
What is a try/except block used for?
To handle errors without crashing the program.
What is a common Python error when dividing by zero?
ZeroDivisionError
What is a NameError?
An error that occurs when trying to use a variable that hasn’t been defined.
What is a TypeError?
An error when an operation is applied to an object of inappropriate type.
What does the len() function do?
Returns the number of items in an object.
What does the range() function do?
Generates a sequence of numbers.