Python
Tekst

Listy jako uporządkowane zbiory danych

Lekcja 31 Moduł 6

Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.



>>> lista_1 = [24, 98, 34, 901]
>>> type(lista_1)
<class 'list'>
>>> len(lista_1)
4


for r in range(len(lista_1)):
    print(lista_1[r])

    
24
98
34
901


for r in range(len(lista_1)):
    print(r, lista_1[r])

    
0 24
1 98
2 34
3 901


lista_2 = [24, 34, "Adam", 3.14, "Python", True, [1, 32], 9.45]
for x in lista_2:
    print(x)

    
24
34
Adam
3.14
Python
True
[1, 32]
9.45


for i,x in enumerate(lista_2):
    print(f" Indeks {i}, lista ma wartość {x}")

    
 Indeks 0, lista ma wartość 24
 Indeks 1, lista ma wartość 34
 Indeks 2, lista ma wartość Adam
 Indeks 3, lista ma wartość 3.14
 Indeks 4, lista ma wartość Python
 Indeks 5, lista ma wartość True
 Indeks 6, lista ma wartość [1, 32]
 Indeks 7, lista ma wartość 9.45


for i,x in enumerate(lista_2):
    print(f" Indeks {i}, lista ma wartość {x}")
    print(f" Typ danych dla {x} to {type(x)}")

    
 Indeks 0, lista ma wartość 24
 Typ danych dla 24 to <class 'int'>
 Indeks 1, lista ma wartość 34
 Typ danych dla 34 to <class 'int'>
 Indeks 2, lista ma wartość Adam
 Typ danych dla Adam to <class 'str'>
 Indeks 3, lista ma wartość 3.14
 Typ danych dla 3.14 to <class 'float'>
 Indeks 4, lista ma wartość Python
 Typ danych dla Python to <class 'str'>
 Indeks 5, lista ma wartość True
 Typ danych dla True to <class 'bool'>
 Indeks 6, lista ma wartość [1, 32]
 Typ danych dla [1, 32] to <class 'list'>
 Indeks 7, lista ma wartość 9.45
 Typ danych dla 9.45 to <class 'float'>


lista_2
[24, 34, 'Adam', 3.14, 'Python', True, [1, 32], 9.45]


lista_2 + 23
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    lista_2 + 23
TypeError: can only concatenate list (not "int") to list


>>> lista_2 + [23]
[24, 34, 'Adam', 3.14, 'Python', True, [1, 32], 9.45, 23]
>>> lista_2
[24, 34, 'Adam', 3.14, 'Python', True, [1, 32], 9.45]
>>> lista_2.append(23)
>>> lista_2.append(2.45)
>>> lista_2.append("Adam")
>>> lista_2
[24, 34, 'Adam', 3.14, 'Python', True, [1, 32], 9.45, 23, 2.45, 'Adam']
>>> lista_2[2]
'Adam'
>>> lista_2[2] = "Linux"
>>> lista_2
[24, 34, 'Linux', 3.14, 'Python', True, [1, 32], 9.45, 23, 2.45, 'Adam']
>>> lista_2[2] = 343
>>> lista_2
[24, 34, 343, 3.14, 'Python', True, [1, 32], 9.45, 23, 2.45, 'Adam']
>>> del(lista_2[3])
>>> lista_2
[24, 34, 343, 'Python', True, [1, 32], 9.45, 23, 2.45, 'Adam']
>>> lista_2[3]
'Python'
>>>
>>>


lista_3 = [ x for x in range(10) ]
>>> lista_3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


lista_4 = [ x**3 for x in range(10) ]
>>> lista_4
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
>>>

Leave a comment

Comment as a guest:

Name * E-Mail *
Website
Pen