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

Python - 2 Marks Solution of Previous PUT - Bharat - Agg

The document provides an overview of key concepts in Python, including reserved keywords, identifiers, linkers, dictionary properties, list cloning, string traversing, file reading modes, the pandas library, and creating 3D arrays. Each concept is defined with essential properties and examples. The information is structured to facilitate understanding of Python's syntax and functionalities.

Uploaded by

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

Python - 2 Marks Solution of Previous PUT - Bharat - Agg

The document provides an overview of key concepts in Python, including reserved keywords, identifiers, linkers, dictionary properties, list cloning, string traversing, file reading modes, the pandas library, and creating 3D arrays. Each concept is defined with essential properties and examples. The information is structured to facilitate understanding of Python's syntax and functionalities.

Uploaded by

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

CO1 What are reserved keywords in Python (BKL :K1-K2 Level)

B CO1 What are identifiers in python. (BKL :K1-K2 Level).


CO2 What is Linker in Python (BKL:KI-K2,Level).
D CO2) Define properties of key in dictionary. (BKL:K1-K2 Level).
E CO3 What is list cloning in python (BKL :K1-K2 Level).
F CO3| What do you mean by traversing a string (BKL: KI-K2 Leve
G CO4 What is the difference between read) and read(n) functions (
H CO4 What is the difference between rt and wt mode (BKL:K1-K
COS What do you mean by pandas in python (BKL:KI-K2 Level)
COS How do you create a 3D array (BKL: K1-K2 Level).
A. What are reserved keywords in Python?
Reserved keywords are predefined words in Python
with special meanings. These words are part of the
Python syntax and cannot be used as identifiers Iike
variable names or function names.

" Keywords define the structure and control flow of a


program.

" They are case-sensitive.

" Youcan use the keyword module to view all Python


keywords.

B. What are identifiers in Python?


Identifiers are names used to identify variables,
functions, classes, or objects in Python. They must
adhere tothe followingrules:

1. Must begin with a letter (A-Z or a-z) or an


underscore

2. Can include letters, digits, and underscores but


cannot use special characters.
3. Must not be a reserved keyword.
4. Case-sensitive, meaning myVar and MyVar are
different.

C. What is Linker in Python?


In Python, a linker refers to the mechanism of linking
libraries, modules, and functions at runtime. Python
does not use a traditional linker as in compiled
languages; instead, it dynamically loads modules and
libraries using its import system. This process
facilitates modularity and allows developers to use
built-in and external libraries seamlessly.

D. Define properties of a key in a dictionary.


A dictionary in Python is a collection of key-value pairs.
Keys have the following properties:

1. Uniqueness: Keys in adictionary must be unique;


duplicate keys overwrite existing values.
2. Immutability: Keys must be of immutable data
types like strings, integers, or tuples.
3. Hashable: Keys must be hashable, meaning they
should have a hash value for efficient lookups.
4. Case-sensitive: Keys distinguish between
uppercase and lowercase letters, so Key and key
are treated as different.
E. What is list cloning in Python?
List cloningrefers to creating a copy of an existing list
in Python. This can be done in multiple ways:

1. Using the copy() method.


2. Using slicing ([:J).
3. Using the list() constructor.

4. Using the copy module.

Example:

Python Copy code

original = [1, 2, 3]
clone1 = original[: ] # Using slicing
clone2= list (or iginal) # Using list()
clone3 = original.copy( ) # Using copy()
print (clone1, clone2, clone3 )

F. What do you mean by traversing a string?


Traversing a string means accessing each character of
a string sequentially, usually in a loop. This is helpful
for operations like searching, replacing, or analyzing
the string.

Example:

Python Copy code

string = "Python!"
# Traversing using a for loop
for char in string:
print ( char )

# Traversing using index


for i in range(len(string) ) :
print(string[i])

G. What is the difference between read() and read (n)


functions?
The read() function reads the entire file content, while
read(n) reads only the first n characters or bytes.

Differences:

1. Amount Read: read() reads the entire content,


while read (n) reads n characters/bytes.
2. Performance: read() can be slower for large files;
read(n) is more memory-efficient.
3. Usage: read() is used when the entire file is
required; read(n) is for partial reading.
4. Output: Both return a string or bytes object.

Example:

Python OCopy code

with open(' example. txt ', 'r) as file:


print(file. read()) # Reads the whole file
file.seek(0) # Reset tothe start
print (file . read (5) ) # Reads first 5
characters
H. What is the difference between r+ and w+ mode?

1. r+: Opens a file for reading and writing. The file


must exist.

2. W+: 0pens a file for reading and writing. It creates


anew file if it doesn't exist or truncates the
existing file.

Differences:

1. File Creation: r+ doesn't create afile if it doesnt


exist; w+ does.

2. Truncation: r+ doesn't truncate the file; w+


truncates it.

3. Default Cursor Position: r+ starts at the beginning;


w+ starts with a cleared file.

4. Use Case: r+ is for modifying existing files; w+ is


for overwriting or creating files.

Example:

Python Copy code

#r+mode
with open(' example.txt', 'r+')as file:
content = file .read()
file .write('Addit ional content')

# w+ mode
with open('example.txt', 'w+') as file:
file .write('New content ' ) # Overwrites
existing content
I. What do you mean by pandas in Python?
Pandas is a powerful library used for data analysis and
manipulation. It provides data structures like Series
and DataFrame for handling tabular data.

Features:

1. Datamanipulation and cleaning.


2. Handling missingdata effectively.
3. Integration with other libraries like NumPy and
Matplotlib.
4. Easy file handling (CSV, Excel, SQL).

Example:

Python Copy code

import pandas as pd
data = {'Name': ['Alice', 'Bob' ], 'Age': [25,
30]}
df = pd. DataFrame (data)
print(df)

J. Howdo you create a 3D array?


In Python, 3D arrays can be created using the NumPy
library. A 3D array has three dimensions (rows,
columns, depth).

Example:

Python OCopy code

import numpyas np
array = np.array(I[[1, 2], [3, 41], [[5, 6], [7,
81]]) # 2x2x2 array
print(array)

You might also like