0% found this document useful (0 votes)
3 views11 pages

PYTHON

The document covers various fundamental concepts of Python programming, including the Python Virtual Machine (PVM), bytecode, tokens, variables, data types, loops, operators, conditional statements, functions, classes, and more. It explains key features like the constructor, method overloading, polymorphism, and provides examples for practical understanding. The content serves as a comprehensive guide for learners to grasp essential Python programming principles and their applications.

Uploaded by

dassonai897
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

PYTHON

The document covers various fundamental concepts of Python programming, including the Python Virtual Machine (PVM), bytecode, tokens, variables, data types, loops, operators, conditional statements, functions, classes, and more. It explains key features like the constructor, method overloading, polymorphism, and provides examples for practical understanding. The content serves as a comprehensive guide for learners to grasp essential Python programming principles and their applications.

Uploaded by

dassonai897
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Discuss Python Virtual Machine (PVM) (3/2 Marks)


2 Marks: The Python Virtual Machine (PVM) executes
bytecode instructions line by line, translating them into
machine-level code for execution.
3 Marks: PVM is part of Python’s interpreter, ensuring
platform independence and handling memory management
and exception handling for Python programs.

2. What is Bytecode? Discuss its Role in Python


Programming (3/2 Marks)
2 Marks: Bytecode is Python's intermediate code, generated
after the source code is compiled, and executed by the Python
Virtual Machine (PVM).
3 Marks: It enables cross-platform compatibility and
optimizes execution by acting as a bridge between source
code and machine-level code.

3. What is a Token in Python? How to Create Variables?


(3/5 Marks)
3 Marks: Tokens are the smallest units in Python (keywords,
literals, operators, etc.). Variables are created by assigning a
value to a name (e.g., x = 10).
5 Marks: Example:
x = 10 # Integer variable
y = "Hello" # String variable
z = [1, 2, 3] # List variable

Tokens structure code, and variables store dynamic data in


Python programs.

4. What is a Variable? (1/2 Marks)


1 Mark: A variable is a named location in memory that stores
data.
2 Marks: It holds values that can be updated during program
execution (e.g., x = 5).

5. Conversion of Temperature from Celsius to Fahrenheit


