(B17) Python part5 Python Data Type - Collection(Arrays) 1- List , Tuple, Set, Dictionary
20220520 152734
Python Collections (Arrays)
There are four collection data types in the Python programming language:
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.
*Set items are unchangeable, but you can remove and/or add items whenever you like.
----------------------------------------------
List:[]
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
EX.
thislist = ["apple", "banana", "cherry"]
print(thislist)
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
A = [23,45,72,23,88,44,72,98]
print(A)
[23, 45, 72, 23, 88, 44, 72, 98]
print(type(A))
class 'list'
print(len(A))
8
index:
-8 -7 -6 -5 -4 -3 -2 -1
[23,45,72,23,88,44,72,98]
0 1 2 3 4 5 6 7
A[0]
23
A[1]
45
A[6]
72
A[3:]
[23, 88, 44, 72, 98]
A[2:5]
[72, 23, 88]
A[-6]
72
A[-6:-3]
[72, 23, 88]
A
[23, 45, 72, 23, 88, 44, 72, 98]
A.extend([33,44,55])
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44, 55]
A[8]
33
A[9]
44
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44, 55]
A.append([12,13,14])
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44, 55, [12, 13, 14]]
A[-1]
[12, 13, 14]
A.pop()
[12, 13, 14]
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44, 55]
A.pop()
55
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44]
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44]
A.index(88)
4
A.index(98)
7
A.count(72)
2
A.count(44)
2
A.count(88)
1
A
[23, 45, 72, 23, 88, 44, 72, 98, 33, 44]
A.sort()
A
[23, 23, 33, 44, 44, 45, 72, 72, 88, 98]
A.reverse()
A
[98, 88, 72, 72, 45, 44, 44, 33, 23, 23]
A.remove(33)
A
[98, 88, 72, 72, 45, 44, 44, 23, 23]
A.remove(44)
A
[98, 88, 72, 72, 45, 44, 23, 23]
A.insert(3,79)
A
[98, 88, 72, 79, 72, 45, 44, 23, 23]
A[1]
88
A[1]=28
A
[98, 28, 72, 79, 72, 45, 44, 23, 23]
A
[98, 28, 72, 79, 72, 45, 44, 23, 23]
A * 2
[98, 28, 72, 79, 72, 45, 44, 23, 23, 98, 28, 72, 79, 72, 45, 44, 23, 23]
B = [101, 204, 509, 309]
A + B
[98, 28, 72, 79, 72, 45, 44, 23, 23, 101, 204, 509, 309]
---------------------------------------------------------------
Tuple: ()
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
Ex.
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
-----------
C = (12,44,82,46,19,52,61,34)
print(C)
(12, 44, 82, 46, 19, 52, 61, 34)
print(type(C))
class 'tuple'
len(C)
8
-8 -7 -6 -5 -4 -3 -2 -1
(12,44,82,46,19,52,61,34)
0 1 2 3 4 5 6 7
C.index(46)
3
C.count(44)
1
C[0]
12
C[4]
19
C[4:]
(19, 52, 61, 34)
C[3:6]
(46, 19, 52)
C[1]
44
C[1]=55
Traceback (most recent call last):
C[1]=55
TypeError: 'tuple' object does not support item assignment
D=(55,66,12,55,78,99,12,55)
D
(55, 66, 12, 55, 78, 99, 12, 55)
D.count(55)
3
D.index(12)
2
C+D
(12, 44, 82, 46, 19, 52, 61, 34, 55, 66, 12, 55, 78, 99, 12, 55)
C * 2
(12, 44, 82, 46, 19, 52, 61, 34, 12, 44, 82, 46, 19, 52, 61, 34)
A+C
Traceback (most recent call last):
A+C
TypeError: can only concatenate list (not "tuple") to list
A *C
Traceback (most recent call last):
A *C
TypeError: can't multiply sequence by non-int of type 'tuple'
------------------------------------------------
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «(B17) Python part5 Python Data Type - Collection(Arrays) 1- List , Tuple, Set, Dictionary», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.