Equipo Academy’s College Simulation Day
ROGUH
2019.11.27
(you can use a smartphone if you don’t have a laptop)
youtu.be/Ct-lOOUqmyY
(Follow the rabbit hole)
$ python 1.0.py
Hello,Alice
# 1.1.py
name = "Alice"
message = "Hello, " + name
print(message)
print("Hello, " + name + "!")
$ python 1.1.py
Hello, Alice
Hello, Alice!
favorite_foods = ["TAMALES", "nigiri sushi", "nutella"]
number_of_favorite_foods = len(favorite_foods)
index_of_favorite_food = 0
print("of my ",
number_of_favorite_foods,
" favorites, ",
favorite_foods[index_of_favorite_food],
" are my most favorite food")
of my 3 favorites, TAMALES are my most favorite food
>>> len("Hello!")
>>> [1, 2] + [3, 4]
The three arrows >>>
mean try it in the REPL.
Python supports slices.
definitions:
favorite_foods[0]
favorite_foods[0:3]
most_states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado"]
top_states = most_states[0:3]
print(top_states)
print("Hello, world!"[0:5])
print("Hello, world!"[0:5])
Hello
print("Hello, world!"[-1])
print("Hello, world!"[-4:-1])
print("Hello, world!"[-4:-1])
Let’s use CodingBat!
hello_name
in Codingbat module String-1make_abba
make_tags
Look at each element using for x in elements
.
numbers = [2, 4, 8]
total = 0
for number in numbers:
total = total + number
# MUST BE 4 SPACES!!!!
highest_known_power_of_two = numbers.pop()
numbers.append(6)
numbers.append(8)
numbers.append(10)
print(total)
print(numbers)
Output:
14
[2, 4, 6, 8, 10]
greetings = {
"english": "Hello, ",
"español": "hola, "
}
greetings["sinhala"] = "හෙලෝ "
greetings["es"] = greetings["español"]
greetings["en"] = greetings["english"]
print(greetings["en"] + name)
Dictionaries assign keys to values.
Traceback (most recent call last):
File, line 8, in <module>
NameError: name 'name' is not defined
too bad :(
lists can be thought of as “weaker” dictionaries: think of a list as a dictionary with its keys set to numbers 0, 1, 2, …, len(list)-1
list = ["a", "b", "c"]
list[0]
silly_dictionary = {-1: "negative", 0: "zero", 1: "one", "1": 1, "one": 1}
print(dictionary[0])
print(dictionary[-1])
print(dictionary[1])
print(dictionary["1"])
def greet(person)
message = "Hello " + person
print(message)
you defined the function “greet
” but didn’t do anything with it! (that’s ok)
def run_code(instructions):
names = dict()
stack = list()
for each_step in instructions:
instruction, associated_data = each_step
if instruction == "PRINT":
value = stack.pop()
print(value)
elif instruction == "STORE_TO_VAR":
value = stack.pop()
names[associated_data] = value
elif instruction == "LOAD_VAR":
stack.append(names[associated_data])
elif instruction == "LOAD_LITERAL":
stack.append(associated_data)
elif instruction == "ADD_LAST_TWO":
result = stack.pop() + stack.pop()
stack.append(result)
else:
print("ERROR: unknown instruction", instruction)
run_code([ ["LOAD_LITERAL", "Alice"],
["STORE_TO_VAR", "name"],
["LOAD_VAR", "name"],
["LOAD_LITERAL", "Hello,"],
["ADD_LAST_TWO", []],
["PRINT", []] ])
# CHALLENGE: MAKE THIS INTERPRETER INTERPRET ITSELF!!!!
hints:
names
is an empty dictionary, stack
is an empty listHello,Alice
See full source code at roguh.com/intro_to_cs.equipo.1127/python.py.
See aosabook.org/en/500L/a-python-interpreter-written-in-python.html for my inspiration.
It supports relatively advanced code for 117 lines of Python. The fun is when you go and make it more powerful.
run_code(parse_string("a = 1\na = a + a\nb='birds'\nprint(a, ' ' + b)"))
All you need is a computer and some time.
Reach out to me with any questions you have!
Python is friendly and has many real-world uses.
Try JavaScript! Or C if you want more control over the machine, or Haskell if you want to peek into the future of programming.
Learning resources are bountiful. Ask me if you want advice.
Route 0: install Arch Linux and use the vim and git command line tools
Route 1: use visual studio code and nice collaboration plugins (GitHub)
Route 2: use websites like CodingBat and other online coding platforms
magically somehow friendlier than Python, but I don’t like the language
Learn from Mike Bostock, https://observablehq.com/