0% found this document useful (0 votes)
11 views6 pages

PWP 2 MARKS

The document outlines various features of Python, including its ease of use, membership operators, data conversion functions, and the concept of default constructors. It also covers topics such as data hiding, namespaces, file handling methods, and object-oriented features like encapsulation and inheritance. Additionally, it provides examples and comparisons of data structures, comments, and modes of file operations in Python.
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)
11 views6 pages

PWP 2 MARKS

The document outlines various features of Python, including its ease of use, membership operators, data conversion functions, and the concept of default constructors. It also covers topics such as data hiding, namespaces, file handling methods, and object-oriented features like encapsulation and inheritance. Additionally, it provides examples and comparisons of data structures, comments, and modes of file operations in Python.
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/ 6

1. List features of Python.

 Easy to Learn and Use  Interactive Mode  Expressive Languag  Interpreted Language
 Cross-platform Language  Portable  Free and Open Source  Object-Orient

2. Describe membership operators in python.


In Python, membership operators are used to check whether a value exists within a sequence (like a string, list,
tuple, set, or dictionary). There are two membership operators:
1) in
 Meaning: Returns True if the value is found in the sequence.
 Example:
3 in [1, 2, 3] # True
'a' in 'apple' # True
2) not in
 Meaning: Returns True if the value is not found in the sequence.
 Example:
5 not in [1, 2, 3] # True
'z' not in 'apple' # True

3. Describe any two data conversion function.


1) int()
 Purpose: Converts a number or numeric string to an integer.
 Example:
int("42") # Output: 42
int(3.7) # Output: 3
2) float()
 Purpose: Converts a number or numeric string to a floating-point number.
 Example:
float("3.14") # Output: 3.14
float(5) # Output: 5.0

4. With neat example explain default constructor concept in Python.


Default Constructor in Python (Short Explanation)
A default constructor is a constructor that takes no arguments (except self) and initializes object attributes with
default values.

Example:
class Student:
def __init__(self):
self.name = "Unknown"

def show(self):
print("Name:", self.name)

s1 = Student()
s1.show()

Output:
Name: Unknown
5. Describe Multiline comment in python.
A multiline comment in Python is typically written using triple quotes (''' or """) to span multiple lines,
though it's technically a multiline string, not an actual comment. For actual comments, multiple lines starting
with # are used.
Example:
'''
This is a multiline comment
using triple quotes.
'''
# Or
# This is a
# true multiline comment
# using hash symbols.

6. Enlist applications for python programming.


 Web Development:
Frameworks like Django, Flask, and FastAPI are used to build web applications.
 Data Science & Analytics:
Libraries like Pandas, NumPy, and Matplotlib are used for data analysis and visualization.
 Machine Learning & AI:
Libraries like TensorFlow, Keras, and Scikit-learn are used to build machine learning models.
 Automation & Scripting:
Python is used to automate repetitive tasks, manage files, or create scripts for system administration.
 Game Development:
Libraries like Pygame are used to create games.
 Scientific Computing:
Python is used in fields like physics, chemistry, and biology with libraries like SciPy and SymPy.

7. Write the use of elif keyword in python.


The elif keyword in Python is used to check additional conditions in an if-else chain, after an initial if
statement. It allows you to test multiple conditions sequentially.
Example:
if x > 10:
print("Greater than 10")
elif x == 10:
print("Equal to 10")
else:
print("Less than 10")
8. Describe the Role of indentation in python.
In Python, indentation is used to define the structure and grouping of code blocks. It replaces the use of braces
{} found in many other programming languages. Indentation is mandatory and indicates which statements
belong to loops, conditionals, functions, classes, and other control structures. Consistent indentation improves
readability and is required for the program to execute correctly—incorrect or inconsistent indentation will result
in an IndentationError.

9. Define Data Hiding concept ? Write two advantages of Data Hiding.


Definition of Data Hiding:
Data Hiding is an object-oriented programming concept where internal object details (like variables or data
members) are hidden from the outside world and can only be accessed through specific methods. It is implemented
using access modifiers (like private or protected) to restrict direct access to the internal state of an object.
Two Advantages of Data Hiding:
1. Improved Security:
By restricting access to sensitive data, it prevents unauthorized or accidental modifications, protecting the
integrity of the object.
2. Encapsulation Support:
It supports encapsulation by keeping the internal implementation hidden, allowing changes without affecting
external code that uses the object.

10.State use of namespace in python.


Use of Namespace in Python:
A namespace in Python is a container that holds names (identifiers) as keys and their corresponding objects (values).
It ensures that names are unique and can be used without conflict. Namespaces help manage variable scope and
avoid naming collisions, especially in larger programs.
Common Uses:
 Organize code into different scopes (e.g., local, global, built-in).
 Prevent naming conflicts between variables or functions.
 Control variable accessibility within functions, classes, or modules.

11.State the use of read() and readline () functions in python file handling.
Use of read() and readline() in Python File Handling:
 read():
Reads the entire content of a file as a single string. You can also specify the number of characters to read.

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


content = f.read()
 readline():
Reads one line at a time from the file. Useful for processing large files line by line.

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


line = f.readline()
12.Explain two ways to add objects / elements to list.
wo Ways to Add Elements to a List in Python:
1. Using append():
Adds a single element to the end of the list.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
2. Using extend():
Adds multiple elements (from another iterable like a list or tuple) to the end of the list.

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]

13.Enlist any four data structures used in python.


Data Structures Used in Python:
1) List 2) Tuple 3) Dictionary 4) Set 5) String (sequence type) 6) Array (from array module)
7) Deque (from collections module) 8) Stack (can be implemented using lists or deque)

14.Give membership operators in python.


Membership Operators in Python:
1. in – Returns True if a value exists in the sequence.

3 in [1, 2, 3] # True

2. not in – Returns True if a value does not exist in the sequence.

4 not in [1, 2, 3] # True

15.Write use of matplotlib package in python.


The matplotlib package in Python is a powerful library used primarily for data visualization. It allows users to create
a wide range of static, interactive, and animated plots and charts.
Uses of matplotlib:
1. Creating 2D Plots:
o Line plots
o Bar charts
o Histograms
o Scatter plots
o Pie charts
2. Customizing Plots:
o Adding titles, labels, legends, and grids
o Controlling axes and figure size
o Using colors, markers, and line styles
3. Saving Figures:
o Exporting plots to image formats such as PNG, PDF, SVG, etc.
4. Interactive Visualization:
o Supports zooming, panning, and updating plots in real-time (especially with matplotlib.pyplot and
tools like %matplotlib notebook in Jupyter)
16.What is data obstruction and data hiding.
 Data Abstraction:
Hiding complex implementation details and showing only essential features to the user.
Example: Using a car without knowing how the engine works.
 Data Hiding:
Restricting access to internal object data to protect it from unauthorized changes.
Example: Making a class variable private in Python.

17.What is dictionary?
Dictionary in Python:
A dictionary is a built-in Python data type that stores data in the form of key-value pairs.
Definition:
A dictionary is used to store multiple items where each item has a key and a value. It is mutable, unordered (as
of Python <3.7), and indexed by keys.
Example:
student = {
"name": "John",
"age": 20,
"grade": "A"
}

18.List membership operators in Python.


List Membership Operators in Python:
Python provides two membership operators to check if an element exists in a list:
1. in Operator
o Returns True if the specified element is present in the list.
o Example:

my_list = [1, 2, 3, 4]
print(3 in my_list) # Output: True
2. not in Operator
o Returns True if the specified element is not present in the list.
o Example:

print(5 not in my_list) # Output: True

19.Compare list and Tuple.


Feature List Tuple
Syntax Created using square brackets [] Created using parentheses ()
Mutability Mutable (can be changed after creation) Immutable (cannot be changed once
created)
Methods Supports many methods (e.g., .append(), .remove(), .pop()) Limited methods (mainly .count()
and .index())
Performance Slower due to mutability and more functionality Faster due to immutability and less
overhead
Usage Suitable for collections that may change Suitable for fixed collections that
shouldn't change
Example my_list = [1, 2, 3] my_tuple = (1, 2, 3)
20.List different object oriented features supported by Python.
 Classes and Objects:
Classes are blueprints for creating objects (instances).
 Encapsulation:
Bundles data and methods, and restricts access to certain parts (via private members).
 Inheritance:
Allows a class to inherit attributes and methods from another class.
 Polymorphism:
Allows different classes to define methods with the same name but different behaviors.
 Abstraction:
Hides complex implementation details and shows only essential features (via abstract classes).
 Composition:
One class contains objects of other classes, showing a "has-a" relationship.

21.State how to perform comments in Python.


 Single-line Comment: Use # to comment a single line.

# This is a comment
 Multi-line Comment: Use triple quotes (''' or """) for multi-line comments.
'''
This is a
multi-line comment
'''

22.Define class and object.


 Class: A class is a blueprint for creating objects, defining attributes and behaviors (methods) that objects
of that class will have.
 Object: An object is an instance of a class, created using the class as a template. It has its own specific
values for the attributes defined in the class.

23.List different modes of opening file in Python.


 'r' – Read mode (default):
Opens the file for reading. If the file does not exist, it raises an error.
 'w' – Write mode:
Opens the file for writing. If the file already exists, it will be overwritten. If it doesn't exist, a new file is
created.
 'a' – Append mode:
Opens the file for appending. Data is written at the end of the file, and if the file does not exist, it creates
a new one.
 'x' – Exclusive creation mode:
Creates a new file. If the file already exists, it raises an error.
 'b' – Binary mode:
Used for reading or writing binary files (e.g., images, videos). Can be combined with other modes (e.g.,
'rb' for reading in binary mode).
 't' – Text mode (default):
Used for text files. Can be combined with other modes (e.g., 'rt' for reading in text mode).

You might also like