PWP Ut2
PWP Ut2
Local Variable: A variable declared inside a function and accessible only within that method
Global Variable: A variable declared outside any function and accessible throughout the
program.
Example:-
x = 10 # Global variable
def func():
y = 5 # Local variable
print(x + y)
Class: A blueprint for creating objects. It defines properties (attributes) and methods
(functions).
Object: An instance of a class that can access its attributes and methods.
Example:-
class Car:
def drive(self):
print("Driving")
mycar = Car() # Object
mycar.drive()
Syntax:
lambda arguments: expression
Example:-
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5
It ensures that critical data is not accessed or modified directly from outside the class.
Advantages:
1. Enhances security by hiding sensitive data.
2. Helps in encapsulation and protects object integrity.
Example:-
class Student:
def __init__(self):
self.__marks = 90 # private variable
def get_marks(self):
return self.__marks
s = Student()
print(s.get_marks()) # ✅ Correct way
# print(s.__marks) # ❌ Error: cannot access private variable
6. Write the Syntax of fopen (in Python it's open())
In Python, the correct function used for opening files is open(), not fopen (which is used in
C/C++).
Syntax:
file_object = open("filename", "mode")
Parameters:
"filename" → Name of the file you want to open (e.g., "data.txt")
"mode" → Mode in which the file is opened
Common modes:
o "r" → Read (default)
o "w" → Write (creates new or overwrites existing)
o "a" → Append
o "b" → Binary
o "r+", "w+", "a+" → Read + Write combinations
Example:
f = open("example.txt", "r")
Example:-
f = open("file.txt", "r")
print(f.read()) # Whole file
print(f.readline()) # First line
4 Marks
1. Describe Numpy and Pandas with Example
NumPy:
Definition: NumPy is a powerful Python library used for numerical computing. It
provides support for multi-dimensional arrays, mathematical operations, and high-
performance matrix computations.
Key Features:
Pandas:
Definition:
Pandas is a high-level data manipulation tool. It is built on NumPy and provides two
main data structures:
Series → 1D labeled array
DataFrame → 2D table (like an Excel sheet)
Key Features:
Data cleaning and manipulation.
Reading/writing CSV, Excel, SQL.
Easy handling of missing data.
Example:-
import pandas as pd
data = {"Name": ["Alvin", "Shweta"], "Age": [19, 20]}
df = pd.DataFrame(data)
print(df)