Introduction to Python
In Scratch we use the same coding tools and concepts as we do in written languages, the main difference is that we are dragging and dropping blocks instead of typing. Python is a popular programming language that is widely used around the world (it's what Mr Tyler used primarily when he was a coder himself).
Over the next few periods we will be learning some of the basics of coding with Python. To do this we will be comparing what we already know in Scratch and making similar versions in Python. |
Getting Started
Task One: Starting nice and simple, go to this online editor: https://www.programiz.com/python-programming/online-compiler/ and change the text to make it say hello to someone. Task Two: Once you've got it to say hi to a friend, change the contents of the print statement to make it say other things. |
Using variables
In coding, a variable is a re-usable component that can hold information, kind of like a bucket. Just like we can use a bucket to hold water in one moment and sand the next, we could use a variable to hold text one moment, then a number the next.
In coding, a variable is a re-usable component that can hold information, kind of like a bucket. Just like we can use a bucket to hold water in one moment and sand the next, we could use a variable to hold text one moment, then a number the next.
In the example on the right we are using a variable called firstName to hold a name, then on the next line we use that variable to say hello to them.
We can re-use this variable again in another sentence like on the right. NOTICE how the text and variable are joined together using commas. Task Three: Create 2 variables to store 2 names of people. Use three print statements to simulate a conversation between the two people. |
Taking Input
Most of the time we want our code not only to give output but to take input as well. To do this we use an input statement like the example on the right. It is a convention to use something like "> " in the input() to make it obvious to the user that we want them to enter something. Task Four: Use the input example to store 2 variables: a name, and a number. Write a print statement telling the person that they owe you the amount of money stored in the number. |
Using numbers to do maths
When we take input from a user, Python assumes that it is just plain text. Because of this we have to tell Python that we are using a number using the int() function like in the example on the right. This is called "casting" - we cast a variable from one type to another. Task Five: Write code to take in two numbers from a user. Make 4 print statements: a) add the two together b) subtract one from the other c) multiply the two together d) divide one by the other |
Using Conditions (if/else)
If/else statements (just like in Scratch) are commonplace across all (or at least nearly all) programming languages. In Python these look like the example on the right. Task Six: Write code to take in a number from a user. If the number is less than 8 it needs to tell them it's too low. If the number is higher than 8 then it should tell them that it's too high. If they enter 8 then it needs to congratulate them and tell them a funny joke. Hint: you might need to look around to figure out how to do 2 comparisons. |
While Loops
Often in code we want things to happen multiple times. For example, in Scratch we might code a cat to continue walking towards a mouse until it catches it. One way of doing this in Python is with while loops. Task Seven: Make a password checker, if the user enters the wrong password then it tells them the password is wrong and asks them to enter it again. |
Python assessment - Quiz
Make a quiz program using Python, the quiz can be on anything you like, some ideas to get you started are: animals, countries, tv shows/movies, video games, sports, food.
Your quiz needs to have at least 4 questions (but can have more). The final question in your program needs to be answered correctly, the code needs to keep asking the user until they can give the right answer. If you aren't super confident yet in Python it is a good idea to make the quiz first using Scratch and then translate it into Python. The document below is a handy resource for seeing the Scratch version and Python version side by side - it might be helpful for you! (Credit Burnside High School for the document).
|
Grok Academy
Grok Academy have a bunch of free tutorials to learn to make some cool things using Python. Once you're finished with the rest of the content on this page, let your teacher know so that they can register you with an account and get you going with it.