Python Learning Resources

←back to posts

Python Learning Resources

13th October 2017

Start your very first python sketch! To start your sketch follow the instructions below! You can either work from or use the editor down below! Joining Trinket is free!         Let’s make a flying rocket ship!   STEP 1 — GETTING STARTED In this lesson we are going to code a rocket to fly around space. screen.setup(400, 400) is the size of our canvas on the right. (400, 400) means that our canvas is 400 pixels wide and 400 pixels high. We’ve made our canvas the same size as the background image we have loaded.   The lines beginning with # are comments. We use comments to write explanations or labels to organise our code and make it helpful for anyone reading to understand it. Try writing a comment! The line should turn green if it works.     STEP 2 — CREATING VARIABLES To make our rocket move, we need variables! Variables are special containers that we use to hold values. Here we’re making a value for how fast we want our rocket to move! On lines 15 and 16, write the code in the picture. We are making two variables, move_speed and turn_speed.       We can make as many variables as we want and name them whatever we want. It’s always good to name our variables based on information we’re storing.     STEP 3 — DEFINING FUNCTIONS   Now we have coded our variables, we need to define some functions that will use them. Functions are bits of code that we write so we can use them whenever we want. On the right you will see the code that we need. ‘def’ means “define” as we are defining our functions.       We are defining a function called “forward” and “turtle” , which is our rocket ship. turtle.forward(move_speed) means that when we use our function forward, our rocket will move at the speed of move_speed.   There are three other functions that we need, can you fill in the blanks?                             ANSWERS Well done! Here are the answers to the code. After adding this to your program, if you press the play button, you’ll notice that nothing happens when we press our keys. We need to code our program to call our new functions when we press the arrow keys.                           STEP 4 — USING OUR ARROW KEYS   Starting on line 31, let’s code our program to work with the arrow keys. screen.onkey(forward, “Up”)  means that our computer will be checking to see if we use our Up arrow. If we press the  up arrow, it will call our function forward.       We need 3 more commands to react when we press the left, right, and down keys. Can you fill in the blanks?                         ANSWERS   Did your code look like this?               Well done, you have coded your first interactive program with Python! Now press play and we will be able to move and turn the rocket ship with our arrow keys.   Heres a finished example below:

Extension activities 

Python is commonly used amongst budding programmers. It is a powerful program that has many uses and is used all around the world.   If you feel like expanding your Python knowledge try some of these exercises: