0% found this document useful (0 votes)
7 views5 pages

PWP Ut2

The document explains key programming concepts in Python, including local and global variables, classes and objects, file opening modes, lambda functions, and data hiding. It also describes the NumPy and Pandas libraries, highlighting their features and providing examples. Additionally, it details the use of read() and readline() functions in file handling.
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)
7 views5 pages

PWP Ut2

The document explains key programming concepts in Python, including local and global variables, classes and objects, file opening modes, lambda functions, and data hiding. It also describes the NumPy and Pandas libraries, highlighting their features and providing examples. Additionally, it details the use of read() and readline() functions in file handling.
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/ 5

2 Marks

1. Explain Local and Global Variables

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)

2. Define Class and Object in Python

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()

3. List Different Modes of Opening File in Python


 "r" – Read
 "w" – Write (overwrites file)
 "a" – Append
 "b" – Binary mode
 "r+" – Read and write
 "w+" – Write and read (overwrites) & "a+" – Append and read
4. Define Use of Lambda Function in Python
A lambda function in Python is an anonymous function (a function without a name) defined
using the lambda keyword. It's mainly used for creating short, single-line functions.

Syntax:
lambda arguments: expression
Example:-
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5

5. Define Data Hiding Concept. Write Two Advantages.

Data Hiding is a concept in Object-Oriented Programming (OOP) where internal object


details (like variables or methods) are hidden from outside access.

This is done by making variables private using double underscores (__).

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")

7. State the Use of read() and readline() in Python File Handling


read(): Reads the entire file content.
readline(): Reads a single line from the file.

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:

 Efficient array handling (like lists but faster).


 Supports vectorized operations.
 Used in scientific computing, machine learning, and data analysis.
Eaxmple:-
import numpy as np
arr = np.array([1, 2, 3])
print(arr*2) # Output: [2 4 6]

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)

You might also like