Skip to content

Commit 30cefd7

Browse files
author
lev epshtein
committed
list and dic added
1 parent 977747f commit 30cefd7

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Lists are mutable sequences
2+
# https://docs.python.org/3/library/stdtypes.html#lists
3+
4+
# 1 print list, len, access
5+
# courses = ['History', 'Math', 'Physics', 'CompSci']
6+
# print(courses)
7+
# print(len(courses))
8+
# print(courses[0])
9+
# print(courses[1])
10+
# print(courses[-2])
11+
# print(courses[-1])
12+
13+
# 2 slicing:
14+
# courses = ['History', 'Math', 'Physics', 'CompSci']
15+
# print(courses[:2])
16+
# print(courses[2:])
17+
# print(courses[1:3])
18+
19+
# 3 list append, insert
20+
# courses = ['History', 'Math', 'Physics', 'CompSci']
21+
# courses_2 = ["Dance","Poetry"]
22+
# print(courses)
23+
# courses.append(courses_2)
24+
# print(courses)
25+
26+
# courses.insert(2,"Music")
27+
# print(courses)
28+
29+
30+
# 4 list remove , pop
31+
# courses = ['History', 'Math', 'Physics', 'CompSci']
32+
# print(courses)
33+
# courses.remove("History")
34+
# print(courses)
35+
#
36+
# pop_value = courses.pop()
37+
# print(courses)
38+
# print(pop_value)
39+
# pop_value = courses.pop(0)
40+
# print(pop_value)
41+
# print(courses)
42+
43+
# 5 list reverse, sort
44+
# courses = ['History', 'Math', 'Physics', 'CompSci']
45+
# nums = [1,5,2,8,4,6]
46+
# print(courses)
47+
# courses.reverse()
48+
# print(courses)
49+
50+
# courses.sort()
51+
# print(courses)
52+
# courses.sort(reverse=True)
53+
# print(courses)
54+
55+
# print(sorted(nums))
56+
# print(nums)
57+
58+
59+
# list min, max, sum
60+
# nums = [1,5,2,8,4,6]
61+
# print(max(nums))
62+
# print(min(nums))
63+
# print(sum(nums))
64+
65+
66+
# 6 list index
67+
# courses = ['History', 'Math', 'Physics', 'CompSci']
68+
# print(courses.index("CompSci"))
69+
70+
# in operator
71+
# courses = ['History', 'Math', 'Physics', 'CompSci']
72+
# print('Math' in courses)
73+
# print('Art' in courses)
74+
#
75+
# for item in courses:
76+
# print(item)
77+
78+
# for index, course in enumerate(courses, start=1):
79+
# print(index, course)
80+
81+
# 7 join, split
82+
# courses = ['History', 'Math', 'Physics', 'CompSci']
83+
# course_str = ', '.join(courses)
84+
# print(course_str)
85+
#
86+
# new_list = course_str.split(', ')
87+
# print(new_list)
88+
89+
### Tuples immutable sequence types ###############
90+
# Mutable
91+
# list_1 = ['History', 'Math', 'Physics', 'CompSci']
92+
# list_2 = list_1
93+
#
94+
# print(list_1)
95+
# print(list_2)
96+
#
97+
# list_1[0] = 'Art'
98+
#
99+
# print(list_1)
100+
# print(list_2)
101+
102+
103+
# Immutable can't append remove etc but rest same as list
104+
# tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
105+
# tuple_2 = tuple_1
106+
#
107+
# print(tuple_1)
108+
# print(tuple_2)
109+
#
110+
# tuple_1[0] = 'Art'
111+
#
112+
# print(tuple_1)
113+
# print(tuple_2)
114+
115+
### Sets unordered and have no duplicates sequences ###############
116+
# Sets optimazed for in
117+
# cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
118+
# print(cs_courses)
119+
# print('Math' in cs_courses) # optimazed for sets !
120+
121+
# art_courses = {'History', 'Math', 'Art', 'Design'}
122+
# print(cs_courses.intersection(art_courses)) # both in sets
123+
# print(cs_courses.difference(art_courses)) # differnets between two
124+
# print(cs_courses.union(art_courses)) # union of both
125+
126+
# empty initialitation
127+
# Empty Lists
128+
# empty_list = []
129+
# empty_list = list()
130+
#
131+
# # Empty Tuples
132+
# empty_tuple = ()
133+
# empty_tuple = tuple()
134+
#
135+
# # Empty Sets
136+
# empty_set = {} # This isn't right! It's a dict
137+
# empty_set = set()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”.
2+
# Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type;
3+
# strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples;
4+
# if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
5+
# You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
6+
# https://docs.python.org/3/tutorial/datastructures.html#dictionaries
7+
8+
# 1 Initialization , key, value. access
9+
# student = {'name': 'Jhon', 'age':'25', 'courses':['Math', 'CompSci']} # key can be int , string all immutable types !
10+
# print(student['name'])
11+
# print(student['courses'])
12+
#
13+
# print(student.get('phone', 'Not Found'))
14+
15+
# 2 add key value
16+
# student = {'name': 'Jhon', 'age':'25', 'courses':['Math', 'CompSci']} # key can be int , string all immutable types !
17+
# student['phone'] = '555-55556'
18+
# print(student)
19+
# student['name'] = 'Janet'
20+
# print(student)
21+
22+
# 3 dictionary update
23+
# student = {'name': 'Jhon', 'age':'25', 'courses':['Math', 'CompSci']} # key can be int , string all immutable types !
24+
# print(student)
25+
# student.update({'name': 'Jane', 'age':23, 'phone': '555-55556'})
26+
# print(student)
27+
28+
# 4 dictionary del method
29+
# student = {'name': 'Jhon', 'age':'25', 'courses':['Math', 'CompSci']} # key can be int , string all immutable types !
30+
# print(student)
31+
32+
# with del
33+
# del student['age']
34+
# print(student)
35+
36+
# with pop
37+
# age = student.pop('age')
38+
# print(age)
39+
# print(student)
40+
41+
42+
# walking through dic
43+
# student = {'name': 'Jhon', 'age':'25', 'courses':['Math', 'CompSci']} # key can be int , string all immutable types !
44+
# print(len(student))
45+
# print(student.keys())
46+
# print(student.values())
47+
# print(student.items())
48+
49+
# walking throught key
50+
# for key in student:
51+
# print(key)
52+
53+
# walking both key, values
54+
# for key, value in student.items():
55+
# print(key,value)

0 commit comments

Comments
 (0)