Chap 2 : List Short Questions Answers

Here is the extracted text from the provided images:


Q.1: write uses libraries and modules.
Ans: In Python, libraries and modules help programmers write code faster, more efficiently, and with fewer errors by providing pre-written code for common tasks.

  • Module: A single Python file (.py) containing functions, classes, or variables
  • Library: A collection of modules grouped to perform related tasks.

Q.2: what is importing and using libraries?
Ans: In Python, importing a library means making its functions and tools available in your program. Libraries contain pre-written code that you can reuse without writing it from scratch.

SyntaxDescription
import library_nameImports the whole library
import library_name as aliasImports the library with a short alias

Examples: import entire library:
1.

import math  
print(math.sqrt(25))   # Output: 5.0

Q.3: what is package structure with example?
Ans: A package in Python is a collection of modules organized in directories. It helps you organize related Python files into a structured and manageable format, especially for large projects.

Example:

math_tools  
   __init__.py  
   add.py  
   subtract.py

Q.4: what is built in data structures?
Ans: Built-in data structures are ready-made, predefined data types that help you store, organize, and manipulate data efficiently.

Common Built-in Data Structures:

Data StructureDescriptionExample
ListOrdered, changeable, allows duplicates[1, 2, 3]
TupleOrdered, unchangeable, allows duplicates(1, 2, 3)
SetUnordered, no duplicates, changeable{1, 2, 3}
DictionaryKey/value pairs, unordered (as of Python 3.6+ insertion ordered){“name”: “Alice”}

Q.5: What is a List with examples?
Ans: A List in Python is a built-in data structure used to store multiple items in a single variable. It is ordered, changeable (mutable), and allows duplicate values.

Example:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # Output: apple

Q.6: What is creating a List?
Ans: Creating a list means declaring a list variable and storing multiple values in it using square brackets [ ].

Syntax:

list_name = [item1, item2, item3, ...]

Q.7: What is accessing list items?
Ans: Accessing list items means retrieving the value of an element in a list using its index (position). Python lists are zero-indexed, meaning the first item is at index 0.

Syntax:

list_name[index]

Examples:

fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[2])   # Output: cherry

Q.8: What is modifying a List?
Ans: Modifying a list means changing, updating, or replacing one or more elements within the list. Since Python lists are mutable, you can change their contents after creation.

Example:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"  
print(fruits)   # Output: ["apple", "orange", "cherry"]

Q.9: Write a short note on methods with example of List.
Ans: Lists in Python are ordered, mutable collections of items. They can store elements of different data types, such as integers, strings, and even other lists.

Common Methods On Lists:

  1. append(item): Adds an item to the end of the list.
    Example: myList.append(10)
  2. remove(item): Removes the first occurrence of the item.
    Example: myList.remove("apple")
  3. sort(): Sorts the list in ascending order (in-place).
    Example: myList.sort()
  4. reverse(): Reverses the list in place.
    Example: myList.reverse()

Got it 👍 The page you shared explains List operations in Python. Here’s a clean and clear version of the content:


Q.10: Write a short note on the List with an example.

Ans: List Operations in Python
A list in Python is a collection of items that is ordered and changeable. You can perform various operations on lists.


1. Creating a List

my_list = [10, 20, 30, 40]

2. Accessing Elements

print(my_list[0])  
# Output: 10

3. Updating Elements

my_list[1] = 25  
print(my_list)  
# Output: [10, 25, 30, 40]

4. Concatenation (combining two lists)

list1 = [1, 2]  
list2 = [3, 4]  
print(list1 + list2)  
# Output: [1, 2, 3, 4]

5. Slicing (getting a part of the list)

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

In short, lists in Python allow you to store multiple values, access them by index, update values, join lists, and extract sublists.


Leave a Reply

Your email address will not be published. Required fields are marked *