Python Tutorials
Python File Handling
Python Modules
Note: Python has no built-in support for Arrays, but Python Lists can be used instead.
Note: This page shows you how to use the LIST AS A MEMBER, however, in order to work with arrays in Python you will need to enter a library, such as NumPy Library.
Arrays are used to store multiple values for one variation:
Create an array containing car names:
cars = ["Ford", "Volvo", "BMW"]
The same members are a special kind of specialty, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), keeping single variable cars may look like this:
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
However, what if you want to get in the car and find one? And what if you did not have 3 cars, but 300?
The solution is a program!
A list can hold multiple values under one word, and you can access values by referring to the reference number.
Refers to the same members by referring to the reference number.
Get the value of the first array item:
x = cars[0]
Modify the value of the first array item:
cars[0] = "Toyota"
Use the len()
method to restore the length of the same element (number of items in the system).
Return the number of elements in the cars
array:
x = len(cars)
Note: The length of the same members is always higher than the maximum index of the same members.
You can use the for in
loop to disconnect from all features of the same member.
Print each item in the cars
array:
for x in cars:
print(x)
You can use the append()
method to add an item to the same component.
Add one more element to the cars
array:
cars.append("Honda")
You can use the pop()
method to remove an item from the list.
Delete the second element of the cars
array:
cars.pop(1)
You can also use the remove()
method to remove an item from the list.
Delete the element that has the value "Volvo":
cars.remove("Volvo")
Note: The remove()
list method only removes the first occurrence of a specified value.
Python has a set of built-in methods that you can use in lists / programs.
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Note: Python has no built-in support for Arrays, but Python Lists can be used instead.