Skip to content

Commit b15943d

Browse files
committed
python ex
1 parent 6437bf4 commit b15943d

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

stepic/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# countries = ['Switzerland', 'Estonia', 'Sweden', 'Latvia', 'Butan', 'Nepal']
2+
3+
# for index in range(len(countries)):
4+
# print(f'{index+1}, {countries[index]}')
5+
6+
# for item in enumerate(countries, start=1):
7+
# print(item)
8+
9+
# for index, country in enumerate(countries, start=1):
10+
# print(f'{index}, {country}')
11+
12+
#=============================================================================================
13+
14+
# from itertools import zip_longest
15+
16+
# countries = ['Switzerland', 'Estonia', 'Sweden', 'Latvia', 'Bhutan', 'Nepal']
17+
# capitals = ['Bern', 'Tallin', 'Stockholm', 'Riga']
18+
19+
# for country, capital in zip_longest(countries, capitals, fillvalue='Unknown'):
20+
# print(f'{capital} is the capital of {country}')
21+
22+
# ============================================================================================
23+
24+
countries = ['Switzerland', 'Estonia', 'Sweden', 'Latvia', 'Bhutan', 'Nepal']
25+
capitals = ['Bern', 'Tallin', 'Stockholm', 'Riga', 'Thimphu', 'Kathmandu']
26+
27+
destinations = list(zip(countries, capitals))
28+
print(destinations)
29+
30+
country, capital = zip(*destinations)
31+
print(country)
32+
print(capital)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import itertools
2+
3+
def main():
4+
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
5+
dni = ["Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота"]
6+
7+
# i = iter(days)
8+
# print(next(i))
9+
# print(next(i))
10+
# print(next(i))
11+
# print(next(i))
12+
13+
# with open("testfile.txt", "r") as fp:
14+
# for line in iter(fp.readline, ''):
15+
# print(line)
16+
17+
# for i in range(len(days)):
18+
# print(i, days[i])
19+
20+
# cycle_days = itertools.cycle(days)
21+
# print(next(cycle_days))
22+
# print(next(cycle_days))
23+
# print(next(cycle_days))
24+
# print(next(cycle_days))
25+
# print(next(cycle_days))
26+
# print(next(cycle_days))
27+
# print(next(cycle_days))
28+
# print(next(cycle_days))
29+
30+
# count_stuff = itertools.count(100, 10)
31+
# print(next(count_stuff))
32+
# print(next(count_stuff))
33+
# print(next(count_stuff))
34+
# print(next(count_stuff))
35+
# print(next(count_stuff))
36+
37+
prices = [10, 20, 30, 40, 50, 40 ,30 ,20 , 10, 100]
38+
acc = itertools.accumulate(prices)
39+
print(list(acc))
40+
41+
# chain_stuff = itertools.chain(days, prices)
42+
# print(list(chain_stuff))
43+
44+
# def test_func(x):
45+
# return x < 50
46+
47+
# print(list(itertools.dropwhile(test_func, prices)))
48+
# print(list(itertools.takewhile(test_func, prices)))
49+
50+
51+
if __name__ == "__main__":
52+
main()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# lambda argument(s): expression
2+
3+
#Normal python function
4+
def func(x):
5+
return x+x
6+
7+
#Lambda function
8+
lambda x: x+x
9+
10+
# scalar (single) values
11+
print((lambda x: x*2)(14))
12+
13+
# # list
14+
list_1 = [1,2,3,4,5,6,7,8,9]
15+
list_obj = (lambda x: x*2)
16+
print(list_obj(list_1))
17+
18+
#string
19+
str1 = 'AveCoder'
20+
rev_upper = lambda string: string.upper()[::-1]
21+
print(rev_upper(str1))
22+
23+
# using with list comprehensions:
24+
is_even_list = [lambda arg=x: arg * 10 for x in range(1, 15)]
25+
26+
# iterate on each lambda function
27+
# and invoke the function to get the calculated value
28+
for item in is_even_list:
29+
print(item())
30+
31+
# conditional if-else
32+
# Example of lambda function using if-else
33+
cond = lambda a, b : a if(a > b) else b
34+
35+
print(cond(10, 20))
36+
37+
#################################
38+
# Lambda with multiple statements
39+
List = [[12,23,56],[2, 6, 14, 72],[13, 5, 7, 22]]
40+
41+
# Sort each sublist
42+
srt_lst = lambda x: (sorted(i) for i in x)
43+
44+
# Get the second largest element
45+
sec_lrg = lambda x, f : [y[len(y)-2] for y in f(x)]
46+
res = sec_lrg(List, srt_lst)
47+
48+
print(res)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
not_raw_str = 'Not\tRaw\nString'
2+
3+
#print(not_raw_str)
4+
5+
raw_str = r'Not\tRaw\nString'
6+
7+
print(raw_str)
8+
9+
print(type(not_raw_str))
10+
print(type(raw_str))
11+
12+
print(r'Not\tRaw\nString' == 'Not\\tRaw\\nString')
13+
14+
print(len(not_raw_str))
15+
print(len(raw_str))
16+
17+
print(list(not_raw_str))
18+
print(list(raw_str))
19+
20+
########
21+
22+
path = 'C:\\Windows\\system32\\cmd.exe'
23+
rpath = r'C:\Windows\system32\cmd.exe'
24+
25+
print(path == rpath)
26+
27+
path2 = 'C:\\Windows\\system32\\'
28+
rpath2 = r'C:\Windows\system32' + '\\'
29+
30+
print(path2 == rpath2)
31+
32+
#########################
33+
34+
s_r = repr(not_raw_str)
35+
print(s_r)
36+
37+
print(list(s_r))
38+
39+
s_r2 = repr(not_raw_str)[1:-1]
40+
print(s_r2)
41+
42+
print(raw_str == s_r2)
43+
44+
print(r'\t' == repr('\t')[1:-1])

stepic/python_fullstack/testfile.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is number 1
2+
This is number 2
3+
This is number 3
4+
This is number 4
5+
This is number 5
6+
This is number 6
7+
This is number 7

0 commit comments

Comments
 (0)