Python Basics
Python Basics
Q2. List out the different types of operators available in Python programming language.
Illustrate each with a suitable example.
Q3. Explain different Data types and Data structures available in Python.
1. Data Types:
o int: Integer values.
o float: Floating-point values.
o str: String values.
o bool: Boolean values (True or False).
o complex: Complex numbers.
2. Data Structures:
o List: Ordered, mutable collection. e.g., [1, 2, 3]
o Tuple: Ordered, immutable collection. e.g., (1, 2, 3)
o Set: Unordered, mutable collection of unique elements. e.g., {1, 2, 3}
o Dictionary: Unordered collection of key-value pairs. e.g., {'a': 1, 'b':
2}
Q4. Give the importance of operator precedence and associativity rules. Evaluate the
following expression by applying the operator precedence and associativity rules For
a=5, b=3, c=12, d=10 and e=3
Operator precedence and associativity determine the order in which operations are performed
in an expression. This ensures expressions are evaluated correctly.
1. a + b ** (c // d) * e
o c // d => 12 // 10 => 1
o b ** 1 => 3 ** 1 => 3
o 3 * e => 3 * 3 => 9
o a + 9 => 5 + 9 => 14
o Result: 14
2. a * (e + c) - d / b
o e + c => 3 + 12 => 15
o a * 15 => 5 * 15 => 75
o d / b => 10 / 3 => 3.333...
o 75 - 3.333... => 71.666...
o Result: 71.666...
Q8. Describe the different types of looping statements of Python programming language
with suitable examples.
Q10. Define list. List out the important features of the list and demonstrate how to add
an element to the list using the + operator, append(), and insert() methods.
• Local Variables: Defined inside a function, accessible only within that function.
• Global Variables: Defined outside all functions, accessible throughout the program.
• Example:
python
x = "global"
def func():
x = "local"
print(x)
func() # Prints "local"
print(x) # Prints "global"
python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution finished")