0% found this document useful (0 votes)
8 views5 pages

Python List and Dictionary Methods Explained

The document explains various list and dictionary methods in Python, including negative indexing, slicing, and functions like append(), remove(), pop(), insert(), and sort(). It also covers the use of operators in lists, basic statistics calculations, and the differences between tuples and lists. Additionally, it discusses dictionary methods and the distinction between lists and dictionaries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Python List and Dictionary Methods Explained

The document explains various list and dictionary methods in Python, including negative indexing, slicing, and functions like append(), remove(), pop(), insert(), and sort(). It also covers the use of operators in lists, basic statistics calculations, and the differences between tuples and lists. Additionally, it discusses dictionary methods and the distinction between lists and dictionaries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1.

Explain negative indexing, slicing, index(), append(), remove(), pop(), insert() and sort()

with syntax and example.

1. Negative Indexing:

- Access elements from the end using negative numbers.

- Example:

my_list = [10, 20, 30]

print(my_list[-1]) # Output: 30

2. Slicing:

- Extract part of a list: list[start:end]

- Example:

my_list = [1, 2, 3, 4, 5]

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

3. index():

- Returns the index of the first matching item.

- Syntax: [Link](value)

- Example: [10, 20, 30].index(20) -> 1

4. append():

- Adds item to the end.

- Example:

l = [1, 2]

[Link](3) -> [1, 2, 3]

5. remove():
- Removes first occurrence of a value.

- Example:

l = [1, 2, 3]

[Link](2) -> [1, 3]

6. pop():

- Removes and returns item at given index.

- Example:

l = [10, 20, 30]

[Link](1) -> 20, list becomes [10, 30]

7. insert():

- Inserts item at given index.

- Syntax: [Link](index, value)

- Example:

[Link](1, 100) -> [10, 100, 30]

8. sort():

- Sorts list in ascending order.

- Example:

l = [3, 1, 2]

[Link]() -> [1, 2, 3]

Q2. Use of in and not in operators in list + Program to find mean, variance, std dev

# Operators Example

l = [1, 2, 3]
print(2 in l) # True

print(5 not in l) # True

# Program for Mean, Variance, Std Dev

import math

nums = [10, 20, 30, 40, 50]

n = len(nums)

mean = sum(nums) / n

variance = sum((x - mean) ** 2 for x in nums) / n

std_dev = [Link](variance)

print("Mean =", mean)

print("Variance =", variance)

print("Standard Deviation =", std_dev)

Q3. Explain len(), sum(), max(), min() with examples.

my_list = [10, 20, 30]

print("Length:", len(my_list)) # 3

print("Sum:", sum(my_list)) # 60

print("Max:", max(my_list)) # 30

print("Min:", min(my_list)) # 10

Q4. Explain set() and setdefault() method in dictionary. Program to swap case.

# set()

l = [1, 2, 2, 3]
s = set(l) # {1, 2, 3}

# setdefault()

d = {"name": "John"}

[Link]("age", 25)

# d -> {"name": "John", "age": 25}

# Swap case program

s = "Java"

print([Link]()) # jAVA

Q5. Explain [Link](), [Link]() and list mutability.

import random

# choice()

l = [1, 2, 3]

print([Link](l)) # Random element

# shuffle()

[Link](l)

print(l) # Shuffled list

# Mutable list

l[0] = 100

print(l)

Q6. Define tuple and difference between tuple and list.


Tuple: Immutable, ordered collection. Use ()

List: Mutable, ordered collection. Use []

Example:

t = (1, 2, 3)

l = [1, 2, 3]

l[0] = 10 # Valid

t[0] = 10 # Error (immutable)

Q7. Dictionary methods get(), items(), keys(), values() + List vs Dictionary

student = {"name": "Alice", "age": 20}

print([Link]("name")) # Alice

print([Link]("city", "NA")) # NA

print([Link]()) # dict_items([...])

print([Link]()) # dict_keys([...])

print([Link]()) # dict_values([...])

# List vs Dictionary:

# List: Ordered, accessed by index, e.g. l[0]

# Dict: Unordered, accessed by key, e.g. d["name"]

You might also like