Day 3
Math functions
- Takes something in, does something to it, then returns the processed thing
- Takes in one/multiple argument(s)
- Note: the concept of programming functions comes from math functions
- Programming functions are more general, can take in more things than just math things
Lists
Creating lists
my_list = ["thing", 2, "thing other]
# initialise a list called "my_list" with contents thing, 2, and thing other
List indexing
my_list[0]
>>> "thing"
- Note: List indexing STARTS FROM ZERO
Adding things to lists
my_list.append(3)
# adds 3 to the end of the list
my_list.insert(0, "Sub to Sherman")
# inserts "Sub to Sherman" to the beginning of the list
- .append() adds things to the end
- .insert() adds things to a specific position
Removing things from lists
my_list.remove("thing")
# removes "thing" from my_list
my_list.pop(1)
# removes what is indexed at 1 from the list and returns it
Functions
Defining a function
def function_name(argument):
do something(argument)
Calling a function
function_name(x)
Turtle
Initialising the turtle and the screen
import turtle
s = turtle.getscreen()
t = turtle.Turtle()
It is good practice to establish both of these at the start of each program.
Other commands in Turtle
For more information on Turtle, we recommend that you read the official Turtle documentation, or Google any specific questions you might have. Feel free to ask me (Daisy) any questions you might have about Turtle, although I cannot guarantee that I'll be able to answer them.
Project
Using Turtle, create the most impressive program. Your program must include the following elements: functions, lists, conditionals, loops, variables, and user input. The user must be able to control some aspect of the drawing (eg. colours used, thickness of lines…)
Upload the file of your final code to a folder in Google Drive and make the folder viewable by everyone, then post the link as a comment to the Google Classroom post.
Sample solution to the project
As the sample solution I created was rather long, I've decided to not attach it to today's post. Instead, it is viewable on our class GitHub repository. Note: some of the functions used here have not been covered in class, and the structure of the program is probably not as well thought out as it could be. If you find the code confusing, feel free to come to me (Daisy) at some point in class, and I'll do my best to talk you through it.