2. Data types in Python
Python has several standard data types:
Numbers
Strings
Lists
Dictionaries
Tuples
Sets
Boolean
3. Classification of data types in Python
Data types can be classified according to several criteria:
mutable (lists, dictionaries and sets)
immutable (numbers, strings and tuples)
ordered (lists, tuples, strings and dictionaries)
unordered (sets) types can be classified according to several criteria:
4. Classification of data types in Python
Data type Mutable Immutable Ordered Unordered
Numbers +
Strings + +
Lists + +
Dictionaries + +
Tuples + +
Sets + +
5. Lists in Python
Список в Python это:
последовательность элементов, разделенных между собой запятой и
заключенных в квадратные скобки
изменяемый упорядоченный тип данных
A data structure, which in most programming languages is called an array. In
Python it is called a list. A list is a sequence of elements, numbered from 0,
like the characters in a string.
7. Lists in Python
A list can be created using a literal or the list() built-in function
Creating a list with a literal:
list1 = [10,20,30,77]
Creating a list with a function list():
list2 = list('Hello')
Empty list
There are two ways to create an empty list:
Use empty square brackets [];
Use the built-in function called list.
mylist = [] # empty list
mylist = list() # empty list
8. Lists in Python
Since the list is an ordered data type, then, as in strings, in lists you can
access an element by number, make slices:
list3 = [1, 20, 4.0, 'word']
print(list3[1])
print(list3[1::])
print(list3[-1])
print(list3[::-1])
9. Lists in Python
You can also reverse a list using the reverse() method:
list3 = [1, 20, 4.0, 'word']
print(list3)
list3.reverse()
print(list3)
10. Lists in Python
Since lists are mutable, the elements of the list can be changed:
list3 = [1, 20, 4.0, 'word']
print(list3)
list3[0] = 'test'
print(list3)
11. Lists in Python
You can also create a list of lists. And, just like in a regular list, you can
access elements in nested lists:
interfaces = [['FastEthernet0/0', '15.0.15.1', 'YES', 'manual', 'up', 'up'],
['FastEthernet0/1', '10.0.1.1', 'YES', 'manual', 'up', 'up'],
['FastEthernet0/2', '10.0.2.1', 'YES', 'manual', 'up', 'down']]
print(interfaces)
print(interfaces[0][0])
print(interfaces[2][0])
print(interfaces[2][1])
12. Lists in Python
The len() function for a list returns the number of elements in the list:
items = [1, 2, 3]
print(len(items))
13. Lists in Python
The sorted() function sorts the elements of a list in ascending order and
returns a new list with the sorted elements:
names = ['John', 'Michael', 'Antony']
print(sorted(names))
Obviously it have few parameters
sorted(iterable, key=key, reverse=reverse)
iterable - Required. The sequence to sort, list, dictionary, tuple etc.
key - Optional. A Function to execute to decide the order. Default is None
reverse - Optional. A Boolean. False will sort ascending, True will sort
descending. Default is False
14. Useful methods for work with lists
A list is a mutable data type, so it's important to note that most list methods
change the list “in place” without returning anything.
append
extend
pop
remove
index
insert
sort
15. Useful methods for work with lists
Append
The append method adds the specified element to the end of the list:
list1 = ['10', '20', '30', '100-200']
list1.append('300')
print(list1)
The append method changes the list in place and returns nothing. If in a script
you need to add an element to the list, and then print the list, you need to do
this on different lines of code.
16. Useful methods for work with lists
Extend
If you need to combine two lists, then you can use two methods: the extend
method and the addition operation.
These methods have an important difference - extend changes the list to which
the method is applied, and summing returns a new list, which consists of two.
list1 = ['10', '20', '30', '100-200']
list2 = ['15', '25', '35']
list1.extend(list2)
print(list1)
print(list1 + list2)
17. Useful methods for work with lists
pop
The pop method removes the element that matches the specified number. But,
what is important, the method returns this element:
list1 = ['10', '20', '30', '100-200']
print(list1.pop(-1))
print(list1)
! Without a number, the last element of the list is removed.
18. Useful methods for work with lists
remove
The remove method removes the specified element.
The remove method does not return the removed element:
list1 = ['10', '20', '30', '100-200']
print(list1.remove('20')) #return None in output, but still change a list)
print(list1)
In the remove method, you must specify the element itself to be removed, and
not its number in the list. If you specify an element number, an error will occur.
19. Useful methods for work with lists
index
The index method is used to check which number an element is stored in the list:
list1 = ['10', '20', '30', '100-200']
print(list1.index('20'))
20. Useful methods for work with lists
insert
The insert method allows you to insert an element at a specific position in the
list:
list1 = ['10', '20', '30', '100-200']
list1.insert(1, '2000')
print(list1)
21. Useful methods for work with lists
sort
The sort method sorts the list in place:
list1 = ['190', '200', '3', '10-2']
list2 = [90, 100, 2, 0, -1]
list1.sort()
list2.sort()
print(list1)
print(list2)
22. Notes
Note 1: As already mentioned, lists in Python are similar to arrays in other
programming languages. However, there is still a difference between lists and
arrays. Array elements always have the same data type and are located in the
computer's memory in a continuous block, while list elements can be
scattered in memory as you like and can have different data types.
Note 2. Please note that when displaying the contents of the list using the
print () function, all string elements of the list are surrounded by single
quotes. If you want to output in double quotes, you need to write the output
code yourself.
Editor's Notes
Part 1 for lists probably
Check with
list1 = [10,20,30,77]
Print(list1)
list2 = list('Hello')
Print(list2)
Check output with this examples
Check output with this examples
Check output with this examples
Check output with this examples
Check output with this examples
Check output with this examples
Check output with this examples
Check output with this examples
Please note that when summing lists, the result of the summation can be assigned to a variable. With append result will be more interesting
list1 = ['190', '200', '3', '10-2']list2 = [90, 100, 2, 0, -1]list3 = list1 + list2list4 = list1.append(list2)print(list3)print(list4)print(list1)