Skip to content

Commit b60537c

Browse files
committed
Add workshop presentation (13.nov)
0 parents  commit b60537c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4462
-0
lines changed

LICENSE

+674
Large diffs are not rendered by default.

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Workshop: Introduction to Python 3
2+
3+
## Presentation
4+
5+
Our presentation was built using remark, and you can open it [here](https://tiagodusilva.github.io/Workshop-Introduction-Python-3/).
6+
7+
## Wiki
8+
Link to our [wiki's Home page](../../wiki/Home)
9+
10+
## Contents
11+
1. [Interpreter](../../wiki/Interpreter)
12+
1. [Variables](../../wiki/Variables)
13+
1. [Data Types](../../wiki/Data-Types)
14+
1. [Flow Control](../../wiki/Flow-Control)
15+
1. [Data Structures](../../wiki/Data-Structures)
16+
1. [Iteration](../../wiki/Iteration)
17+
1. [Functions](../../wiki/Functions)
18+
1. [Comprehensions and Generators](../../wiki/Comprehensions-and-Generators)
19+
1. [Modules](../../wiki/Modules)
20+
21+
## Exercises and Solutions
22+
23+
1. [Exercises](../../wiki/Exercises)
24+
1. [Solutions](../../wiki/Solutions)
25+
26+
## Recommended IDEs
27+
- [Spyder/Anaconda](https://www.anaconda.com/distribution/): Installs python and various modules for you, recommended for an easier setup.
28+
- [VS Code](https://code.visualstudio.com/Download): Requires the manual installation of the [most recent version of Python 3](https://www.python.org/downloads/). Please refer to [Python in VS Code](https://code.visualstudio.com/docs/languages/python) for more detailed instructions. We recommend the Code Runner extension (present on the website listed above).
29+
- [Vim](https://www.vim.org/): [Vim8](https://vim8.org/) or [NeoVim](https://neovim.io/) prefered. Only use if you are familiar with Vim. Recommended extensions: coc, coc-python (node.js required for both).
30+
31+
## Other Resources
32+
33+
- Python Standard Library (Latest 3.x version released)
34+
- [Website](https://docs.python.org/3/library/index.html)
35+
- [Download as a file](https://docs.python.org/3/download.html)
36+
37+
## Authors
38+
39+
- João Costa (@joaocostaifg)
40+
- João Lucas (@joaolucasmartins)
41+
- Tiago Silva (@tiagodusilva)

assets/computer-society-logo.svg

+184
Loading

assets/nuieee-logo.png

39.8 KB
Loading

assets/slicing.png

82.3 KB
Loading

assets/variables-objects.png

69.6 KB
Loading

code/data structures/del_statement.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
alist = [1, 2, 3, 4, 5, 6, 7, 8]
4+
print("Our list:", alist)
5+
6+
# Remove 1 element
7+
del alist[1]
8+
print("Our list:", alist)
9+
10+
# Remove a slice
11+
del alist[0:3]
12+
print("Our list:", alist)
13+
14+
# Remove all elements (same as alist.clear())
15+
del alist[:]
16+
print("Our list:", alist)
17+

code/data structures/dict.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
# Examples a dictionary declaration that associates names with age
4+
# Equivalent to ages = dict([[Carlos, 12], [Vilas, 19], [Tiago, 5]])
5+
# Or, since the keys are all simple strings, ages = dict(Carlos=12, Vilas=19, Tiago=5)
6+
ages = {"Carlos": 12, "Vilas": 19, "Tiago": 5}
7+
print("Our dict:", ages)
8+
9+
# Print a single element
10+
print("Tiago's age:", ages["Tiago"])
11+
12+
# We can add any key:value pair to dictionary at any time
13+
ages[432.5] = "oops"
14+
print("Our dict:", ages)
15+
16+
# The list() funtion will return a list of the keys in the dictionary in insertion order
17+
print("Our list:", list(ages))
18+
19+
# A dictionary is also an iterable (will be discussed in the iterable chapter)
20+
print("Carlos" in ages)
21+
22+
# The next commented line results in an order because we can't access non-existant keys
23+
# print(ages["123"])
24+
25+
# If we store a key that's already in use, the previous value of that key is dropped
26+
ages["Carlos"] = 11
27+
print("Our dict:", ages)
28+
29+
# We can use the del keyword with dictionaries
30+
del ages["Vilas"]
31+
print("Our dict:", ages)

code/data structures/lists.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
# Declare a list with the items 1, 2
4+
alist = [1, 2]
5+
print("Our list:", alist)
6+
7+
# Appends an element to the end of the list
8+
alist.append(5)
9+
print("Our list:", alist)
10+
11+
# Appends all the elements in an iterable to 'alist'
12+
anotherlist = [3, 4]
13+
alist.extend(anotherlist)
14+
print("Our list:", alist)
15+
16+
# Inserts an item, x, at a given position, i, in a list (list.insert(i,x))
17+
alist.insert(1, 9)
18+
print("Our List:", alist)
19+
20+
# Removes the first item from the list with value x (list.remove(x))
21+
alist.remove(9)
22+
print("Our List:", alist)
23+
24+
# Remove and return an item from a list at a given position, i
25+
# If no argument is given, remove and return the last element from the list
26+
alist.pop(1)
27+
print("Our List:", alist)
28+
29+
# Return the index of the first item with value x in the list
30+
# Can take an option start and/or end index (list.index(x[, start[, end]]))
31+
print("Index:", alist.index(1))
32+
33+
# Count the number of times an item, x, appears in the list
34+
print("Number of occurrences", alist.count(1))
35+
36+
# Reverses the items of the list, in place
37+
alist.reverse()
38+
print("Our list:", alist)
39+
40+
# blist is a pointer to alist
41+
blist = alist
42+
# blist is a shallow copy of alist
43+
blist = alist.copy()
44+
45+
# remove all items from a list (equivalent to del alist[:])
46+
print("Our list:", blist)
47+
blist.clear()
48+
print("Our list:", blist)
49+

code/data structures/pack_tuples.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
# Tuple packing
4+
atuple = (1, "hey", 123412.32)
5+
print("Our tuple", atuple)
6+
7+
# Sequence unpacking (paretheses are also optional here)
8+
(x, y, z) = atuple
9+
print("Our varibles:", x, y, z)

code/data structures/sets.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
3+
# Declare a set
4+
aset = {"yum", "sweet", "potato", "yum"}
5+
# Equivalent to aset = set(['yum', 'sweet', 'potato'])
6+
print("Our set:", aset)
7+
8+
# Check membership
9+
# This in keyword will be discussed in detail in the chapter about iterables
10+
# For now, we just need to know that it checks if "yum" is equal to at least 1 of the elements of aset
11+
print("yum" in aset)
12+
print("Iargo" in aset)
13+
14+
# Crating sets from an iterable (string)
15+
x = set("comparable")
16+
y = set("complementary")
17+
18+
# Unique letters
19+
print("Unique letters in x:", x)
20+
print("Unique letters in y:", y)
21+
22+
# Difference
23+
print("Letters in x but nor in y:", x - y)
24+
25+
# Union (Or)
26+
print("Letters in x, y or both:", x | y)
27+
28+
# Intersection (And)
29+
print("Letter in both x and y:", x & y)
30+
31+
# Symmetric difference (Xor)
32+
print("Letters in x or y but not in both:", x ^ y)
33+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
# Declare an empty tuple
4+
emptytuple = ()
5+
6+
# Declare a tuple with only 1 element
7+
singletuple = (1, ) # Notice the trailing comma
8+
9+
print("Our tuples:", emptytuple, singletuple)
10+
11+
print("emptytuple's len:", len(emptytuple))
12+
print("singletuple:", len(singletuple))
13+

code/data structures/tuples.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
# The paratheses are not mandatory in most declaratios but we recommend their use either way
4+
atuple = (1, 2, 'hey', 4.2, 0)
5+
6+
# Print the whole tuple
7+
print("Our tuple:", atuple)
8+
9+
# Elements are indexed
10+
print("1st element of out tuple", atuple[0])
11+
12+
# Nested tuples
13+
btuple = ("a", atuple, 96, atuple)
14+
print("Our nested tuple:", btuple)
15+
16+
# Since tuples are immutable, this next commented line results in an error
17+
# atuple[1] = 3
18+
19+
# But they can contain mutable objects
20+
alist = [1, 2, 3]
21+
ctuple = (alist, 4, 5)
22+
print("Our ctuple:", ctuple)
23+
24+
ctuple[0][0] = 3
25+
print("Our ctuple:", ctuple)
26+

code/flow control/elif_example.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
day = int(input("Weekday? (1-7) "))
4+
5+
if (day == 1): # Parentheses are optional
6+
print("Sunday")
7+
8+
elif day == 2:
9+
print("Monday")
10+
11+
elif day == 3:
12+
print("Tuesday")
13+
14+
elif day == 4:
15+
print("Wednesday")
16+
17+
elif day == 5:
18+
print("Thursday")
19+
20+
elif day == 6:
21+
print("Friday")
22+
23+
elif day == 7:
24+
print("Saturday")
25+
26+
else:
27+
print("Number out of the expected range (1-7)")
28+

code/flow control/for_else_example.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
number = 123
4+
5+
for i in range(3):
6+
print(3 - i, "guess(es) left")
7+
guess = int(input("Your guess? "))
8+
9+
if number == guess:
10+
print("You got it right!!")
11+
# We use the break keyword to end a loop early
12+
break
13+
14+
else:
15+
print("Better luck next time.")
16+
17+
else:
18+
print("You didn't manage to guess the number.")
19+

code/flow control/for_example.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
number = 123
4+
5+
for i in range(3):
6+
print(3 - i, "guess(es) left")
7+
guess = int(input("Your guess? "))
8+
9+
if number == guess:
10+
print("You got it right!!")
11+
# We use the break keyword to end a loop early
12+
break
13+
14+
else:
15+
print("Better luck next time.")
16+
# We use the continue keyword to go straight to the next loop iteration
17+
# (We just wanted to show it in this case)
18+
continue
19+
20+
if number != guess:
21+
print("You didn't manage to guess the number.")
22+

code/flow control/if_basic_example.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
day = int(input("Weekday? (1-7) "))
4+
if day == 1 or day == 7: # You can test multiple condition at the same time
5+
print("Weekend")
6+
7+
else:
8+
print("Work day")
9+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
number = 123
4+
5+
guess = int(input("Your guess (tries left = 3)? "))
6+
if number == guess:
7+
print("You got it right with 1 try!!")
8+
9+
else:
10+
guess = int(input("Your guess (tries left = 2)? "))
11+
if number == guess:
12+
print("You got it right with 2 tries!!")
13+
14+
else:
15+
guess = int(input("Your guess (tries left = 1)? "))
16+
if number == guess:
17+
print("You got it right with 3 tries!!")
18+
19+
else:
20+
print("You didn't manage to guess the number")
21+

code/flow control/while_example.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
number = 123
4+
choice = "yes"
5+
6+
guess = int(input("Your guess? "))
7+
8+
# Parantheses are optional
9+
# The tested condition works the same as in the If structure
10+
while (number != guess) and (choice == "yes"):
11+
choice = input("You didn't get it right, would you like to try again? (yes/no) ")
12+
guess = int(input("Your guess? "))
13+
14+
if choice == "yes":
15+
print("You got it right!!")
16+
17+
else:
18+
print("Better luck next time.")
19+

0 commit comments

Comments
 (0)