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

Programming With Python

The document outlines key concepts in Python programming, including data structures, membership operators, and file handling methods. It explains the use of various functions, features of Python, and differences between lists and tuples. Additionally, it covers data abstraction, data hiding, and the significance of indentation and namespaces.

Uploaded by

adigenshin1611
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views6 pages

Programming With Python

The document outlines key concepts in Python programming, including data structures, membership operators, and file handling methods. It explains the use of various functions, features of Python, and differences between lists and tuples. Additionally, it covers data abstraction, data hiding, and the significance of indentation and namespaces.

Uploaded by

adigenshin1611
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Programming with Python (22616)

(2 marks questions)

1) Four data structures used in Python:

1. List (list)

2. Tuple (tuple)

3. Dictionary (dict)

4. Set (set)

2) Membership operators in Python:

1. in → Checks if a value exists in a sequence.

2. not in → Checks if a value does not exist in a sequence

3) Syntax to sort a list:

list_name.sort() # Sorts the list in ascending order (default)

list_name.sort(reverse=True) #For descending order

4) Use of matplotlib package in Python:

 It is used for data visualization (creating graphs and charts).

 It helps to plot line graphs, bar charts, histograms, scatter plots, etc.

5) Data Abstraction and Data Hiding:

 Data Abstraction → Hides complex details and shows only the necessary features.
(Example: Using a function without knowing its internal working.)

 Data Hiding → Restricts access to internal data using private variables (__var).
(Example: Preventing direct modification of variables in a class.)
6) Syntax of fopen in Python (open function):

file = open("filename.txt", "mode")

example: file = open("data.txt", "r") # Opens file in read mode

Modes:

 "r" → Read

 "w" → Write

 "a" → Append

 "rb", "wb" → Binary read/write

7) What is a dictionary in Python?

 A dictionary is a collection of key-value pairs.

 It is unordered, mutable, and indexed by unique keys.

 Uses {} (curly brackets) for declaration.

Example: my_dict = {"name": "Adi", "age": 21}

print(my_dict["name"]) # Output: Adi

8) Enlist applications for Python programming.

 Web Development (Django, Flask)

 Data Science & Machine Learning (Pandas, TensorFlow)

 Automation & Scripting

 Game Development (Pygame)

 Cybersecurity & Ethical Hacking

9) Write the use of elif keyword in Python.

 The elif keyword (short for else if) is used to check multiple conditions in an if-else
statement. It helps reduce the need for writing multiple if statements, making the
code more readable and efficient.
Example:
num = 10
if num > 10:
print("Greater")
elif num == 10:
print("Equal")
else:
print("Smaller")

10) Describe the role of indentation in Python.

 Python uses indentation (spaces or tabs) to define blocks of code instead of {} like
other languages.
 Incorrect indentation causes errors.

11) Define Data Hiding concept. Write two advantages of Data Hiding.

 Data Hiding means restricting direct access to object data using private variables
(__var).
Advantages:

1. Security – Prevents unauthorized access to data.

2. Encapsulation – Hides implementation details from users.

12) State the use of namespace in Python.

 A namespace is a container for variable names to avoid conflicts.

Example:

x = 5 # Global namespace

def func():

y = 10 # Local namespace

14) State the use of read() and readline() functions in Python file handling.

 read() – Reads the entire file as a single string

example:

file = open("test.txt", "r")

content = file.read()

file.close()

readline() – Reads one line at a time

Example:

file = open("test.txt", "r")

line = file.readline()

file.close()
15) Explain two ways to add objects/elements to a list.

1. Using append() – Adds one element at the end.

Example:

my_list = [1, 2, 3]

my_list.append(4) # [1, 2, 3, 4]

2. Using extend() – Adds multiple elements at once.

Example:

my_list.extend([5, 6]) # [1, 2, 3, 4, 5, 6]

16) Features of Python:

1. Easy to Learn – Simple syntax like English.

2. Interpreted – Executes line by line.

3. Dynamically Typed – No need to declare variable types.

4. Object-Oriented – Supports OOP concepts like classes.

5. Extensive Libraries – Many built-in modules (NumPy, Pandas, etc.).

6. Platform Independent – Runs on Windows, macOS, Linux.

17) Membership Operators in Python:

Used to check if a value exists in a sequence (list, string, etc.).

 in → Returns True if the value is present.

 not in → Returns True if the value is not present.

18) Two Data Conversion Functions:

1. int() → Converts to an integer

x = int("10") # 10

2. str() → Converts to a string

y = str(100) # "100"
19) Default Constructor in Python:

1. A default constructor is a constructor that takes no arguments.

2. automatically runs when an object is created.

20) mkdir() Function:

 Used to create a new directory (folder).

 Example:

import os

os.mkdir("new_folder") # Creates a folder named 'new_folder'

21) Multiline Comments in Python:

1. Triple quotes (""" """ or ''' ''')

"""

This is a

multiline comment

"""

2. Multiple # lines

# This is

# a multiline

# comment

22) Different Modes of Python:

1. Interactive Mode – Runs code directly in Python shell (>>>).

2. Script Mode – Runs saved Python files (.py) using a terminal or IDE.

23) Identity Operators in Python:

Identity operators compare memory locations of objects.

 is → Returns True if both objects refer to the same memory location.

 is not → Returns True if they refer to different memory locations.


24) List and tuple difference between.

LIST TUPLE
1. Lists are mutable Tuples are immutable
2. Lists consume more memory Tuple consumes less memory as compared to
the list
3. Lists have several built-in methods Tuple does not have many built-in methods.
4. The list is better for performing operations, A Tuple data type is appropriate for accessing
such as insertion and deletion. the elements
5. The implication of iterations is Time- The implication of iterations is comparatively
consuming Faster

25) Local and Global Variables:

 Local Variable: Declared inside a function, accessible only in that function.

 Global Variable: Declared outside a function, accessible throughout the program.

You might also like