Imp Questions
Imp Questions
Exception handling in Python allows the programmer to handle runtime errors gracefully. It
uses try, except, else, and finally blocks to catch and handle exceptions without
terminating the program abruptly.
Example:
Try:
Print(10 / num)
Except ValueError:
Except ZeroDivisionError:
Finally:
Print(“Execution completed.”)
2. Explain Inheritance
Inheritance in Python enables a class (child class) to derive properties and methods from
another class (parent class). It allows code reusability and the creation of hierarchical
relationships.
Example:
Class Animal:
Def speak(self):
Print(“Animal speaks”)
Class Dog(Animal):
Def speak(self):
Print(“Dog barks”)
D = Dog()
d.speak()
Turtle graphics is a Python library for creating graphics and drawings. It uses a virtual
“turtle” that moves around the screen, drawing lines or shapes as per the commands.
Basic commands:
Example:
Import turtle
T = turtle.Turtle()
t.forward(100)
t.right(90)
t.forward(50)
turtle.done()
Modules in Python are files containing Python code (functions, classes, variables) that can
be reused. Built-in modules (e.g., math, os, random) and custom modules simplify
programming tasks.
Example:
Import math
Print(math.sqrt(16))
5. Explain Object-Oriented Programming Using Python
Example:
Class Car:
Self.brand = brand
Def display(self):
My_car = Car(“Toyota”)
My_car.display()
6. Explain Text Files and Types of Text Files
Text files store data in human-readable format, using characters. Common types:
Content = file.read()
Print(content)
Example:
Num = 10
If num > 5:
Keyword arguments are passed by explicitly specifying the parameter name. They improve
code readability and allow argument reordering.
Example:
Greet(age=20, name=”Alice”)
Example:
Try:
Print(“Result:”, result)
Except ZeroDivisionError:
Except ValueError:
Print(“Invalid input, enter a number.”)
Python uses pass-by-object-reference. Objects like lists can be modified inside functions,
while immutable types like integers remain unchanged.
Example:
Def modify_list(lst):
Lst.append(4)
My_list = [1, 2, 3]
Modify_list(my_list)
Example:
For I in range(5):
If I == 3:
Continue
Print(i)
Arithmetic: +, -, *, /
Assignment: =, +=, -=
Example:
A, b = 10, 20
Print(a + b) # Addition
Example:
Exception handling in Python allows the programmer to handle runtime errors gracefully. It
uses try, except, else, and finally blocks to catch and handle exceptions without
terminating the program abruptly.
Example:
Try:
Print(10 / num)
Except ValueError:
Except ZeroDivisionError:
Finally:
Print(“Execution completed.”)
2. Explain Inheritance
Inheritance in Python enables a class (child class) to derive properties and methods from
another class (parent class). It allows code reusability and the creation of hierarchical
relationships.
Example:
Class Animal:
Def speak(self):
Print(“Animal speaks”)
Class Dog(Animal):
Def speak(self):
Print(“Dog barks”)
D = Dog()
d.speak()
Turtle graphics is a Python library for creating graphics and drawings. It uses a virtual
“turtle” that moves around the screen, drawing lines or shapes as per the commands.
Basic commands:
Example:
Import turtle
T = turtle.Turtle()
t.forward(100)
t.right(90)
t.forward(50)
turtle.done()
Modules in Python are files containing Python code (functions, classes, variables) that can
be reused. Built-in modules (e.g., math, os, random) and custom modules simplify
programming tasks.
Example:
Import math
Print(math.sqrt(16))
Example:
Class Car:
Self.brand = brand
Def display(self):
My_car.display()
Text files store data in human-readable format, using characters. Common types:
Content = file.read()
Print(content)
Example:
Num = 10
If num > 5:
Keyword arguments are passed by explicitly specifying the parameter name. They improve
code readability and allow argument reordering.
Example:
Greet(age=20, name=”Alice”)
Example:
Try:
Print(“Result:”, result)
Except ZeroDivisionError:
Except ValueError:
Python uses pass-by-object-reference. Objects like lists can be modified inside functions,
while immutable types like integers remain unchanged.
Example:
Def modify_list(lst):
Lst.append(4)
My_list = [1, 2, 3]
Modify_list(my_list)
Example:
For I in range(5):
If I == 3:
Continue
Print(i)
Arithmetic: +, -, *, /
Example:
A, b = 10, 20
Print(a + b) # Addition
Example:
Name = “John” # String literal