Python Arrays (Lists)
1. What is an Array (List) in Python?
In Python, instead of classical arrays, the most commonly used data structure is the List.
Lists are dynamic and mutable data structures that allow us to store multiple values in a
single variable.
Example:
numbers = [10, 20, 30, 40]
Another Example:
names = ["Ali", "Ayşe", "Ahmet"]
2. Data Type of Lists
In Python, the data type of an array is 'list'.
The output of type([1,2,3]) is: <class 'list'>.
Lists can be heterogeneous, meaning they can store different data types at the same time.
Example:
data = [10, "Hello", 3.5, True]
print(type(data))
3. Ways to Create Lists
- Empty list: my_list = []
- List with elements: fruits = ["apple", "banana", "strawberry"]
- Mixed data type: data = [10, "Ali", 3.14, True]
Extra Example:
numbers = list((1, 2, 3, 4))
4. Index Logic
In Python lists, indexing starts from 0.
Example:
letters = ["a", "b", "c", "d"]
print(letters[0]) # a
print(letters[3]) # d
5. Negative Index (Reverse Access)
With negative indexing, you can access elements from the end of the list.
Example:
nums = [10, 20, 30, 40]
print(nums[-1]) # 40
print(nums[-2]) # 30
6. List Slicing
Slicing allows us to get a specific portion of a list.
Example:
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[2:]) # [30, 40, 50]
print(numbers[::2]) # [10, 30, 50]
7. Modifying List Elements
Lists are mutable (changeable) structures.
Example:
my_list = [1, 2, 3]
my_list[1] = 100
print(my_list) # [1, 100, 3]
Another Example:
my_list[0] = 50
8. Adding Elements to a List
append() -> Adds an element to the end of the list
insert(index, value) -> Adds an element at a specific position
extend() -> Adds another list
Example:
lst = [1, 2, 3]
[Link](4)
[Link](1, 10)
[Link]([5, 6])
print(lst)
9. Removing Elements from a List
remove() -> Removes by value
pop() -> Removes by index
del -> Deletes the element completely
Example:
lst = [10, 20, 30, 40]
[Link](20)
[Link](1)
del lst[0]
print(lst)
10. Using Lists with Loops
For loop:
numbers = [1, 2, 3, 4]
for element in numbers:
print(element)
Using index:
for i in range(len(numbers)):
print(numbers[i])
11. List Length and Basic Functions
len(list) -> Number of elements
sum(list) -> Sum (for numerical lists)
min(list), max(list) -> Minimum and maximum values
Example:
nums = [5, 2, 9, 1]
print(len(nums)) # 4
print(sum(nums)) # 17
print(min(nums)) # 1
print(max(nums)) # 9
12. List Methods (Most Important)
append(), remove(), pop(), sort(), reverse(), count(), index(), clear(), copy()
Example:
lst = [3, 1, 2]
[Link]()
print(lst) # [1, 2, 3]
[Link]()
print(lst) # [3, 2, 1]
13. List Copying and Reference Logic
a = [1,2,3]
b = a # Same reference
Correct copying:
b = [Link]()
Example:
[Link](4)
print(a) # [1,2,3] (if copied correctly)
14. Homogeneous and Heterogeneous Lists
Homogeneous: Lists containing the same data type
Example:
nums = [1, 2, 3, 4]
Heterogeneous: Lists containing different data types
Example:
mixed = [1, "Python", 3.14, False]
15. Advantages of Python Lists
- Dynamic size
- Can store different data types
- Easy to use
- Powerful built-in methods
- Widely used in data science and artificial intelligence
Example Use Case:
shopping_list = ["milk", "bread", "eggs"]
16. Summary
Python array = list
Defined with square brackets []
Index starts from 0
Mutable and dynamic
One of the most fundamental and important data structures in programming