Index
Lists in Python
Difference Between Strings and Lists in Python
List Slicing
List Methods
Tuples in Python
Difference Between Tuples and Lists in Python
Tuple Slicing
Tuple Methods
1. Lists in Python
Definition:
A list in Python is an ordered collection of items (elements) that can be of any data type. Lists are mutable, meaning the elements can be changed after the list has been created.
Syntax:
list_name = [element1, element2, element3, ...]
Examples:
Basic Example:
numbers = [1, 2, 3, 4]
print(numbers)
Output:[1, 2, 3, 4]
Advanced Example:
mixed_list = [1, "Python", [2, 3], {"key": "value"}]
mixed_list[2].append(4)
print(mixed_list)
Output:[1, 'Python', [2, 3, 4], {'key': 'value'}]
Rules:
Lists are mutable, so elements can be added, removed, or changed.
2. Difference Between Strings and Lists in Python
Definition:
Strings are sequences of characters, while lists are sequences of elements that can be of different data types. Strings are immutable, meaning they cannot be changed after creation, whereas lists are mutable.
Syntax:
String Syntax:
string_name = "Hello"
List Syntax:
list_name = [1, 2, 3]
Examples:
Basic Example:
word = "Python" # A string is created
letters = ['P', 'y', 't', 'h', 'o', 'n'] # A list of characters is created
print(word[0]) # Accessing and printing the first character in the string 'word'
print(letters[0]) # Accessing and printing the first element in the list 'letters'
Output:
P
P
Advanced Example:
word = "Python"
letters = list(word)
letters[0] = 'J'
print(word) # Strings are immutable
print("".join(letters)) # Lists can be modified and joined back into a string
Output:
Python
Jython
Rules:
Strings are immutable; lists are mutable. Lists can hold various data types, while strings are limited to characters.
3. List Slicing
Definition:
List slicing allows you to extract a portion of a list by specifying a start, stop, and optionally a step.
Syntax:
list_variable[start:end:step]
Examples:
Basic Example:
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4])
Output:[1, 2, 3]
Advanced Example:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8]
print(numbers[::2])
Output:[0, 2, 4, 6, 8]
Rules:
Omitting the start index defaults to 0; omitting the end index slices to the end of the list.
4. List Methods
Definition:
List methods are built-in functions that perform operations on lists, such as adding, removing, or modifying elements.
Common List Methods:
append(): Adds an element to the end of the list.
extend(): Adds all elements of an iterable to the end of the list.
insert(): Inserts an element at a specified index.
remove(): Removes the first occurrence of a specified element.
pop(): Removes and returns an element at a specified index.
Syntax:
list_name.method(arguments)
Examples:
Basic Example:
fruits = ['apple', 'banana', 'cherry']
fruits.append('date') print(fruits)
Output:['apple', 'banana', 'cherry', 'date']
Advanced Example:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'blueberry')
fruits.remove('cherry') print(fruits)
Output:['apple', 'blueberry', 'banana']
Rules:
Lists are mutable, allowing you to modify them using these methods.
5. Tuples in Python
Definition:
A tuple in Python is an ordered collection of elements similar to a list, but unlike lists, tuples are immutable. Once created, the elements within a tuple cannot be changed.
Syntax:
tuple_name = (element1, element2, element3, ...)
Examples:
Basic Example:
coordinates = (10, 20)
print(coordinates)
Output:(10, 20)
Advanced Example:
nested_tuple = (1, (2, 3), [4, 5])
print(nested_tuple[1][1])
Output:3
Rules:
Tuples are immutable, meaning you cannot modify the elements after creation.
6. Difference Between Tuples and Lists in Python
Definition:
The primary difference between tuples and lists is mutability. Lists are mutable (can be changed), whereas tuples are immutable (cannot be changed).
Syntax:
Tuple Syntax:
tuple_name = (1, 2, 3)
List Syntax:
list_name = [1, 2, 3]
Examples:
Basic Example:
fruits_tuple = ('apple', 'banana', 'cherry')
fruits_list = ['apple', 'banana', 'cherry'] print(fruits_tuple)
print(fruits_list)
Output:('apple', 'banana', 'cherry')['apple', 'banana', 'cherry']
Advanced Example:
fruits_list = ['apple', 'banana', 'cherry']
fruits_tuple = ('apple', 'banana', 'cherry')
fruits_list[1] = 'blueberry'
# fruits_tuple[1] = 'blueberry' # This line would cause an error print(fruits_list) print(fruits_tuple)
Output:['apple', 'blueberry', 'cherry']('apple', 'banana', 'cherry')
Rules:
Lists are mutable and can be changed after creation; tuples are immutable and cannot be modified after creation.
7. Tuple Slicing
Definition:
Tuple slicing allows you to extract a portion of a tuple by specifying a start, stop, and optionally a step.
Syntax:
tuple_variable[start:end:step]
Examples:
Basic Example:
numbers = (0, 1, 2, 3, 4, 5)
print(numbers[1:4])
Output:(1, 2, 3)
Advanced Example:
numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8)
print(numbers[::2])
Output:(0, 2, 4, 6, 8)
Rules:
Slicing a tuple returns a new tuple. The original tuple remains unchanged because tuples are immutable.
8. Tuple Methods
Definition:
Tuple methods are limited compared to list methods due to the immutability of tuples. The primary methods available are count()
and index()
.
Common Tuple Methods:
count(): Returns the number of times a specified value appears in the tuple.
index(): Returns the index of the first occurrence of a specified value.
Syntax:
tuple_name.method(arguments)
Examples:
Basic Example:
numbers = (1, 2, 3, 2, 4)
print(numbers.count(2))
Output:2
Advanced Example:
numbers = (10, 20, 30, 40, 50)
print(numbers.index(30))
Output:2
Rules:
Tuples, being immutable, have a limited set of methods
In this edition of FM University, we've explored the foundational elements of Python's data structures, focusing on lists and tuples. Understanding the differences between these two structures, along with their methods and slicing techniques, is essential for writing efficient and effective Python code. By mastering these concepts, you can handle a wide range of programming tasks, from simple data manipulation to more complex data processing.
-Vishal Rajput