Python basics Flashcards
What is a variable in Python?
A variable is a symbolic name that stores data in memory. It allows you to refer to the value later in the code.
Example:
x = 10
name = “Alice”
What are the main data types in Python?
the main data types include int (integer), float (decimal), str (string), bool (boolean), list, tuple, dict (dictionary), and set.
How do you comment code in Python?
Use # for single-line comments, and triple quotes ‘’’ or “”” for multi-line comments.
Example:
# This is a single-line comment
“””
This is a
multi-line comment
“””
What does type() do in Python?
type() returns the data type of a variable or value.
Example:
print(type(10)) # Output: <class ‘int’>
How do you create a list in Python?
A list is created using square brackets [] and can hold multiple data types.
Example:
fruits = [“apple”, “banana”, “cherry”]
What is the difference between a list and a tuple?
A list is mutable (changeable), while a tuple is immutable (cannot be changed after creation).
Example:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
How do you access a dictionary value by key?
Use square brackets with the key name to access the corresponding value.
Example:
person = {“name”: “Alice”, “age”: 25}
print(person[“name”]) # Output: Alice
What is a set in Python?
A set is an unordered collection of unique elements.
Example:
my_set = {1, 2, 3, 3}
# Output: {1, 2, 3}
Explain an if statement in Python.
An if statement checks a condition and executes the code block if the condition is True.
Example:
x = 10
if x > 5:
print(“x is greater than 5”)
What is a for loop used for in Python?
A for loop iterates over a sequence (like a list, tuple, or string) and executes a code block for each element.
Example:
for fruit in [“apple”, “banana”, “cherry”]:
print(fruit)
What is the purpose of a while loop?
A while loop repeats a block of code as long as a specified condition is True.
Example:
count = 0
while count < 5:
print(count)
count += 1
How do you define a function in Python?
Use the def keyword followed by the function name and parentheses. Code inside the function is indented.
Example:
def greet(name):
print(“Hello, “ + name)
What is the purpose of the return statement in a function?
return exits the function and optionally returns a value to the caller.
Example:
def add(x, y):
return x + y
Explain the difference between local and global variables.
Local variables are defined within a function and cannot be accessed outside of it, while global variables are defined outside functions and can be accessed anywhere in the code.
What are list comprehensions?
List comprehensions provide a concise way to create lists based on existing iterables, often with conditions.
Example:
squares = [x**2 for x in range(10)]
what is the difference between yield and return
return
Exits the function immediately, returning a single value or object.
Memory Usage: Stores the entire result in memory, which can be inefficient for large data.
Example:
def get_numbers():
return [1, 2, 3, 4, 5]
yield
Pauses the function, saving its state, and returns a value temporarily.
Returns a generator: Allows handling one item at a time, ideal for large data.
Example:
def get_numbers():
for i in range(1, 6):
yield i
Summary
return: Exits the function with a single result.
yield: Pauses, returns items one by one, making it memory-efficient for large data.
What is a generator in Python?
A generator is a function that yields values one at a time and allows you to iterate through them without storing the entire sequence in memory.
Example:
def count_up_to(n):
count = 1
while count <= n:
yield count
count += 1
How do you add an element to a list?
Use append() to add an element to the end, or insert(index, element) to add it at a specific position.
numbers = [1, 2, 3]
numbers.append(4) # [1, 2, 3, 4]
numbers.insert(1, 10) # [1, 10, 2, 3, 4]
How do you remove duplicates from a list?
Convert the list to a set and back to a list.
Example:
items = [1, 2, 2, 3]
unique_items = list(set(items)) # [1, 2, 3]
What is a tuple, and how is it different from a list?
A tuple is an immutable, ordered sequence. Unlike lists, tuples cannot be modified after creation.
How can you access elements in a tuple?
Use indexing, similar to lists.
Example:
my_tuple = (1, 2, 3)
print(my_tuple[0]) # Output: 1
How do you access a value in a dictionary?
Use the key in square brackets or the get() method.
Example:
person = {“name”: “Alice”}
print(person[“name”]) # Output: Alice
How do you remove a key-value pair from a dictionary?
Use pop(key) or del statement.
Example:
person.pop(“age”)
del person[“name”]
What is a set in Python?
An unordered collection of unique elements, defined with {}.
How do you add elements to a set?
Use add() for a single element, or update() for multiple elements.
Example:
my_set = {1, 2}
my_set.add(3) # {1, 2, 3}
my_set.update([4, 5]) # {1, 2, 3, 4, 5}
What is the difference between union() and intersection() methods in sets?
union() combines elements from both sets, while intersection() returns only common elements.
How do you reverse a list in Python?
You can use the reverse() method (modifies the list in place) or slice notation.
lst = [1, 2, 3, 4]
lst.reverse() # In-place modification
# or
reversed_lst = lst[::-1] # Slicing