Python
Year 7 - Lesson 1
Python
Year 7 - Lesson 1
KEY OBJECTIVE
To program a computer using the python programming language
PRIMM
We will be using the PRIMM approach when we learn to code
Predict
Run
Investigate
Modify
Make
Task 1 - PREDICT
What do you think this code will do?
What will the code do?
RUN
INVESTIGATE
NOTE: everything that is indented under the defined procedure belongs to it. In this case only one print command but it could be several lines of code
MODIFY
Using the code you have try and:
make it say a different message
Make the message print three times by calling the procedure three times
MAKE
Create your own program that will:
Have a procedure called "computing" that prints the message "I love computing"
In the main code call that procedure
What is Syntax?
The way we write a line of code is important, as the computer needs to recognise it.
Think about our 'English'. It is important that we use correct punctuation, such as Commas and Full Stops. If we didn't, it would make our writing difficult to read.
If we do not use the correct symbols and spellings in our Python coding, then the computer will not be-able to run our programs.
What is debugging?
Debugging code refers to the process of identifying and fixing errors, bugs, or defects in a computer program that we have created.
An error occurs, when you have done something wrong in your program. You could have spelt something wrong or even missed out a symbol...
Debugging Continued...
Create with code, shows you where your error is. Below is a screenshot that shows this.
Task 2 - Debugging
In this task, you will need to debug the code using the error messages that create with code provide.
Variables
In programming, a variable is like a container that holds a piece of information. This information can be anything, like a number, a word, or even a whole sentence!
For example, imagine you have a box that can hold a piece of fruit. You can put an apple in the box, and then later on, you can take the apple out and look at it or even swap it for a banana. In programming, a variable is like that box. You can put information into the variable, and then you can use that information later on in your program and change it as you please.
To use a variable, you first need to give it a name. This is like labelling the box with the word "fruitbox" so you know what's inside. In programming, you can choose any name you want for your variable, as long as it follows certain rules. For example, the name can't have spaces in it, and it can't start with a number.
Task 3 - Variables - Predict
PREDICT
What do you think the code will do?
RUN
INVESTIGATE and MODIFY
Using the code try the following:
make the program call the other two procedures
Change the name in the printing2 procedure to your name
(Harder) Add more text after the greeting in printing3 so it will print the following --> "Hello Sally , nice to meet you"
Taking input
We can ask the user for inputs into our programs.
So lets get the computer to do this
PREDICT
What do you think this code will do when run?
INVESTIGATE AND MODIFY
Try and do the following:
change the procedure name for printmyname() to printname()
add two more input questions to the askmestuff() procedure about a favourite hobby and place in the world and store them in suitable variables
add another print statement to the same procedure to print suitable text using the new stored variables.
MAKE
Create your own program that:
has a procedure that asks 2 questions and stores them as variables
calls this procedure from the main code
results in a sentence being printed, using the stored variables
Syntax errors?
Let's test some of those error checking skills
On the next drag and drop which bits of code are correct and which would cause an error?
CORRECT
CAUSE AN ERROR
print = ("Hello")
Print("Yes!")
print("this one)
name = imput("what is your name")
name = input("what is your name")
print("this text")
fav colour = "Green"
age = 10
print("Hello", name)
print("Bye",name")
Commenting our code. What is the importance?
By commenting our code, it tells you (the programmer) and anyone else who looks at the code what it does.
By using the '#' symbol, it will not display when your code has been run.
You have been seeing them since we started but you have to start using them to help you explain what you are doing
What symbol do I use to comment code?
What is the process of checking a program for errors by running it and fixing the issues?
Error location
De-bugging
Error finding
Code correction
What type of information can be stored in a variable?
Only text
Only sounds and images
Only numbers or text
Numbers, text, and other types of data
Fill in the blanks
Python is a text based coding environment when you can write your own programs. It is important to do a procedures first approach as these are like a menu of the coding blocks available to the coder. When you want to use any block of code you just call it by writing its name followed by ().
A variable is a value that can change when the program is running. A good example might be your health in a shooting game. We must always declare variables so that we can store values in them before we then use them later in the program. A common way to output this to the screen is to use the print function.
Fixing errors in our code is known as debugging. Most common errors are typos where we break the rules of the programming language. Other errors like getting indentation levels wrong also cause many errors. Check these carefully. It is also always good practice to add comments to your code to explain what it does.
Recall task
Write a program that:
Asks for your name and stores it
Asks for your favourite colour and stores it
Asks for your favourite animal and stores it
Prints the sentence below, filling in the blanks with the 3 stored values:
Hi there name . I wonder if you have ever seen a favcolour favanimal animal before?