KEMBAR78
Lists & Tuples in Python | PDF | Computing | Computer Programming
0% found this document useful (0 votes)
4 views5 pages

Lists & Tuples in Python

Uploaded by

ehtsalman
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)
4 views5 pages

Lists & Tuples in Python

Uploaded by

ehtsalman
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/ 5

Lists in Python

 Definition: Ordered, mutable (can change), allows duplicates, defined using


[ ].
 Use in AI/ML: To store datasets, features, predictions, etc.

Example & Operations:

# Creating a list
fruits = ["apple", "banana", "cherry", "banana"]
print("Original List:", fruits)

# Accessing elements
print("First Element:", fruits[0])
print("Last Element:", fruits[-1])

# Adding elements
fruits.append("orange")
print("After Append:", fruits)

fruits.insert(1, "mango")
print("After Insert:", fruits)

# Removing elements
fruits.remove("banana") # removes first occurrence
print("After Remove:", fruits)

popped = fruits.pop() # removes last element


print("Popped Element:", popped)
print("After Pop:", fruits)

# Updating elements
fruits[0] = "kiwi"
print("After Update:", fruits)

# Slicing
print("Sliced List:", fruits[0:2])

# List operations
numbers = [2, 4, 6, 8]
print("Sum:", sum(numbers))
print("Max:", max(numbers))
print("Min:", min(numbers))
print("Sorted:", sorted(numbers))
Tuples in Python
 Definition: Ordered, immutable (cannot change), allows duplicates, defined
using ( ).
 Use in AI/ML: For fixed data (like coordinates, RGB values, or database
keys).

# Creating a tuple

colors = ("red", "green", "blue", "green")

print("Tuple:", colors)

# Accessing elements

print("First Element:", colors[0])

print("Last Element:", colors[-1])

# Tuple operations

print("Count of 'green':", colors.count("green"))

print("Index of 'blue':", colors.index("blue"))

# Nested tuple

point = (10, 20, (30, 40))

print("Nested Tuple:", point)

print("Access nested element:", point[2][0])


# Concatenation

t1 = (1, 2, 3)

t2 = (4, 5, 6)

print("Concatenated Tuple:", t1 + t2)

# Repetition

print("Repetition:", t1 * 2)

# Conversion between list & tuple

list_from_tuple = list(colors)

tuple_from_list = tuple(fruits)

print("Tuple to List:", list_from_tuple)

print("List to Tuple:", tuple_from_list)

Difference Between List & Tuple


Feature List [ ] Tuple ( )
Mutability Mutable (can change) Immutable (cannot change)
Performanc Slower (more flexible) Faster (less flexible)
e
Use case Dynamic data (datasets, Fixed data (coordinates,
features) configs)

Applications of Lists & Tuples in AI context:

 Lists are used for storing training samples, predictions, dynamic data.
 Tuples are used for fixed values like coordinates, weights, RGB pixels, database
records.

You might also like