(3 Marks)
Use the formula:
Fahrenheit=(Celsius×9/5)+32\text{Fahrenheit} = (\
text{Celsius} \times 9/5) + 32
Example:
celsius = float(input("Enter temperature in Celsius:
"))
fahrenheit = (celsius * 9/5) + 32
print(f"Temperature in Fahrenheit: {fahrenheit}")

6. Check if the Input Number is Prime or Not (5 Marks)


A prime number is greater than 1 and divisible only by 1 and
itself. Example:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number")
break
else:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")

7. What is Datatype? What is a Loop? Discuss range()


(1+2+2 Marks)
1 Mark: A datatype defines the type of data a variable can
hold (e.g., int, float, str).
2 Marks: A loop repeatedly executes a block of code (e.g.,
for or while).
2 Marks: The range() function generates sequences of
numbers. Example:
for i in range(1, 5):
print(i)

8. Types of Operators in Python (5 Marks)


Operators perform operations on values. Types:
1. Arithmetic (+, -, *, /)
2. Relational (>, <, ==)
3. Logical (and, or, not)
4. Bitwise (&, |, ^)
5. Assignment (=, +=, -=).

9. Conditional Statements with Example (5 Marks)


Conditional statements (if, if-else, if-elif-else) direct
program flow. Example:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

10. Difference Between Function and Method (5 Marks)


Aspect Function Method
A function that is associated with a
A standalone block of reusable code
Definition class and operates on its object or class
that performs a specific task.
data.
Defined using the def keyword
Defined within a class and typically
Declaration followed by the function name and
includes self as the first parameter.
parentheses.
Called by its name with arguments (if Called using the dot (.) operator on an
Invocation
required). object or class instance.
Operates independently of any object orOperates on the data of the object or
Scope
class. class it is associated with.
Usually operates on the instance of a
Can return values and be invoked from
Return class and can return data from that
anywhere in the program.
instance.
11. Recursive, map(), lambda(), and filter() (5 Marks)
1. recursive(): A recursive function is one that calls itself to solve
smaller instances of the same problem until a base case is
reached.
2. map(): The map() function applies a given function to each item
in an iterable (like a list) and returns an iterator of the results.
3. lambda(): A lambda function is an anonymous, inline function
defined using the lambda keyword, often used for simple, short
operations.
4. filter(): The filter() function filters elements from an iterable
based on a given condition, returning an iterator of the filtered
results.

12. Loop, Operator, List, Tuples (5 Marks)


1. Loop: A loop repeatedly executes a block of code as long as a
specified condition is true.
2. Operator: An operator performs operations on variables or
values, such as arithmetic, logical, or comparison operations.
3. List: A list is a mutable, ordered collection of items that can
contain elements of different types.
4. Tuple: A tuple is an immutable, ordered collection of items,
useful for storing data that should not be modified.

13. __init__ Function (5 Marks)


Constructor (__init__) in Python:

1. Purpose: The constructor initializes an object's attributes when


it is created, ensuring the object starts with a valid state.
2. Definition: It is defined using the __init__() method inside a
class.
3. Parameters: The constructor accepts self as its first parameter,
followed by any other parameters needed to initialize object
attributes.
4. Automatic Invocation: The constructor is automatically called
when an object of the class is instantiated.
5. Role in OOP: It helps in creating objects with specific
attributes, allowing for object consistency and proper
initialization.
Example:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

14. Break Statement in For Loop (5 Marks)


 Purpose of break:
The primary purpose of the break statement is to provide control over
loop execution, allowing you to exit the loop early when a specific
condition is met.

 Syntax:
The break statement is placed inside the loop, typically within an if
statement to check for a condition. Once that condition is satisfied,
break immediately stops the loop.

 When to Use:
It is commonly used in situations where searching for a specific item
in a collection or performing a task has been completed, and
continuing the loop is unnecessary.

 Loop Exit:
When the break is executed, the loop stops, and the program
continues with the next statement after the loop block.

 Use Case:
The break statement helps improve efficiency by avoiding
unnecessary iterations and enabling early termination, which can be
important in scenarios like searching or processing user input.

15. Is elif a Nested if Statement? (3/5 Marks)


3 Marks: No, elif is not a nested if statement. It is part of the same level
of conditional checks in a sequence of if, elif, and else statements. The elif
is used to check additional conditions if the previous if condition is False,
while else serves as a fallback when none of the conditions are met. In
contrast, a nested if statement occurs when an if statement is placed inside
another if or elif, making the condition dependent on the outer condition.

5 Marks: No, elif is not a nested if statement in Python. The elif


statement is part of a sequence of conditional checks used in
conjunction with if and else. It stands for "else if" and allows for
multiple conditions to be tested one after the other in a clear, non-
nested manner. When an if condition evaluates to False, the program
moves to the next elif condition. If all if and elif conditions fail, the
else block is executed (if defined).

In contrast, a nested if occurs when one if statement is placed inside


another if statement, meaning the second condition is dependent on
the first one being true.

16. What is a Class? How to Create One? (3/5 Marks)


3 Marks: What is a class? How are classes created in Python?
1. Class Definition: A class is defined using the class
keyword followed by the class name.
2. Attributes: Attributes are variables that define the
properties of objects in the class.
3. Methods: Methods are functions that define behaviors
and operations of the class.
5 Marks: What is a class? How are classes created in Python?
1. Class Definition: A class is defined using the class keyword
followed by the class name.
2. Attributes: Attributes are variables that define the properties of
objects in the class.
3. Methods: Methods are functions that define behaviors and
operations of the class.
4. Object Creation: Objects are created by calling the class name,
which initializes them.
5. Constructor (__init__): The constructor method initializes the
attributes of the class when an object is created.

17. Define Function in Python (3/5 Marks)


3 Marks: Define function in Python
1. Function Definition: A function is defined using the def
keyword, followed by the function name and
parentheses.
2. Parameters: Functions can accept parameters (inputs)
that allow them to perform tasks using specific data.
3. Return Statement: A function may return a value using
the return statement, which can be used in expressions or
stored in variables.
5 Marks: Define function in Python

1. Function Definition: A function is defined using the def


keyword, followed by the function name and
parentheses.
2. Parameters: Functions can accept parameters (inputs)
that allow them to perform tasks using specific data.
3. Return Statement: A function may return a value using
the return statement, which can be used in expressions or
stored in variables.
4. Modularity: Functions allow you to write reusable
blocks of code, improving modularity and code
organization.
5. Scope: Variables defined inside a function have local
scope, meaning they exist only within the function.

18. For Loop with Example (5 Marks)


Explain for loop with a suitable program
1. Iteration: A for loop allows you to iterate over a
sequence, such as a list, tuple, or string.
2. Syntax: The basic syntax of a for loop is for item in
sequence:, where item is each element of the sequence.
3. Execution: The loop executes the block of code inside it
for each item in the sequence.
4. Range: You can use range() to generate a sequence of
numbers for iteration.
5. Control: You can control the loop using break, continue,
or else for further functionality.
Example:
for i in range(5):
print(i)
In this example, the for loop iterates over numbers from 0 to 4
and prints each number.

19. What is a Dictionary? (3/5 Marks)


3 Marks:
1. Definition: A dictionary is an unordered collection of
data in Python, stored as key-value pairs.
2. Key-Value Pair: Each item in the dictionary consists of
a key and its associated value, where the key must be
unique.
3. Mutable: Dictionaries are mutable, meaning you can
change their content (add, remove, or modify items).

5 Marks:

4. Definition: A dictionary is an unordered collection of


data in Python, stored as key-value pairs.
5. Key-Value Pair: Each item in the dictionary consists of
a key and its associated value, where the key must be
unique.
6. Mutable: Dictionaries are mutable, meaning you can
change their content (add, remove, or modify items).
7. Accessing Values: You can access dictionary values by
referring to their keys inside square brackets or using
the .get() method.
8. Use Case: Dictionaries are ideal for cases where you
need fast lookups based on a unique key.

Example:
student = {"name": "John", "age": 20, "major": "Computer
Science"}
print(student["name"]) # Output: John
In this example, the dictionary student contains key-value
pairs like "name": "John", where "name" is the key and "John"
is the value.

20. Constructor Overriding (5 Marks)


Discuss the constructor overriding in Python
1. Constructor in Python: The __init__() method is used
to initialize object attributes when a class is instantiated.
2. Constructor Overriding: In a subclass, the __init__()
method can be redefined, effectively overriding the
parent class’s constructor.
3. Customization: Overriding allows a subclass to modify
the initialization process and add or change the attributes
as needed.
4. Use of super(): The super() function is used in the
subclass constructor to call the parent class’s constructor
and ensure proper initialization of inherited attributes.
5. Inheritance of Attributes: Constructor overriding
ensures that both parent and child class attributes are
initialized correctly, combining functionality from both
classes.

21. Polymorphism in Operators (5 Marks)


Discuss polymorphism in operators
1. Polymorphism Concept: Polymorphism allows objects
of different types to be treated as objects of a common
base type, enabling different behaviors in the same
interface.
2. Operator Overloading: In Python, polymorphism can
be implemented by overloading operators. This means
that the behavior of operators like +, -, or * can be
customized for user-defined classes.
3. Custom Behavior: By overloading an operator, you
define how it behaves when applied to objects of your
class, which provides flexibility and reusability.
4. Method Resolution: When an overloaded operator is
used, Python resolves the correct method to call based on
the type of operands involved.
5. Code Clarity: Polymorphism in operators enhances code
readability and makes it easier to work with objects in a
natural way without needing explicit function calls.

22. Overlapping Methods (3/5 Marks)


3 Marks:
1. Overloading Concept: Method overlapping refers to
defining multiple methods with the same name in a class,
where each method differs in the number or type of
parameters.
2. Multiple Signatures: The same method name can be
used to perform different tasks depending on the
arguments passed to it.
3. Customization: Overlapping methods allow you to
handle various input types or argument counts in a
flexible way.

5 Marks:

4. Overloading Concept: Method overlapping refers to


defining multiple methods with the same name in a class,
where each method differs in the number or type of
parameters.
5. Multiple Signatures: The same method name can be
used to perform different tasks depending on the
arguments passed to it.
6. Customization: Overlapping methods allow you to
handle various input types or argument counts in a
flexible way.
7. Dynamic Dispatch: The method invoked is determined
at runtime based on the argument types, ensuring the
correct version is executed.
8. Limitations: Python does not support method
overloading directly like other languages (e.g., Java or
C++). Instead, it uses default arguments or *args and
**kwargs to achieve similar behavior.

You might also like