Python Fundamentals Flashcards

(20 cards)

1
Q

What are Python’s 8 main data types?

A

int, float, str, bool, list, tuple, set, dict.

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

What is the difference between a list and a tuple?

A

Lists are mutable (can change), tuples are immutable (cannot change).

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

How do you create a dictionary in Python?

A

Using curly braces: my_dict = {“key1”: value1, “key2”: value2}.

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

What is a Python set?

A

An unordered collection of unique elements.

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

How do you check the type of a variable?

A

Use type(variable).

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

What is the difference between == and is?

A

== checks value equality, is checks object identity (memory location).

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

How do you write a Python function?

A

Using def: def my_function(arg1, arg2): return result.

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

What are *args and **kwargs used for?

A

*args allows passing variable number of positional arguments, **kwargs for keyword arguments.

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

Using try, except, finally blocks.

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

How do you read a CSV file using Python?

A

Using pandas: df = pd.read_csv(‘file.csv’).

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

How do you iterate over a list?

A

Using for item in my_list: or list comprehensions [x for x in my_list].

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

What is a Python list comprehension?

A

A concise way to create lists: [x*2 for x in my_list if x>0].

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

What are Python modules and packages?

A

Modules are .py files; packages are directories of modules with __init__.py.

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

How do you import a module?

A

import module_name or from module_name import function_name.

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

What is the difference between shallow and deep copy?

A

Shallow copy copies the object but references nested objects; deep copy copies everything recursively (copy.deepcopy()).

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

What is Python’s lambda function?

A

An anonymous function: lambda x: x**2.

17
Q

What is the difference between append() and extend() for lists?

A

append() adds one element, extend() adds all elements from an iterable.

18
Q

How do you check if a key exists in a dictionary?

A

Using if key in my_dict:.

19
Q

What is Python’s enumerate() function?

A

Returns index and value when iterating: for i, v in enumerate(my_list):.

20
Q

How do you sort a list in Python?

A

Using my_list.sort() (in-place) or sorted(my_list) (returns new list).