Python Flashcards

1
Q

What are the different data types in Python?

A

Python has several built-in data types including int, float, str, list, tuple, dict, set, and bool

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

How do you create a list, tuple, or dictionary in Python?

A

List: my_list = [1, 2, 3]
Tuple: my_tuple = (1, 2, 3)
Dictionary: my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}

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

What is the difference between mutable and immutable data types?

A

Mutable: Can be changed after creation (e.g., list, dict, set).
Immutable: Cannot be changed after creation (e.g., int, float, str, tuple).

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

What types of loops are there?

A

For loops & while loops

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

How do you handle conditional statements?

A

Use if, elif, and else to execute code based on conditions.

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

How do you define a function in Python?

A

Use the def keyword:

def my_function(parameters):
# Function body
return result

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

What is the difference between a function and a method?

A

Function: A block of reusable code defined using def.
Method: A function that is associated with an object (called using object.method()).

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

How do you import a module in Python?

A

Use import module_name or from module_name import specific_function.

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

How do you handle exceptions in Python?

A

Use try and except blocks:

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

Can you give an example of a try-except block?

A

try:
result = 10 / 0
except ZeroDivisionError:
print(“Cannot divide by zero!”)

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

How do you read from and write to a file in Python?

A

Use the open() function with modes r (read) or w (write):

with open(‘file.txt’, ‘r’) as file:
contents = file.read()

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

What are some common methods for handling file operations?

A

read(), readline(), write(), and close().

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

How do you use a third-party library in your script?

A

Install with pip install library_name and import with import library_name.

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

What tools or environments do you use for developing Python scripts?

A

Common tools include IDEs like PyCharm, VS Code, and Jupyter Notebook.

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

What is a class in Python, and how do you create one?

A

A class is a blueprint for creating objects, defined using the class keyword:

class MyClass:
def __init__(self, attribute):
self.attribute = attribute

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

Explain inheritance and how it is implemented in Python.

A

Inheritance allows a class to inherit attributes and methods from another class. Implemented using class declaration:
class ParentClass:
# Parent methods and attributes

class ChildClass(ParentClass):
# Inherits from ParentClass

17
Q

Write a script to read a CSV file and print its contents.

A

with open(‘file.csv’, newline=’’) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row)

18
Q

Create a function that takes a list of numbers and returns the average.

A

def calculate_average(numbers):
return sum(numbers) / len(numbers) if numbers else 0

19
Q

Write a Python script that fetches data from an API and processes it.

A

import requests

response = requests.get(‘https://api.example.com/data’)
if response.status_code == 200:
data = response.json()
# Process data