0% found this document useful (0 votes)
0 views3 pages

Python Lab 2

Uploaded by

luvgoswami39
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)
0 views3 pages

Python Lab 2

Uploaded by

luvgoswami39
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
You are on page 1/ 3

List

Lists are used to store multiple items in a single variable.


Lists are one of 4 built-in data types in Python used to store collections of data, the other 3
are Tuple, Set, and Dictionary, all with different qualities and usage.

Lists are created using square brackets:

mylist = ["one", "two", "three"]

Printing List values

print(mylist)

# Demonstration of Lists in Python

# 1. Creating lists
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40, 50]
mixed = ["hello", 100, 3.14, True]

print("Initial Lists:")
print("Fruits:", fruits)
print("Numbers:", numbers)
print("Mixed:", mixed)
print()

# 2. Accessing elements (indexing and slicing)


print("Accessing elements:")
print("First fruit:", fruits[0]) # indexing
print("Last number:", numbers[-1]) # negative indexing
print("First three numbers:", numbers[0:3]) # slicing
print()

# 3. Adding elements
fruits.append("orange") # add at end
numbers.insert(2, 25) # insert at position
print("After adding elements:")
print("Fruits:", fruits)
print("Numbers:", numbers)
print()

# 4. Removing elements
fruits.remove("banana") # remove by value
removed_number = numbers.pop() # remove last element
print("After removing elements:")
print("Fruits:", fruits)
print("Numbers:", numbers)
print("Removed number:", removed_number)
print()

# 5. Updating elements
fruits[1] = "kiwi"
numbers[0] = 5
print("After updating elements:")
print("Fruits:", fruits)
print("Numbers:", numbers)
print()

# 6. Looping through lists


print("Looping through fruits:")
for fruit in fruits:
print(fruit)
print()

# 7. List comprehension
squares = [x**2 for x in range(1, 6)]
print("Squares using list comprehension:", squares)
print()

# 8. Sorting and reversing


numbers.sort()
fruits.sort()
print("Sorted numbers:", numbers)
print("Sorted fruits:", fruits)

numbers.reverse()
fruits.reverse()
print("Reversed numbers:", numbers)
print("Reversed fruits:", fruits)
print()

# 9. Nested lists
nested = [[1, 2], [3, 4], [5, 6]]
print("Nested list:", nested)
print("Access element 3 from nested list:", nested[1][0])

Expected Outputs
Initial Lists:
Fruits: ['apple', 'banana', 'cherry']
Numbers: [10, 20, 30, 40, 50]
Mixed: ['hello', 100, 3.14, True]

Accessing elements:
First fruit: apple
Last number: 50
First three numbers: [10, 20, 30]

After adding elements:


Fruits: ['apple', 'banana', 'cherry', 'orange']
Numbers: [10, 20, 25, 30, 40, 50]

After removing elements:


Fruits: ['apple', 'cherry', 'orange']
Numbers: [10, 20, 25, 30, 40]
Removed number: 50

After updating elements:


Fruits: ['apple', 'kiwi', 'orange']
Numbers: [5, 20, 25, 30, 40]

Looping through fruits:


apple
kiwi
orange

Squares using list comprehension: [1, 4, 9, 16, 25]

Sorted numbers: [5, 20, 25, 30, 40]


Sorted fruits: ['apple', 'kiwi', 'orange']
Reversed numbers: [40, 30, 25, 20, 5]
Reversed fruits: ['orange', 'kiwi', 'apple']

Nested list: [[1, 2], [3, 4], [5, 6]]


Access element 3 from nested list: 3

You might also like