GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PythonLearning/Lists.py at master · egormin/PythonLearning

Expand file tree

/

Lists.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
239 lines (183 loc) · 6.13 KB
/

Lists.py

Copy path

File metadata and controls

239 lines (183 loc) · 6.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 1) Zadanie spiskov
print("\n***** Zadanie Spiskov (sposob 1) *****")
# Spisok tsifr
int_list = [1, 2, 3, 4]
print(type(int_list))
print("int list:", int_list)
# Spisok srok
str_list = ['a', 'vdd', 'efg', 'q']
print("str list:", str_list)
# Pustoy spisok
empty_list = []
print("Empty list:", empty_list)
print("\n***** Zadanie Spiskov (sposob 2) *****")
list_from_range = list(range(5)) # Preobrazuem diapazon v spisok
print(list_from_range) # [0, 1, 2, 3, 4]
list_from_string = list('this string') # Preobrazuem stroku v spisok
print(list_from_string) # ['t', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g']
# 2) Ispol'zovanie spiskov
print("\n***** Ispol'zovanie spiskov *****")
chr_list = ['h', 'e', 'l', 'l', 'o']
print(chr_list[0]) # h
print(chr_list[-1]) # o
print(chr_list[-2]) # l
# 3) Srez spiskov
# srez spiska - eto guppa elementov spiska po ih indeksam
# new_list = my_list[start:end:step]
chr_list = ['h', 'e', 'l', 'l', 'o']
print(chr_list[1:5:1]) # ['e', 'l', 'l', 'o']
print(chr_list[0:5:2]) # ['h', 'l', 'o']
# po umolchaniyu nachal'nyi =0, konechniy = dlina spiska
print(chr_list[::2]) # ['h', 'l', 'o']
print(chr_list[0:2]) # ['h', 'e']
print(chr_list[0:-2]) # ['h', 'e', 'l']
print(chr_list[-1:0:-1]) # ['o', 'l', 'l', 'e']
print(chr_list[::-1]) # ['o', 'l', 'l', 'e', 'h']
print(chr_list[:]) # ['h', 'e', 'l', 'l', 'o']
print(chr_list[2:]) # ['l', 'l', 'o']
print(chr_list[2::2]) # ['l', 'o']
# 4) Is value in list
print("\n***** Is value in list *****")
int_list = [1, 2, 3, 4]
str_list = ['a', 'vdd', 'efg', 'q']
value = 3
if value in int_list:
print("value {} is in list {}".format(value, int_list))
value = 'q'
if value in str_list:
print("value {} is in list {}".format(value, str_list))
stroka = 'zhopa'
if 'zh' in stroka:
print("Simvol zh est' v stroke", stroka)
for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if letter in "AEIOU":
print(letter, "is a vowel")
else:
print(letter, "is a consonant")
#5) Dlina spiska
print("\n***** Dlina spiska *****")
int_list = [1, 2, 3, 4]
print("dlina spiska {0} ravna {1} simvolov".format(int_list, len(int_list)))
stroka = 'zhopa'
print(stroka[2])
print(len(stroka))
print(stroka[2::])
print(stroka[0] + stroka[-2:]) # zpa (0 + 2-й с конца и до конца)
# 6) Modifikaciya spiskov
print("\n***** Dobavlenie znacheniya *****")
int_list = [1, 2, 3, 4]
print(int_list) # [1, 2, 3, 4]
int_list.append(99) # [1, 2, 3, 4, 99]
print(int_list)
int_list += [88, 33, 55] # [1, 2, 3, 4, 99, 88, 33, 55]
print(int_list)
str_list = ['a', 'vdd', 'efg', 'q']
print(str_list) # ['a', 'vdd', 'efg', 'q']
str_list += ['add', 'values']
print(str_list) # ['a', 'vdd', 'efg', 'q', 'add', 'values']
str_list += ['add', 'values', 7, 99]
print(str_list) # ['a', 'vdd', 'efg', 'q', 'add', 'values', 'add', 'values', 7, 99]
print("\n***** Izmenenie znacheniya *****")
int_list = [1, 2, 3, 4]
print(int_list)
int_list[0] = 100
print(int_list) # [100, 2, 3, 4]
int_list[0], int_list[2] = 99, 22
print(int_list) # [99, 2, 22, 4]
print("\n***** Udalenie znacheniya *****")
int_list = [1, 2, 3, 4]
print(int_list) # [1, 2, 3, 4]
del int_list[2]
print(int_list) # # [1, 2, 4]
print([1, 2, 3]+[4, 5, 6]) # [1, 2, 3, 4, 5, 6]
spis1 = [1, 2, 3]
spis2 = [4, 5, 6]
print(spis1 + spis2) # [1, 2, 3, 4, 5, 6]
spis1 = ['one']
spis2 = spis1 * 5
print(spis2) # ['one', 'one', 'one', 'one', 'one']
# 7) Obhod elementov spiska
print("\n***** Obhod elementov spiska *****")
for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if letter in "AEIOU":
print(letter, "is a vowel")
else:
print(letter, "is a consonant")
int_list = [1, 2, 3, 4]
for _ in int_list:
print(_) # 1,2,3,4
str_list = ['a', 'vdd', 'efg', 'q']
for _ in str_list:
print(_) # a, vdd, efg, q
print("\n***** Chisla fibonacci *****")
n = 10
fibs = [1, 1]
for i in range(n - 2):
fibs.append(fibs[i] + fibs[i + 1]) # [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
print(fibs)
# One line iteration
int_list = [1, 2, 3, 4]
doubled = [num * 2 for num in int_list]
friends = ['Sam', 'Smith', 'Ell', 'Mike']
starts_s = []
for friend in friends:
if friend.startwith("S"):
starts_s.append(friend")
# Or the same:
friends = ['Sam', 'Smith', 'Ell', 'Mike']
starts_s = [friend for friend in friends if friend.startswith("S")]
# 8) Operacii so spiskami
print("\n***** Operacii so spiskami *****")
# kopiya spiska
spis1 = [1, 2, 3]
spis2 = spis1[:] # ih id budut raznie
# Sravnonie spiskov
print(spis1 == spis2) # sravnenie spiskov po znacheniyu
print(spis1 is spis2) # sravnenie spiskov po id
# 9) Operacii so spiskami
print("\n***** Objedinenie spiskov *****")
spisok1 = [1, 2, 3]
spisok2 = [1, 2, 5, 6]
spisok3 = set(spisok1) # Preobrazuem v mnozhestvo
spisok4 = set(spisok2)
print(spisok3 | spisok4) # {1, 2, 3, 5, 6}
# Ili tak
spisok1 = [1, 2, 3]
spisok2 = [1, 2, 5, 6]
Newspis = list((zip(spisok1, spisok2)))
print(Newspis) # [(1, 1), (2, 2), (3, 5)] Objediniayutsya tak: (spis1[0], spis2[0]), (spis1[1], spis2[1]).....
# Iterations and destructions
students = {"Mike": 19, "Nick": 35, "Shane": 21}
print(list(students.items())) # [('Mike', 19), ('Nick', 35), ('Shane', 21)]
people = [
("Bob", 42, "mechanic"),
("Mike", 32, "doctor"),
("John", 55, "judge"),
]
for person in people:
print(f"Name: {person[0]}, age: {person[1]}, job: {person[2]}")
# Or the same
for name, age, job in people:
print(f"Name: {name}, age: {age}, job: {job}")
# Use _ if param is not needed
person = ("Bob", 42, "mechanic")
name, _, job = person
print(name, job)
# head, tail
head, *tail = [1, 2, 3, 4, 5]
print(head)
print(*tail)
# List comprehensions
sequence = [1, 2, 3]
doubled = [double(x) for x in sequence]
# The same
doubled1 = [(lambda x: x * 2)(x)for x in sequence]
# The same
doubled2 = list(map(lambda x: x * 2, sequence))
# The same
doubled3 = list(map(double, sequence))
print(doubled)
print(doubled1)
print(doubled2)
print(doubled3)
# [2, 4, 6]
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.