pytnbeginer Flashcards

1
Q

What are the basic data types in Python?

A

The basic data types in Python are int, float, str, and bool.

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

How do you write an if statement in Python?

A

An if statement in Python is written as:
if condition:
# code to execute if condition is true

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

What is a function in Python?

A

A function in Python is a block of code that performs a specific task. It is defined using the ‘def’ keyword.

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

How do you create a list in Python?

A

A list in Python is created using square brackets, e.g., my_list = [1, 2, 3]

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

What is a dictionary in Python?

A

A dictionary in Python is a collection of key-value pairs, e.g., my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}

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

How do you handle exceptions in Python?

A

Exceptions in Python are handled using try/except blocks. Code that may raise an exception is placed in the try block, and the code to handle the exception is placed in the except block.

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

What is a class in Python?

A

A class in Python is a blueprint for creating objects. It is defined using the ‘class’ keyword.

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

How do you create a tuple in Python?

A

A tuple in Python is created using parentheses, e.g., my_tuple = (1, 2, 3)

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

What is a set in Python?

A

A set in Python is an unordered collection of unique elements, e.g., my_set = {1, 2, 3}

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

How do you read a file in Python?

A

To read a file in Python, use the open() function with the ‘r’ mode, e.g., with open(‘file.txt’, ‘r’) as file: content = file.read()

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

How do you write to a file in Python?

A

To write to a file in Python, use the open() function with the ‘w’ mode, e.g., with open(‘file.txt’, ‘w’) as file: file.write(‘text’)

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

What is a module in Python?

A

A module in Python is a file containing Python code that can be imported and used in other Python programs.

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

How do you import a module in Python?

A

To import a module in Python, use the import statement, e.g., import module_name

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

What is a package in Python?

A

A package in Python is a collection of modules organized in directories.

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

How do you install a package in Python?

A

To install a package in Python, use pip, e.g., pip install package_name

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

What is a loop in Python?

A

A loop in Python is used to iterate over a sequence of elements.

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

How do you write a for loop in Python?

A

A for loop in Python is written as:
for element in sequence:
# code to execute for each element

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

How do you write a while loop in Python?

A

A while loop in Python is written as:
while condition:
# code to execute while condition is true

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

What is the difference between a list and a tuple?

A

A list is mutable (can be changed), whereas a tuple is immutable (cannot be changed).

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

How do you access elements in a list?

A

Elements in a list are accessed using their index, e.g., my_list[0]

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

How do you add elements to a list?

A

Elements are added to a list using the append() method, e.g., my_list.append(4)

22
Q

How do you remove elements from a list?

A

Elements are removed from a list using the remove() method, e.g., my_list.remove(2)

23
Q

What is a lambda function in Python?

A

A lambda function in Python is a small anonymous function defined using the lambda keyword, e.g., lambda x: x * 2

24
Q

How do you create a dictionary with default values?

A

A dictionary with default values is created using defaultdict from the collections module, e.g., defaultdict(int)

25
Q

How do you iterate over a dictionary?

A

You iterate over a dictionary using a for loop, e.g., for key, value in my_dict.items():

26
Q

How do you create a list comprehension?

A

A list comprehension is a concise way to create lists, e.g., [x for x in range(5)]

27
Q

What is the difference between ‘==’ and ‘is’ in Python?

A

’==’ checks for value equality, while ‘is’ checks for identity (whether they are the same object).

28
Q

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

A

To check if a key exists in a dictionary, use the in keyword, e.g., ‘key’ in my_dict

29
Q

How do you merge two dictionaries?

A

To merge two dictionaries, use the update() method, e.g., dict1.update(dict2)

30
Q

How do you sort a list in Python?

A

To sort a list in Python, use the sort() method, e.g., my_list.sort()

31
Q

What is a generator in Python?

A

A generator in Python is a function that returns an iterator using the yield keyword.

32
Q

How do you create a generator?

A

A generator is created like a regular function but uses the yield keyword instead of return.

33
Q

What is the use of ‘yield’ keyword in Python?

A

The ‘yield’ keyword is used to produce a value from a generator and pause its state.

34
Q

How do you handle multiple exceptions in a single block?

A

Multiple exceptions are handled using multiple except blocks or a single except block with a tuple of exceptions.

35
Q

What is the purpose of the ‘finally’ block?

A

The ‘finally’ block contains code that will run no matter what, whether an exception occurs or not.

36
Q

How do you define a class method?

A

A class method is defined using the @classmethod decorator and takes cls as its first parameter.

37
Q

What is inheritance in Python?

A

Inheritance in Python allows a class to inherit attributes and methods from another class.

38
Q

How do you override a method in Python?

A

To override a method in Python, define a method in the subclass with the same name as in the superclass.

39
Q

What is polymorphism in Python?

A

Polymorphism in Python allows methods to do different things based on the object it is acting upon.

40
Q

How do you create an instance of a class?

A

An instance of a class is created by calling the class, e.g., my_object = MyClass()

41
Q

What is encapsulation in Python?

A

Encapsulation in Python is the bundling of data and methods that operate on that data within a single unit.

42
Q

How do you define a private attribute in a class?

A

A private attribute in a class is defined by prefixing it with double underscores, e.g., __my_private_attr

43
Q

What is the purpose of the ‘self’ parameter?

A

The ‘self’ parameter refers to the instance of the class and is used to access variables and methods associated with the class.

44
Q

How do you create a constructor in Python?

A

A constructor in Python is defined using the __init__ method and initializes the object’s attributes.

45
Q

What is a decorator in Python?

A

A decorator in Python is a function that modifies the behavior of another function.

46
Q

How do you use the ‘with’ statement for file handling?

A

The ‘with’ statement is used for resource management and ensures proper acquisition and release of resources, e.g., with open(‘file.txt’, ‘r’) as file:

47
Q

What is the ‘pass’ statement used for?

A

The ‘pass’ statement is a null operation used as a placeholder in functions, loops, etc.

48
Q

How do you convert a string to an integer in Python?

A

To convert a string to an integer in Python, use the int() function, e.g., int(‘123’)

49
Q

How do you check the length of a list in Python?

A

To check the length of a list in Python, use the len() function, e.g., len(my_list)

50
Q

How do you concatenate strings in Python?

A

To concatenate strings in Python, use the + operator, e.g., ‘Hello’ + ‘ ‘ + ‘World’