0% found this document useful (0 votes)
5 views7 pages

Padlo

The document contains a series of questions and answers related to Python programming and data structures, including multiple-choice questions, short answers, and code examples. Key topics include string slicing, immutability of tuples, differences between lists and tuples, and the use of if-else statements. Additionally, it provides practical use cases for tuples and demonstrates how to read a CSV file using Pandas.

Uploaded by

upsciaspcs1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Padlo

The document contains a series of questions and answers related to Python programming and data structures, including multiple-choice questions, short answers, and code examples. Key topics include string slicing, immutability of tuples, differences between lists and tuples, and the use of if-else statements. Additionally, it provides practical use cases for tuples and demonstrates how to read a CSV file using Pandas.

Uploaded by

upsciaspcs1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Here are the answers to your questions:

**Sample MCQ questions**

* Which data structure in Pandas is 2-dimensional?

b) DataFrame

* What is the output of list(range(3))?

b) [0, 1, 2]

* Which operator is used for exponentiation in Python?

b) \*\*

* What does len("Python") return?

b) 6

* Which of the following will raise a TypeError?

c) 3 + '5'

* What does the type() function do?

b) Returns the class/type of a variable

* Which data structure is immutable in Python?

b) tuple [cite: 2]

* Which keyword is used to start a loop in Python?

c) for

* What is the primary data structure in Pandas for 1D data?

b) Series

* What is the output of print("a" \* 3)?

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].

Syntax: `string[start : end]` [cite: 6]

* `start` is the index where slicing begins (inclusive)[cite: 6].

* `end` is the index where slicing ends (exclusive)[cite: 6].

* Indexing starts from 0[cite: 7].

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].

**Write a short Python code using Pandas to:**

* **Read a CSV file named students.csv**

* **Display the first 5 rows**

* **Show the column names**

```python

import pandas as pd

# Read the CSV file

df = pd.read_csv('students.csv') # cite: 8

# Display the first 5 rows

print("First 5 rows of the dataset:")

print(df.head()) # cite: 8

# Show the column names

print("\nColumn names in the dataset:")

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)

# Attempting to modify a tuple will raise a TypeError

# 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.**

| Feature | List | Tuple |

| :----------- | :-------------------------------------- | :-------------------------------------- |

| Mutability | Mutable (can be changed after creation) | Immutable (cannot be changed after creation)
[cite: 15] |

| Syntax | Uses square brackets `[]` | Uses parentheses `()` |

| 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

# Get input from the user

num = int(input("Enter an integer: "))

# Check if the number is even or odd using the modulo operator

if num % 2 == 0:

print(f"The number {num} is Even.")

else:

print(f"The number {num} is Odd.")

```

**Explanation of `if-else` statement:**

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.

* The `if` block is executed if the condition following `if` is true.

* The `else` block is executed if the condition following `if` is 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

# Get input from the user for three numbers


num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

# Find the maximum using if-else statements

if num1 >= num2 and num1 >= num3:

maximum = num1

elif num2 >= num1 and num2 >= num3:

maximum = num2

else:

maximum = num3

print(f"The maximum of the three numbers is: {maximum}")

```

**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].

You might also like