Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84ff9a0

Browse files
Lists, tuples and sets added
1 parent cf011fc commit 84ff9a0

File tree

5 files changed

+224
-1
lines changed

5 files changed

+224
-1
lines changed

‎lists.py‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#####
2+
# LISTS
3+
#####
4+
5+
# Lists are mutable collections, we can modify them.
6+
# Elements of lists are inside [ ]
7+
8+
courses = ['History', 'Math', 'Physics', 'ComputerScience']
9+
print(courses)
10+
print(len(courses))
11+
print(courses[0]) # Usage of index in lists
12+
print(courses[3])
13+
14+
# Usage of negative index
15+
print(courses[-1]) # Print the last one
16+
print(courses[-2])
17+
18+
# print(courses[4]) # Index Error
19+
20+
print('\n')
21+
22+
# SLICING
23+
print(courses[0:2]) # From 0 (included) to 2 (excluded)
24+
print(courses[:2])
25+
print(courses[1:])
26+
27+
28+
print('\n')
29+
30+
31+
# LIST METHODS
32+
# Add Remove
33+
courses.append('Art') # Add a course to the list (at the end)
34+
print(courses)
35+
36+
courses.insert(0, 'Algebra') # Add 'Algebra' element in the position 0 of the list
37+
print(courses)
38+
39+
courses_2 = ['Education', 'Machine Learning']
40+
courses.extend(courses_2) # We want to add multiple values (at the end)
41+
print(courses)
42+
# WARN: courses.insert(0, courses_2) will add the entire courses_2 list as a single element in the course list.
43+
44+
courses.remove('Art')
45+
print(courses)
46+
47+
item = courses.pop() # Removes the last item (list usage as a stack or a queue), it returns the value
48+
print(courses)
49+
print(item)
50+
51+
52+
print('\n')
53+
54+
55+
# Order
56+
57+
courses.reverse() # Reverse order
58+
print(courses)
59+
60+
courses.sort() # Sort items (str in alphabetical order)
61+
print(courses)
62+
63+
nums = [1, 3, 7, 32, 2]
64+
nums.sort() # Sort items (int in ascending order)
65+
print(nums)
66+
67+
nums.sort(reverse=True) # Equals to nums.reverse()
68+
print(nums)
69+
70+
71+
print('\n')
72+
73+
74+
# If we want to sort without altering the original list
75+
nums = [1, 3, 7, 32, 2]
76+
sorted_nums = sorted(nums)
77+
print(sorted_nums) # That is sorted
78+
print(nums) # The original one wasn't touched
79+
80+
81+
print('\n')
82+
83+
84+
# Min, Max, Sum
85+
print(min(nums))
86+
print(max(nums))
87+
print(sum(nums))
88+
89+
print('\n')
90+
91+
92+
# Find
93+
courses = ['History', 'Math', 'Physics', 'ComputerScience']
94+
print(courses.index('Math')) # Print 'Math' index
95+
print(courses.index('History'))
96+
# print(courses.index('Art')) # Error
97+
print('Art' in courses) # Boolean result (Is there 'Art' in courses?)
98+
99+
100+
print('\n')
101+
102+
103+
# Loop
104+
for item in courses: # Cicle the list calling every single cicle the element as 'item' and each time print it.
105+
print(item)
106+
107+
print('\n')
108+
109+
for index, course in enumerate(courses): # Same cicle that prints also the index of the element
110+
print(index, course)
111+
112+
print('\n')
113+
114+
for index, course in enumerate(courses, start=1): # Same cicle with numbers that starts from 1 instead of 0
115+
print(index, course)
116+
117+
118+
print('\n')
119+
120+
121+
# Join
122+
courses = ['History', 'Math', 'Physics', 'ComputerScience']
123+
course_str = ', '.join(courses) # Join the elements of the list in a single string
124+
print(course_str) # This is a string
125+
126+
# Split
127+
new_list = course_str.split(", ") # Splits the string taking ", " as sepator and returns a List of strings
128+
print(new_list) # This is a List of strings
129+
130+
131+
print('\n')
132+
133+
134+
# Empty List (2 methods)
135+
empty_list = list()
136+
empty_list = []
137+
138+
139+
# --------------------> <-------------------- #

‎numbers.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@
6161
print('\n')
6262

6363

64+
#####
6465
# STRINGS AND NUMBERS (CASTING)
66+
#####
67+
6568
num_1 = '100' # Those are strings!
6669
num_2 = '200'
6770

‎sets.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#####
2+
# SETS
3+
#####
4+
5+
# Sets have no duplicate elements and unordered
6+
# Elements of lists are inside { }
7+
8+
courses = {'History', 'Math', 'Physics', 'Art'}
9+
print(courses) # Try to execute and see the order of the print! (unordered)
10+
11+
courses.add('Computer Science')
12+
courses.add('Art') # This is a duplicate and will not be inserted
13+
print(courses)
14+
15+
# Sets are useful to check efficiently if there is an element (Optimized for this)
16+
print('Math' in courses)
17+
18+
# See values shered between sets (Optimized for this)
19+
courses_1 = {'History', 'Math', 'Physics', 'Computer Science'}
20+
courses_2 = {'History', 'Math', 'Design', 'Art'}
21+
22+
print(courses_1.intersection(courses_2)) # Returns a set with intersection between 2 sets.
23+
print(courses_1.difference(courses_2)) # Difference
24+
print(courses_1.union(courses_2)) # Union
25+
26+
# Emply Set (1 method)
27+
empty_set = set()
28+
# empty_set = {} # Wrong! This will create an empty dictionary
29+
30+
31+
# --------------------> <-------------------- #

‎strings.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# BASIC STRINGS
1+
#####
2+
# STRINGS
3+
#####
24

35
# This is a comment
46

‎tuples.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#####
2+
# TUPLES
3+
#####
4+
5+
# Tuples are immutable, so we can't modify them.
6+
# Elements of lists are inside ( )
7+
8+
9+
# Example with list on why we need Tuples:
10+
11+
list_1 = ['History', 'Math', 'Physics', 'CompSci']
12+
list_2 = list_1 # We are inizializing list_2 pointer to list_1
13+
14+
print(list_1)
15+
print(list_2)
16+
17+
# So if we change list_1 we are also changing list_2
18+
list_1[0] = 'Art'
19+
20+
print(list_1)
21+
print(list_2)
22+
23+
24+
print('\n')
25+
26+
27+
# Immutable Tuples:
28+
tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
29+
tuple_2 = tuple_1
30+
31+
print(tuple_1)
32+
print(tuple_2)
33+
34+
# tuple_1[0] = 'Art' # This will give an error!
35+
36+
# print(tuple_1)
37+
# print(tuple_2)
38+
39+
40+
print('\n')
41+
42+
43+
# Empty Tuple (2 methods)
44+
empty_tuple = tuple()
45+
empty_tuple = ()
46+
47+
48+
# --------------------> <-------------------- #

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /