Padlo
Padlo
b) DataFrame
b) [0, 1, 2]
b) \*\*
b) 6
c) 3 + '5'
b) tuple [cite: 2]
c) for
b) Series
a) aaa
**Short answer questions**
**What is string slicing in Python? Demonstrate with an example on the string "HelloWorld" to extract
"World".**
String Slicing in Python refers to accessing a part (substring) of the string using indexing and the colon (:)
operator[cite: 4]. It allows us to extract a section of the string based on start and end positions[cite: 5].
Example:
```python
text = "HelloWorld"
slice = text[5:10]
print(slice)
```
Output:
```
World
```
Explanation:
"HelloWorld" has index positions H(0) e(1) l(2) l(3) o(4) W(5) o(6) r(7) l(8) d(9)[cite: 7]. `text[5:10]`
extracts characters from index 5 up to (but not including) index 10, which results in "World"[cite: 7].
```python
import pandas as pd
df = pd.read_csv('students.csv') # cite: 8
print(df.head()) # cite: 8
print(df.columns) # cite: 9
```
**Explain immutability in Python using tuples. Give an example of a tuple and what happens if you try to
modify it.**
Immutability in Python means that once an object is created, its state cannot be changed. Tuples are an
example of an immutable data structure in Python[cite: 15]. Their contents cannot be changed once
defined[cite: 15]. This prevents accidental or unauthorized modification of data[cite: 16].
Example:
```python
my_tuple = (1, 2, 3)
print(my_tuple)
# my_tuple[0] = 5
```
If you try to execute `my_tuple[0] = 5`, Python will raise a `TypeError` because tuples do not support
item assignment.
**Differentiate between list and tuple with at least three points of comparison.**
| Mutability | Mutable (can be changed after creation) | Immutable (cannot be changed after creation)
[cite: 15] |
| Performance | Slower for iteration and lookup | Faster for iteration and lookup [cite: 16] |
| Use Case | For collections of items that may change | For collections of fixed data [cite: 17] |
**Write a Python program to check whether a number entered by the user is even or odd. Explain the
use of the if-else statement.**
```python
if num % 2 == 0:
else:
```
The `if-else` statement is a conditional control flow statement. It allows a program to execute different
blocks of code based on whether a given condition is true or false.
In the example above, `num % 2 == 0` is the condition. If the remainder of `num` divided by 2 is 0
(meaning it's an even number), the code inside the `if` block is executed. Otherwise, the code inside the
`else` block is executed.
**Write a Python program to find the maximum of three numbers entered by the user using if-else.**
```python
maximum = num1
maximum = num2
else:
maximum = num3
```
**Why are tuples considered faster and more secure than lists? Support your answer with two practical
use-cases.**
Tuples are considered faster and more secure than lists in Python for the following reasons:
* **Immutability (Security):** Tuples are immutable, which means their contents cannot be changed
once they are defined[cite: 15, 16]. This immutability prevents accidental or unauthorized modification
of data, making them more "secure" in terms of data integrity[cite: 16].
* **Performance (Speed):** Because tuples are immutable, Python can allocate less memory and
optimize their execution[cite: 16]. This makes them faster than lists for operations like iteration and
lookup[cite: 16].
**Two Practical Use-Cases:**
1. **Storing Fixed Data:** Tuples are ideal for storing data that should not change, such as geographic
coordinates. For example, `coordinates = (19.07, 72.87)` ensures that latitude and longitude remain
constant[cite: 17].
2. **Dictionary Keys:** Tuples can be used as keys in a dictionary, whereas lists cannot[cite: 18]. This is
because dictionary keys must be hashable, and immutability makes tuples hashable[cite: 18]. For
example, `location_data = {(19.07, 72.87): "Mumbai", (28.61, 77.20): "Delhi"}` uses tuples as keys to map
coordinates to cities[cite: 18].