Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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.
| Syntax | Description |
|---|---|
import library_name | Imports the whole library |
import library_name as alias | Imports 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 Structure | Description | Example |
|---|---|---|
| List | Ordered, changeable, allows duplicates | [1, 2, 3] |
| Tuple | Ordered, unchangeable, allows duplicates | (1, 2, 3) |
| Set | Unordered, no duplicates, changeable | {1, 2, 3} |
| Dictionary | Key/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:
myList.append(10)myList.remove("apple")myList.sort()myList.reverse()Got it 👍 The page you shared explains List operations in Python. Here’s a clean and clear version of the content:
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.
my_list = [10, 20, 30, 40]
print(my_list[0])
# Output: 10
my_list[1] = 25
print(my_list)
# Output: [10, 25, 30, 40]
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2)
# Output: [1, 2, 3, 4]
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.