Skip to content

Commit 1ac30ac

Browse files
committed
add files adapted from Replit version of intro-to-python
0 parents  commit 1ac30ac

37 files changed

+2497
-0
lines changed

010_comments.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Video alternative: https://vimeo.com/954334215/cbd70ba2fa
2+
3+
# Hello! Welcome to programming. I will guide you through these early steps.
4+
#
5+
# To begin, I'd like to share with you a little quote from a writer called Italo
6+
# Calvino, himself retelling a Chinese story:
7+
#
8+
# "Among Chuang-tzu's many skills, he was an expert draftsman. The king asked
9+
# him to draw a crab. Chuang-tzu replied that he needed five years, a country
10+
# house, and twelve servants. Five years later the drawing was still not begun.
11+
# "I need another five years," said Chuang-tzu. The king granted them. At the
12+
# end of these ten years, Chuang-tzu took up his brush and, in an instant, with
13+
# a single stroke, he drew a crab, the most perfect crab ever seen."
14+
#
15+
# Perhaps you have come to programming because you would like to be good at it.
16+
# Perhaps you know people, or you have heard of people, who are really
17+
# exceptional programmers.
18+
#
19+
# You, very likely, are not this person yet. Like Chuang-tzu, it will take you
20+
# many years to reach the effortless skill of an expert programmer.
21+
#
22+
# But every programmer has sat where you are sat, curious and willing to learn.
23+
# They have spent many hours getting things wrong, being stuck, being confused.
24+
# You will go through this too.
25+
#
26+
# Every expert sat where you are sat. What they all have in common? They kept
27+
# going.
28+
#
29+
# So let's get started.
30+
#
31+
# What are we learning?
32+
# =====================
33+
#
34+
# We're going to learn the fundamentals of the programming language Python. Most
35+
# programming languages are quite similar, so it doesn't matter much that this
36+
# is Python. The key ideas are the same.
37+
#
38+
# We're going to learn enough to succeed in the Makers interview process. The
39+
# ideas themselves are quite simple. The complexity of programming is in the
40+
# combination of these ideas. Here's a list of what we'll learn:
41+
#
42+
# * Functions (making your own programs)
43+
# * Arithmetic (basic maths)
44+
# * Expressions and statements (the building blocks of programs)
45+
# * Strings (letters and words)
46+
# * Conditionals (ifs and elses)
47+
# * Lists (sequences of items)
48+
# * Loops (whiles and fors)
49+
# * Dictionaries (pairs of items)
50+
#
51+
# You will find exercises throughout the material, signposted with @TASK. We
52+
# will finish the material with some extra tricky exercises for you to test your
53+
# skills.
54+
#
55+
# By the way — these lines starting with the `#` character? They're called
56+
# comments. They don't get run by Python — they're just for me to talk to you.
57+
# You can create one yourself if you like.
58+
59+
# Type your name as a comment on the next line.
60+
61+
# Hint: if you're on a Mac, type opt + 3 to get a #
62+
63+
# Now open up 011_identity.py and get started with some real code!

011_identity.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Video alternative: https://vimeo.com/954334266/1ad4327868
2+
3+
# Start reading here:
4+
5+
def just_return_it(num):
6+
return num
7+
8+
# This is a function. A function is a reusable block of code.
9+
10+
# Think of a function as a little machine. It takes an input, processes it in
11+
# some way, and then returns an output.
12+
13+
# This `just_return_it` function has:
14+
15+
# * A name: `just_return_it` that we can use to call it
16+
17+
# * A parameter: `num` that it takes as input. You might also hear these
18+
# informally referred to as 'arguments'
19+
20+
# * A body: `return num` that processes the input and returns the result as
21+
# output. Python uses spaces at the start of each line to indicate what's the
22+
# body and what isn't
23+
24+
# `just_return_it` is a very simple function, it doesn't do very much. It takes
25+
# a piece of data as input, and returns it as output.
26+
27+
# We call a function like this:
28+
29+
just_return_it(4)
30+
# Returns 4
31+
32+
# In the code above, the argument `4` goes into the `just_return_it` function
33+
# and takes the place of `num` in the function body.
34+
35+
# Want to see? Here, we can use `print` to show you:
36+
37+
print("just_return_it(4) returns:")
38+
print(just_return_it(4))
39+
40+
# @TASK: Run this program in the bottom terminal panel:
41+
#
42+
# python 011_identity.py
43+
#
44+
45+
TODO: reference video alternative
46+
# If you don't see the terminal, watch this video:
47+
# https://vimeo.com/954334352/1f0dee9379
48+
#
49+
# After running the command, you should see:
50+
#
51+
# ```
52+
# just_return_it(4) returns:
53+
# 4
54+
# ```
55+
#
56+
# This shows us that when we call the `just_return_it` function with the input
57+
# parameter `4` — it gives us `4` back.
58+
#
59+
# Now move on to 012_add_one.py

