String
datatype.
If I remeber so well, I see we also did a little bit Lists
in our geting started tutorial.
It would be much better for your mastery of this tutorial, if you have gone through it.
Now that we know how to do calculations, and manipulate strings, lets dive into another most interesting python data type, List
.
Array
, is not the same in Python, and it's the best comparisonlist
.
In short, array
s are called list
s in python.list
as a set of different datatypes, separated by commas, enclosed in square brackets.>>> lists = [1,"hello",{},set(),(),[]]
.Lists
, and loll, just as we did with Strings,
in, not in
to use when manipulating python Lists,
for checking whether a given element is in the given list.>>> lists = [1,3,9,"a"]
>>> lists+[8,"b"] # [1,3,9,"a",8,"b"]
>>> lists += ["done"] # [1,3,9,"a","done"]
>>> lists * 2 # [1,3,9,"a",1,3,9,"a"]
>>> lists[0] # 1 ie first element
>>> lists = [1,3,9,"a"]
>>> "a" in lists # True
>>> 3 in lists # True
>>> "p" in lists #False
>>> "p" not in lists # True
sum(), max(), min(), sorted()
.sum()
adds all the numeric items and return their sum, max()
returns the maximum/greatest item in the list, min()
returns the lowest item in the listsorted()
returns the list sorted in ascending or descending order as advised.>>> lists = [2,1,3,4]
>>> sum(lists) # 2+1+3+4 = 10
>>> max(lists) # 4
>>> min(lists) # 1
>>> sorted(lists) # 1,2,3,4
map, reduce
ect.Numbers
and
Strings
discussed earlier in our series,.append(object)
.append()
is a python list method, used to add objects to the end of a list. Strings, Dictionaries, Numbers
and even other Lists
Here is a precise example of a python list.>>> List = [1,5,2,1]
>>> List.append('man')
>>> print(List) # [1,5,2,1,'man']
>>> List.append([9,'a']) # [1,5,2,1,'man',[9,'a']]
.clear()
clear()
removes/deletes everything in the list and leave it empty.
Warning: This function must be used with care because, by using this function, all the data in the list will be deleted.
>>> List = [1,5,2,1]
>>> List.append('man')
>>> print(List) # [1,5,2,1,'man']
>>> List.clear() # []
.copy()
copy()
copies/duplicates everything in the list into another list.
Actually, we use this function when, for instance, we want to use contents of the list while leaving the initial list unchanged.
>>> List1 = [1,5,2,1]
>>> List2 = List1.copy()
>>> print(List1) # [1,5,2,1]
>>> print(List2) # [1,5,2,1]
.count(value)
count()
takes one argument and returns how many times the argument appears in the list.>>> List = [1,5,2,1]
>>> List.count(2) # 1
>>> print(List.count(1)) # 2
>>> List.count('a') # 0
.extend(iterable)
extend()
adds/extends a list by adding items from the iterable.
Note: The iterable
can be another list, a tuple,
dictionary and/or set.
Here is a brief usage of this fucntion.>>> List = [1,5,2,1]
>>> List.extend(['a','b'])
>>> print(List) [1,5,2,1,'a','b']
>>> List.extend((1,3,4)) # []
.index(value)
index()
is most useful when the position of that item in the list is not known.
The proper usage of this function is as follows.
>>> List1 = [1,5,2,1]
>>> List1.index(5) 1
>>> print(List1.index(1)) # 0
>>> print(List1.index(2)) # 2
.insert(index, object)
insert()
is mostly used when the position of the item to be inserted into list matters.
Below is the proper usage of this function in examples.
>>> List1 = [1,5,2,1]
>>> List1.insert(0,10) 1
>>> print(List1) # [10,1,5,2,1]
>>> print(List1[0:2]) # [10,1]
.pop(index=-1)
>>> List1 = [1,5,2,1]
>>> List1.pop() 1 -> Last Item
>>> print(List1.pop(1)) # 5 -> item on index 1
>>> [].pop() # IndexError: pop from empty list
.remove(value)
pop()
function, a method .remove()
do not return anything. ValueError
.>>> List1 = [1,5,2,1]
>>> List1.remove(5)
>>> print(List1.index(1)) # 2 (5 is no longer there.)
>>> List1.remove(12) # ValueError: list.remove(x): x not in the list.
.reverse()
reverse()
.reverse()
as the name tells, is the powerful weapon to reverse the order of our sequence.
If we have a sequence of ascending order numbers, then we can make use of this function to reverse it to go descending.>>> List1 = [1,2,3,4,5]
>>> List1.reverse()
>>> print(List1) # [5,4,3,2,1]
.sort(key=None, reverse=False)
sort()
method.>>> List1 = [3,2,1,4]
>>> List1.sort()
>>> print(List1) # [1,2,3,4]
del
method.del
statement can delete different items and objects in python.>>> List1 = [1,5,2,1]
>>> del List1[2]
>>> print(List1) # [1,2,1]
Lists
, hahaha, yes it is! We're done with this little creature called List.Tuples
.