So you took the CoderDojo Workshop on Python and want to know where to go next?
Well lets start with what did you learn in the workshop. You learnt a little about Python syntax (the code you have to write to make things happen in your project), as well as variables and setting their value. You also learned about a useful mathematical tool called modulo, which allows you to calculate the remainder in a division. For example 5 can be grouped (divided) into 2 groups of 2 with 1 leftover. This is usually called the remainder. It’s not in all programming languages but for those that it is in, it is usually written as a ‘%‘, ‘mod‘, or ‘rem‘. In Python it is ‘%‘. So the example above is 5 % 2 = 1.
And also repetition using different types of loops, like while and for.
And using lists to keep track of many items in one bundle.
This gives you a great foundation for doing things in Python, but there is a lot more to go.
There are two very important parts of Python that we haven’t tackled yet. Conditional statements which allow you to control if something gets done, and functions which allow you to build code you can reuse multiple times in your project.
Sometimes in your code, you want to only run a certain part of the code when something special is happening. To do this, you ask a question: “Is something happening?”. This question is referred to the condition, and if this is true, then you run the code, if it is not (false) then you don’t. In most languages, this is referred to an if (or sometimes if/then) statement. Often though, you need to run certain code when your condition is true, and other code when it is not. In this case, you have the else. This will run the code, if the condition is false. In many languages, you test to see if numbers are equal using ==.
if x == 5:
print ("x is 5")
print ("x is not 5")
Sometimes you need to have an even more complex flow, and in those cases you need to use many if and else statements chained together. For example you may want to do one thing if your something is an apple, something else if it is a banana, and a third thing if it is not either of them. N In Python, there is a special way of doing this to make writing them easier, using the elif.
if x == "Apple":
print("x is an apple")
elif x == "Banana":
print("x is a banana")
print("x is not an apple and x is not a banana")
Conditional Statements are very useful in your programming. If you started with Scratch, you may well be familiar with them. To practice using conditional statements, try out the following projects.
When you have some code that you want to use multiple times, you probably have just written it multiple times (or maybe your a bit of a wizard, and are copying and pasting). Everything works great, right! At first it works great, but after a while you have a very very big project that is really hard to keep track of whats happening. This is can be a big problem when something goes wrong. It can also be a real pain when you want to change what that bit of code does. You have to change it in many places. If you miss one you can get weird things happening in your project. Enter Functions.
Functions are a way of putting that bit of code aside so that you can use it in the rest of your project without having to write it out every time. The way they work is a bit like the code that runs in a loop. Take our loop code in the image above. You didn’t have to write out turtle.forward(1) and turtle.right(1) hundreds of times, you only had to write it out once. But instead of having to do it many times at once like in a loop, with a function you can run the code at whatever place you need.
The description above doesn’t actually say everything a function can do. So lets describe it.
- It runs code
- The code can contain variables, loops, values. Anything that you can put into your main project, you can put into a function.
- It can use parameters
- Parameters are variables you can use in your function’s code (if you need to) so that your code can run slightly differently each time. For example. Maybe you want to add 3 values together, 3,5, and 7. But the next time you want to add 4,6, and 8. You could make 2 functions. In Python, you create your functions using the def keyword.
print(3 + 5 + 7)
print(4 + 6 + 8)
A better way is to use parameters. Parameters help you allow you to write one version of your code that can do both of the calculations. In this example we have three called number1, number2, and number3.
def add(number1, number2, number3)
print number1 + number2 + number3
This makes it so you can use the code in many more places.
- It returns a value
- These values are things you can send out of your function and use them in the rest of your code. Maybe you don’t just want to those values together, maybe you need to do a multiplication afterwards. Returning a value allows you to do this. You can put them into other variables, or just use them in your code.
def add(number1, number2, number3)
return number1 + number2 + number3
x = add(3,5,7) * add(4,6,8)
In other programming languages, functions can go by different names. Sometimes these names mean that the function acts differently to what we described above. Some common names you might see are Subroutine, Method, Lamdba, Delegate.
The next step is to try out functions using the following projects: