What is a variable in Python? Provide an example.
A variable in Python is a reserved memory location to store values. For example, x = 5 assigns the integer value 5 to variable x.
How do you create a list in Python with mixed data types? Provide an example.
A list in Python can include mixed data types and is defined with square brackets. Example: my_list = [1, 'Hello', 3.14]
Explain the use of the if statement in Python with an example.
The if statement is used for conditional execution. For example: if x > 0: print('Positive') will print ‘Positive’ if x is greater than 0.
How can you handle exceptions in Python? Provide a code snippet.
Exceptions in Python can be handled using the try-except block. Example:
```python
try:
print(10/0)
except ZeroDivisionError:
print(‘Cannot divide by zero’)
~~~
What is a function in Python? Provide an example of a simple function.
A function in Python is a block of code which only runs when it is called. For example, def greet(): print('Hello!') defines a function that prints ‘Hello!’.
How do you iterate over a list in Python? Provide an example using a for loop.
To iterate over a list in Python, you can use a for loop. Example:
```python
for item in [1, 2, 3]:
print(item)
~~~
Describe how to import a module in Python and provide an example with the math module.
To import a module in Python, use the import statement. For example, import math allows access to mathematical functions.
How do you create a class in Python? Provide an example with a simple class.
A class in Python is created using the class keyword. Example:
```python
class Dog:
def bark(self):
print(‘Woof!’)
~~~
Explain dictionary comprehension in Python with an example.
Dictionary comprehension provides a concise way to create dictionaries. Example: {x: x**2 for x in (1, 2, 3)} creates a dictionary with numbers as keys and their squares as values.
What is the use of the append() method in lists? Provide an example.
The append() method adds an element to the end of a list. Example: my_list = [1, 2]; my_list.append(3) results in [1, 2, 3].
What does the built-in function abs do in Python?
The built-in function abs is used for various purposes, for example…
What does the built-in function all do in Python?
The built-in function all is used for various purposes, for example…
What does the built-in function any do in Python?
The built-in function any is used for various purposes, for example…
What does the built-in function ascii do in Python?
The built-in function ascii is used for various purposes, for example…
What does the built-in function bin do in Python?
The built-in function bin is used for various purposes, for example…
What does the built-in function bool do in Python?
The built-in function bool is used for various purposes, for example…
What does the built-in function bytearray do in Python?
The built-in function bytearray is used for various purposes, for example…
What does the built-in function bytes do in Python?
The built-in function bytes is used for various purposes, for example…
How to implement recursion in Python?
Here’s a basic example of how recursion can be implemented: …
How to implement inheritance in Python?
Here’s a basic example of how inheritance can be implemented: …
How to implement polymorphism in Python?
Here’s a basic example of how polymorphism can be implemented: …
How to implement encapsulation in Python?
Here’s a basic example of how encapsulation can be implemented: …
How to implement iteration in Python?
Here’s a basic example of how iteration can be implemented: …
How to implement enumeration in Python?
Here’s a basic example of how enumeration can be implemented: …