1. Explain Basic python functions with example.
2. Explain python List with example.
3.Explain python Tuple with example.
.Explainpython Set with example.
5. Explain Conditional Statements in python.
6. Explain Exception handling in python.
7.Explain Assert statement in python with example.
8. Explain Multiple lnheritance in python with example.
9. Explain constructor in python.
10. Explain anonymous functions in python with example. First a follwrite down question and
then write a simple answer
1. Basic Python Functions:
Afunction in Python is a block of reusable code that performsa specific task. It takes inputs,
processes them, and returns an output. Functions help in modularizing code and making it
more readable.
Example:
python O Copy code
def add_numbers(x, y):
result = X + y
return result
sum _result = add_numbers(3, 5) # Calls the function
print (sum_result) # Output: 8
2. Python List:
A list in Python is a collection of items that are ordered and changeable. It allows duplicate
values and can contain different data types. Lists are created using square brackets LJ'.
Example:
python OCopy code
my_list = [1, 2, 3, 'apple' , 'banana' ]
print (my_list ) # Output: [1, 2, 3, 'apple' , 'banana' ]
3. Python Tuple:
A tuple is similar to a list but is immutable, meaning its elements cannot be changed after
creation. Tuples are created using parentheses 0.They are often used to group related
data.
Example:
python Copy code
my_tuple = (1,2, 3, ' apple' , 'banana')
print (my _tuple ) # Output: (1, 2, 3, 'apple' , 'banana' )
4. Python Set:
Aset in Python is an unordered collection of unique elements. It's useful for operations like
testing membership and removing duplicate elements. Sets are created using curly braces
Example:
python Copy code
my_set = (1, 2, 3, 3, 4, 5)
print (my_set) # Output: (1, 2, 3, 4, 5)
5. Conditional Statements:
Conditional statements in Python allow you to control the flow of your program based on
certain conditions. These are if,'elif' (else if), and 'else.
Example:
python O Copy code
age = 25
if age >= 18:
print("You are an adult . ")
else:
print ("You are a minor. ")
6. Exception Handling:
Exception handling in Python allows you to gracefully deal with errors or exceptions that
might occur during program execution. It involves try', 'except', 'else', and finally '
blocks.
Example:
python Copy code
try:
result = 10/0
except ZeroDivisionError as e:
print(f"Error: (e)")
7. Assert Statement:
The 'assert statement is used to test conditions that should always be true. If the condition
is false, it raises an AssertionError.
Example:
python Copy code
X = 2
assert x == 1, "x should be 1"
8. Multiple Inheritance:
Multiple inheritance in Python allows a class to inherit attributes and methods from multiple
base classes. It's achieved by listing the parent classes in the class definition.
Example:
python O Copy code
class A:
def method_ a(self) :
print ("Method A")
class B:
def method _ b(self):
print (" Method B")
class C(A, B) :
pass
obj_c = C)
obj_c.method_a()
obj_c.method_b()
9. Constructor in Python:
A
constructor in Python is a special method (' ._init__)that gets called when an object of a
class is instantiated. It's used to initialize the object's attributes.
Example:
python 9 Copy code
class Person:
def init__(self, name, age):
self. name = name
self. age = age
john = Person("John Doe" , 30)
10. Anonymous Functions (Lambda Functions):
Anonymous functions, also known as lambda functions, are small, one-line functions without
a name. They are defined using the lambda keyword.
Example:
python Copy code
square = lambda x: x**2
result = square (5) # Output: 25