012_add_one.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Video alternative: https://vimeo.com/954334235/902b0b036d#t=0
2+
3+
# Let's create a more useful function:
4+
5+
def add_one(num):
6+
return num + 1
7+
8+
# Here is how it used:
9+
10+
add_one(6)
11+
12+
# And let's see it printed out:
13+
14+
print("add_one(6) returns:")
15+
print(add_one(6))
16+
17+
# @TASK: Run this code in the terminal:
18+
#
19+
# python 012_add_one.py
20+
21+
# `add_one` is a function (little machine) that takes a number as an input
22+
# parameter, adds one to it, and then returns the result.
23+
24+
# Now move on to 013_add_two.py

013_add_two.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from lib.helpers import check_that_these_are_equal
2+
3+
# Video alternative: https://vimeo.com/954334235/902b0b036d#t=84
4+
5+
# Now you try. Here's an exercise for you:
6+
#
7+
# @TASK: Write a function called `add_two` that:
8+
#
9+
# * Takes a number as input
10+
# * Adds two to it
11+
# * Returns the result
12+
13+
# YOUR FUNCTION GOES BELOW THIS LINE
14+
15+
16+
17+
# YOUR FUNCTION GOES ABOVE THIS LINE
18+
19+
# @TASK: To check your work, run this in the terminal:
20+
21+
# python 013_add_two.py
22+
23+
# This will run the test at the bottom of this file.
24+
25+
# If you have trouble, look back at `add_one` _very_ closely, character by
26+
# character, to see if you can see any differences to your version. Pay
27+
# particular attention to the `:` at the end of the first line, and the spacing
28+
# at the start of some of the lines.
29+
30+
print("Function: add_two")
31+
32+
check_that_these_are_equal(
33+
add_two(6),
34+
8
35+
)
36+
37+
# When you're done, move on to 014_multiply_numbers.py

014_multiply_numbers.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Video alternative: https://vimeo.com/954334235/902b0b036d#t=322
2+
3+
# So far we've seen functions that take only one input. They can actually take
4+
# two, or even more, inputs. Take a look at this:
5+
6+
# Notice two parameters with a comma between them
7+
def multiply_numbers(num_a, num_b):
8+
return num_a * num_b # That `*` means multiply
9+
10+
# And let's use it:
11+
12+
print("multiply_numbers(2, 3) is:")
13+
print(multiply_numbers(2, 3))
14+
15+
print("multiply_numbers(3, 5) is:")
16+
print(multiply_numbers(3, 5))
17+
18+
# @TASK: Run these in the terminal using:
19+
20+
# python 014_multiply_numbers.py
21+
22+
# Now move on to 015_add_numbers.py to write your own.

