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

PRA7

The document provides an overview of creating and manipulating lists in Python, including examples of lists with integers, strings, and mixed data types. It covers list creation using the list() constructor, accessing elements, adding, updating, and removing elements from lists. The document includes code snippets demonstrating each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

PRA7

The document provides an overview of creating and manipulating lists in Python, including examples of lists with integers, strings, and mixed data types. It covers list creation using the list() constructor, accessing elements, adding, updating, and removing elements from lists. The document includes code snippets demonstrating each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

.

//Creating a List
# List of integers
a = [1, 2, 3, 4, 5]

# List of strings
b = ['apple', 'banana', 'cherry']

# Mixed data types


c = [1, 'hello', 3.14, True]

print(a)
print(b)
print(c)

//Using list() Constructor

# From a tuple
a = list((1, 2, 3, 'apple', 4.5))

print(a)

//Creating List with Repeated Elements


# Create a list [2, 2, 2, 2, 2]
a = [2] * 5

# Create a list [0, 0, 0, 0, 0, 0, 0]


b = [0] * 7

print(a)
print(b)

//Accessing List Elements


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

# Access first element


print(a[0])

# Access last element


print(a[-1])

//Adding Elements into List


# Initialize an empty list
a = []

# Adding 10 to end of list


a.append(10)
print("After append(10):", a)

# Inserting 5 at index 0
a.insert(0, 5)
print("After insert(0, 5):", a)

# Adding multiple elements [15, 20, 25] at the end


a.extend([15, 20, 25])
print("After extend([15, 20, 25]):", a)

//Updating Elements into List


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

# Change the second element


a[1] = 25

print(a)

//Removing Elements from List


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

# Removes the first occurrence of 30


a.remove(30)
print("After remove(30):", a)

# Removes the element at index 1 (20)


popped_val = a.pop(1)
print("Popped element:", popped_val)
print("After pop(1):", a)

# Deletes the first element (10)


del a[0]
print("After del a[0]:", a)

You might also like