-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5.py
78 lines (57 loc) · 2.39 KB
/
day_5.py
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
# lists
'''
There are four collection data types in Python :
List: is a collection which is ordered and changeable(modifiable). Allows duplicate members.
Tuple: is a collection which is ordered and unchangeable or unmodifiable(immutable). Allows duplicate members.
Set: is a collection which is unordered, un-indexed and unmodifiable, but we can add new items to the set. Duplicate members are not allowed.
Dictionary: is a collection which is unordered, changeable(modifiable) and indexed. No duplicate members.
Array: data structure used to store multiple items, vector containing multiple items
'''
# create lists
import random as rd
list = list()
list = []
names = ['Minna', 'Ben', 'Carina', 'Mathilde']
print(f'Names: {names}')
# lists can have different inputs
list = [True, 'Minna', {'Eyes': 'Green'}, (1, 2, 3, 4, 5)]
print(list)
list[2] # {'Eyes': 'Green'}
first, second, third, *rest = names
print(rest)
names[::2] # take by two's
# modifying
names[3] = 'Maja'
names.append('Mathilde')
names.insert(2, 'Lisa')
# ['Minna', 'Ben', 'Lisa', 'Carina', 'Maja']
minna = 'Minna' in names # True
# removing items
names.remove('Carina')
names.pop() # 'Maja'
# ['Minna', 'Ben', 'Lisa']
del names[2]
# ['Minna', 'Ben']
names2 = names.copy()
all_names = names + names2
all_names.count('Minna') # 2
len(all_names)
print(all_names)
# sorting
all_names.sort() # ['Ben', 'Ben', 'Minna', 'Minna']
all_names.sort(reverse=True) # ['Minna', 'Minna', 'Ben', 'Ben']
names2.clear() # deletes all elements
# how to know whether something is a method (var.method) or a function (function(var)) -> usually method, unless you wrote it yourself, or few cases see [here](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/02_Day_Variables_builtin_functions/02_variables_builtin_functions.md)
# Exercises
rd.randint(1, 40)
# 6 : Declare a list variable named it_companies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon.
it_companies = ['Facebook', 'Google', 'Microsoft',
'Apple', 'IBM', 'Oracle', 'Amazon']
# 14: Join the it_companies with a string '#; '
'#; '.join(it_companies)
# Level 2:3
# ['China', 'Russia', 'USA', 'Finland', 'Sweden', 'Norway', 'Denmark']. Unpack the first three countries and the rest as scandic countries.
countries = ['China', 'Russia', 'USA',
'Finland', 'Sweden', 'Norway', 'Denmark']
china, russia, usa, *scandic = countries
scandic