015_add_numbers.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from lib.helpers import check_that_these_are_equal
2+
3+
# Video alternative: https://vimeo.com/954334235/902b0b036d#t=444
4+
5+
# @TASK: Now you try. Here's an exercise for you:
6+
#
7+
# Write a function called `add_numbers` that:
8+
#
9+
# * Takes two numbers as input
10+
# * Adds them together
11+
# * Returns the result
12+
13+
# YOUR FUNCTION GOES BELOW THIS LINE
14+
15+
16+
17+
# YOUR FUNCTION GOES ABOVE THIS LINE
18+
19+
# @TASK: Check your work by running:
20+
21+
# python 015_add_numbers.py
22+
23+
# Below is a test for your function.
24+
25+
print("add_numbers(2, 3) is:")
26+
27+
check_that_these_are_equal(
28+
add_numbers(2, 3),
29+
5
30+
)
31+
32+
print("add_numbers(3, 5) is:")
33+
34+
check_that_these_are_equal(
35+
add_numbers(3, 5),
36+
8
37+
)
38+
39+
# When you're done, move on to 016_operators.py

016_operators.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Video alternative: https://vimeo.com/954334235/902b0b036d#t=606
2+
3+
# So far you've seen very simple computations. I'm going to show you how to
4+
# perform more advanced ones. Before I do, let's break down `add_one` a bit
5+
# further`. I'm going to give you some more terminology.
6+
7+
def add_one(num):
8+
return num + 1
9+
10+
# You may need to widen the panel or zoom out to see the table:
11+
12+
# | Code | What is it? |
13+
# | -------------- | -------------------------------------------------- |
14+
# | def | `def` is a keyword that defines a new function |
15+
# | add_one | `add_one` is the function name |
16+
# | (num) | `(num)` is the parameter list |
17+
# | num | `num` is a parameter |
18+
# | : | The `:` symbol indicates the body should start now |
19+
# | return num + 1 | `return num + 1` is a statement |
20+
# | num + 1 | `num + 1` is an expression |
21+
# | num | `num` here is a variable |
22+
# | + | `+` is an operator |
23+
# | 1 | `1` is a literal number |
24+
25+
# Don't worry about remembering all of that table, but pay attention now to
26+
# three items: operators, statements, and expressions. We're going to look at
27+
# all three next.
28+
29+
# First we'll look at operators.
30+
31+
# @TASK: To be a great programmer you will have to become a great researcher.
32+
# Let's get started:
33+
#
34+
# 1. Search the web for "Python operators", then...
35+
# 2. Find and fill out the following list of operators.
36+
#
37+
# I've started it for you.
38+
39+
# Addition
40+
added = 2 + 3
41+
print(f"2 + 3 = {added} (should be 5)")
42+
43+
# Multiplication
44+
multiplied = 2 * 3
45+
print(f"2 * 3 = {multiplied} (should be 6)")
46+
47+
# @TASK: For each section below:
48+
#
49+
# 1. Uncomment the code by removing the `# `
50+
# 2. Replace the `?` with the right operator
51+
# 3. Check it by running `python 016_operators.py`
52+
53+
54+
# == Subtraction ==
55+
56+
# subtracted = 2 ? 3 print(f"2 ? 3 = {subtracted} (should be -1)")
57+
58+
# == Division ==
59+
60+
# divided = 2 ? 3 print(f"2 ? 3 = {divided} (should be 0.6666666666666666)")
61+
62+
# This kind of 'decimal point' number, 0.6666666666666666 is called a float, by
63+
# the way, meaning 'floating point'.
64+
65+
# == Modulus == Sometimes known as "remainder if we divide 3 by 2"
66+
67+
# modulus = 3 ? 2 print(f"3 ? 2 = {modulus} (should be 1)")
68+
69+
# == Floor division == Sometimes known as "division without remainder"
70+
71+
# floor_divided = 2 ? 3 print(f"2 ? 3 = {floor_divided} (should be 0)")
72+
73+
# == Exponentiation == Sometimes known as "2 to the power of 3"
74+
75+
# expr = 2 ? 3 print(f"2 ? 3 = {expr} (should be 8)")
76+
77+
# There are many more operators in Python that you can research. You're very
78+
# welcome to try out a few below:
79+
80+
# OPERATOR PLAYGROUND STARTS
81+
82+
83+
84+
# OPERATOR PLAYGROUND ENDS
85+
86+
# Happy? Move on to 017_expressions.py

0 commit comments

Comments
 (0)