filmov
tv
Lecture 5 : Tuples in Python | Python Full Course Free

Показать описание
Python Tuples
A tuple in Python is a collection of items that is:
Ordered: The items have a defined order.
Immutable: Once a tuple is created, its elements cannot be changed, added, or removed.
Allows Duplicates: Like lists, tuples can have repeated items.
Creating a Tuple
Tuples are defined using parentheses () with items separated by commas.
Examples:
python
Copy code
# Empty tuple
empty_tuple = ()
# Tuple with one item (comma is required)
single_item = (5,)
# Tuple with multiple items
numbers = (1, 2, 3, 4)
fruits = ("apple", "banana", "cherry")
# Mixed data types
mixed = (10, "hello", True, 3.14)
# Nested tuple
nested = (1, 2, (3, 4))
Accessing Tuple Elements
You can access tuple elements using their index, similar to lists. Indexing starts at 0.
Examples:
python
Copy code
fruits = ("apple", "banana", "cherry")
# Accessing elements
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: cherry
# Negative indexing (from the end)
print(fruits[-1]) # Output: cherry
Immutable Nature
Tuples cannot be modified after creation, which means:
No adding or removing items.
No changing existing items.
However, you can:
Reassign the entire tuple:
python
Copy code
fruits = ("apple", "banana")
fruits = ("cherry", "mango") # Reassign a new tuple
Modify mutable elements within a tuple: If a tuple contains mutable objects (like lists), those objects can be changed.
python
Copy code
nested = (1, [2, 3], 4)
nested[1][0] = 99
print(nested) # Output: (1, [99, 3], 4)
Tuple Operations
Length of a Tuple:
python
Copy code
print(len(fruits)) # Output: 3
Check if an Item Exists:
python
Copy code
print("apple" in fruits) # Output: True
Concatenate Tuples:
python
Copy code
tuple1 = (1, 2)
tuple2 = (3, 4)
combined = tuple1 + tuple2
print(combined) # Output: (1, 2, 3, 4)
Repeat a Tuple:
python
Copy code
print((1, 2) * 3) # Output: (1, 2, 1, 2, 1, 2)
Unpacking a Tuple
You can assign the items of a tuple to variables directly.
Example:
python
Copy code
fruits = ("apple", "banana", "cherry")
a, b, c = fruits
print(a) # Output: apple
print(b) # Output: banana
print(c) # Output: cherry
# Using *
numbers = (1, 2, 3, 4, 5)
x, *y, z = numbers
print(x) # Output: 1
print(y) # Output: [2, 3, 4]
print(z) # Output: 5
Tuple Methods
Tuples have two built-in methods:
Method Description
count(x) Returns the number of times x appears in the tuple
index(x) Returns the first index of x
Example:
python
Copy code
numbers = (1, 2, 2, 3, 4, 2)
When to Use Tuples
When you need an immutable collection of items.
When you want to ensure the data cannot be modified by mistake.
For faster lookups compared to lists in certain scenarios.
Summary Table
Feature Tuple
Ordered Yes
Mutable No
Duplicates Allowed
Indexing Yes (positive and negative)
Common Uses Fixed data, coordinates, constants
Would you like detailed examples for any specific tuple operations? 😊
#python #tuples #pythontuples #tuplesinpython #pythonlist #pythonlists #programming #pythonprogramming #listsinpython #lists #pythonforbeginners #pythonforbegginer #programmingtutorial #pythontutorial #tutorial #pythonbasics #pythonbasicstoadvance #list #learncoding #pythonprogramming #pythonprojects #python3 #pythonforeveryone #coding #java #javascript #programmer #developer #html #snake #coder #code #computerscience #technology #css #machinelearning #pythonprogramming #linux #ballpython #php #datascience #reptile #snakes #reptiles #software #webdevelopment #webdeveloper #tech #codinglife #softwaredeveloper #artificialintelligence #webdesign #deeplearning #pythonregius #ballpython #pythoncode #ballpythonmorph #bigdata #reactjs #pythondeveloper #pythonlearning #pythoncode #pythonprogrammer #datastructure #pythonlove #pythonprogramminglanguage #codingbootcamp #appdeveloper #bigdata #pythontutorial
A tuple in Python is a collection of items that is:
Ordered: The items have a defined order.
Immutable: Once a tuple is created, its elements cannot be changed, added, or removed.
Allows Duplicates: Like lists, tuples can have repeated items.
Creating a Tuple
Tuples are defined using parentheses () with items separated by commas.
Examples:
python
Copy code
# Empty tuple
empty_tuple = ()
# Tuple with one item (comma is required)
single_item = (5,)
# Tuple with multiple items
numbers = (1, 2, 3, 4)
fruits = ("apple", "banana", "cherry")
# Mixed data types
mixed = (10, "hello", True, 3.14)
# Nested tuple
nested = (1, 2, (3, 4))
Accessing Tuple Elements
You can access tuple elements using their index, similar to lists. Indexing starts at 0.
Examples:
python
Copy code
fruits = ("apple", "banana", "cherry")
# Accessing elements
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: cherry
# Negative indexing (from the end)
print(fruits[-1]) # Output: cherry
Immutable Nature
Tuples cannot be modified after creation, which means:
No adding or removing items.
No changing existing items.
However, you can:
Reassign the entire tuple:
python
Copy code
fruits = ("apple", "banana")
fruits = ("cherry", "mango") # Reassign a new tuple
Modify mutable elements within a tuple: If a tuple contains mutable objects (like lists), those objects can be changed.
python
Copy code
nested = (1, [2, 3], 4)
nested[1][0] = 99
print(nested) # Output: (1, [99, 3], 4)
Tuple Operations
Length of a Tuple:
python
Copy code
print(len(fruits)) # Output: 3
Check if an Item Exists:
python
Copy code
print("apple" in fruits) # Output: True
Concatenate Tuples:
python
Copy code
tuple1 = (1, 2)
tuple2 = (3, 4)
combined = tuple1 + tuple2
print(combined) # Output: (1, 2, 3, 4)
Repeat a Tuple:
python
Copy code
print((1, 2) * 3) # Output: (1, 2, 1, 2, 1, 2)
Unpacking a Tuple
You can assign the items of a tuple to variables directly.
Example:
python
Copy code
fruits = ("apple", "banana", "cherry")
a, b, c = fruits
print(a) # Output: apple
print(b) # Output: banana
print(c) # Output: cherry
# Using *
numbers = (1, 2, 3, 4, 5)
x, *y, z = numbers
print(x) # Output: 1
print(y) # Output: [2, 3, 4]
print(z) # Output: 5
Tuple Methods
Tuples have two built-in methods:
Method Description
count(x) Returns the number of times x appears in the tuple
index(x) Returns the first index of x
Example:
python
Copy code
numbers = (1, 2, 2, 3, 4, 2)
When to Use Tuples
When you need an immutable collection of items.
When you want to ensure the data cannot be modified by mistake.
For faster lookups compared to lists in certain scenarios.
Summary Table
Feature Tuple
Ordered Yes
Mutable No
Duplicates Allowed
Indexing Yes (positive and negative)
Common Uses Fixed data, coordinates, constants
Would you like detailed examples for any specific tuple operations? 😊
#python #tuples #pythontuples #tuplesinpython #pythonlist #pythonlists #programming #pythonprogramming #listsinpython #lists #pythonforbeginners #pythonforbegginer #programmingtutorial #pythontutorial #tutorial #pythonbasics #pythonbasicstoadvance #list #learncoding #pythonprogramming #pythonprojects #python3 #pythonforeveryone #coding #java #javascript #programmer #developer #html #snake #coder #code #computerscience #technology #css #machinelearning #pythonprogramming #linux #ballpython #php #datascience #reptile #snakes #reptiles #software #webdevelopment #webdeveloper #tech #codinglife #softwaredeveloper #artificialintelligence #webdesign #deeplearning #pythonregius #ballpython #pythoncode #ballpythonmorph #bigdata #reactjs #pythondeveloper #pythonlearning #pythoncode #pythonprogrammer #datastructure #pythonlove #pythonprogramminglanguage #codingbootcamp #appdeveloper #bigdata #pythontutorial
Комментарии