- Reflect on the advantages of learning to code.
- Understand why Python is an ideal first language
- Get started using Python.
All must complete a “Hello World” program, use Python to ask a question and print the answer to the screen. (Level 5)
Most should complete the above but adding some meaningful comments to the code. Use Python to perform simple maths. (Level 6)
Some could complete the above, as well as adapting a program to ask the user for a question, then perform a calculation on the given number. Use detailed comments in code (Level 7)
Words to learn: coding, program, input, output
Watch this video below which shows the importance of coding:
Now discuss and write down 4 reasons why everyone should know how to code. If you finish quickly, hit the back button on your browser and look why Python is an ideal first language to learn properly.
Let’s get started with Python. Some of you will have seen this before, but it’s a very good idea to revise the basics before we make the new Call Of Duty!
First make a folder called “Python” in your (H): drive. Now click the windows button, then “All programs”, then “ICT programs”, and finally, the “Python 3.4” folder. In there you’ll see a program called “IDLE (Python GUI)”. Open it up and you should see this:
**Important** This first window that you can see is called the Python “Shell“. This takes care of running your programs. It is not intended for writing your programs in! If you start to type your code here, it will try to run each line you type, and soon give you some very interesting error messages!
Make sure you type exactly what you see…
Now click “Run”, then “Run Module”. You’ll be asked where to save your program. Make sure you call it “hello” and save it to your (H): drive (in your Python folder).
Now, your first program runs! Well done! The program outputs our message to the shell window.
You can try this with your own messages if you like…
Run this, and you’ll see the first line is printed out, and the program waits at the second line-
It will pause here until you type something and hit return. Whatever you type will be saved in a block of memory with the label “veg”. (This is called a variable – more on those next lesson!).
Now the program will run the third line where it prints whatever value is in “veg” followed by the message “is a really tasty vegetable”.
Now answer these questions in your book/folder:
- I’ve just had a great idea for a program. I’ll open up Python and type it straight into the shell. Is this OK? Why?
- What happens when I type (exactly):print “I love computing”)
How do I make it work?
- Write a program that asks for a name, and outputs a welcome message, including the nameFor example:Computer:”What’s your name?“.
I type “Sally“,
Computer: “Welcome Sally“.Test your program before you write it down.
- Add to the program above so that it then asks “How are you?“, takes my reply (for example “great“), and responds with “I’m glad that you are [my reply]“.Extension:
- Write a program which asks for the user’s name, then age. It stores each value (as above) then outputs:
Your name is [user’s name] and you are [user’s age] years old!
Python Does Maths for you!
Start a new program (click on “file”, then “new window”), and type:
Now run this, saving it as “sums”. Great! Python solves the problem for us. Try this:
Python now does subtraction as well! This is really useful in more complicated programs, but there’s more…
Try these lines of code:
Questions (write the answers in sentences in your book/folder):
- in the sample code above, what did each of the symbols, + ,- ,/ ,* mean?
- Try this:
Now change the numbers a little and see if you can guess what ” ** ” means (clue- think of power…).
- What about this?
(clue- whole numbers)
- Finally, the strange one:
print(13%4) (Clue – leftovers!)Extension:
- Write a line of code which outputs “2+2=4“. Sound easy? You can’t use the number 4 in your code!!
- Now change the program so that it asks me my name, and then outputs
“[my name], 2 plus 2 is obviously 4 !”Again, you can’t use number 4!!