Getting g Started With Scratch

Download Report

Transcript Getting g Started With Scratch

Getting Started With Python
Brendan Routledge
[email protected]
07865 070322
What is Python?
• A widely-used programming language
• Focus is on code readability and syntax allows concepts to
be expressed in fewer lines of code
• Python is used by many major organisations including NASA
and YouTube
• It is one of the 3 programming languages which can be used
to write Google Apps
The Computing Curriculum
At Key Stage 1 children will understand algorithms, that programmes require
precise instructions, will create and debug simple programmes, predicting how
they will work and understand how technology appears in everyday life
At Key Stage 2 children will design, write and debug programs, use sequence,
selection, and repetition in programs, work with variables and different kinds of
input, be able to correct errors in programmes and algorithms and understand
how networks work
At Key Stage 3 learners will understand use of key algorithms that reflect
computational thinking, use 2 or more programming languages, one of which is
textual, design and develop modular programs that use procedures or functions,
understand simple Boolean logic and some of its uses in circuits and
programming
Key Aspects
• What is an Algorithm?
• The need for Precision
• The importance of Prediction
• Essential skills of Debugging
• Importance of Sequencing
• Efficiency of Repetition/Looping
• Use of Variables to extend functionality
The Iterative Process of Programming
ANALYSE
What is the problem
you need to solve?
DEBUG
Fix any problems and
improve it
TEST
Does your program do
what you wanted?
PLAN
Plan how your program
will solve the problem
PROGRAM
Use your plan to write/edit
your program
Working within the Python Shell
Select ‘Idle’ to open the
editor/interpreter
This editor is an area to try out
commands – what you write here
is not saved!
Try these – do they work?:
•
•
•
•
•
print(5)
Print(5)
print(hello)
print(‘hello world’)
print(“hello world”)
So ‘print’ means output something to the screen
Working within the Python Shell
Escape sequences are specific switches
to change the way things display
Try this:
print(“Question: What goes clip?”)
then
print(“A one legged horse”)
then try
print(“Question: What goes clip?\nAnswer: A one legged horse”)
\n
\t
\\
\”
creates a return line in a string of text
creates a tab indent in string of text
allows a backslash to appear in string of text
allows speech mark to appear in string of text
Working within the Python Shell
Try these – do they work?:
•
•
•
•
input()
input(What is your name?)
input(“What is your name?”)
input(‘What is your name?’)
So ‘input’ means input something from keyboard
Working with variables
A variable is a container which stores
some data for use later or elsewhere.
A variable needs a name so that it can
be identified when it is used.
Call your variable ‘a’ and give it a value
a=5
Ask Python what ‘a’ is by typing ‘a’ and
it will return a value
Variables can also be text strings –
a = “hello world”
So a variable is a way of storing data for use in a program
Working with variables
Variables need more suitable names to
identify them.
myName = “Brendan”
faveFood = “Fish”
type ‘myName’ – does it output the
expected value of the variable?
Variables should be carefully named when used in your program
Python for Maths
Can use Python as a mathematical tool.
Try typing 10+10
Use + - * /
Make this into a variable
sum = 10+10+10
Try some other calculations
Python can carry out many mathematical operations
Using repetition
To use repeated commands (loops) in
Python is easy.
number=1
while number < 101:
print (number)
number = number+1
Task – write a short program which counts
up to 100 in 5s
Was your program:
number=0
while number < 101:
print(number)
number = number+5
Python handles repetition and looping easily
Using repetition
Try creating a text string which loops
lines=0
while lines < 50:
print (“I will not write code in Art lessons”)
lines = lines+1
Try some of your own ideas
Python handles repetition and looping easily
Creating and saving a program
From Python Shell window, select File > New File to open new window
Save your program with a new filename
Write a very short program e.g.
print(“Hello this is my very first program in Python”)
then run it by pressing F5.
Write a simple function, as follows:
Start with a comment – denoted by a # at the start of the line
Script could be something like:
# This program will say hello to the user
def main():
print(“What is your name?”)
yourName = input()
print(yourName)
Run program – at the prompt type
main()
Creating and saving a longer program
From Python Shell window, select File > New File to open new window
Save your program with a new filename
Write a short programme which defines a function called times_tables and which
allows you to see any times table.
Start with:
def times_tables(num):
n=1
while n <= 10:
print(n, "x", num, "=", n*num)
n =n+1
How would you make it display a different partof a times table, say from 20 to
30?
An even longer program….
We are going to make a Number Guessing Game.
Computer will think of a number and player will try to guess it
Copy the code from the sheet very carefully.
As you go try to figure out what each bit of the code does in the game
Two new things in this script:
1. The ‘return’ keyword – tells the is_same() function what value should be sent
back when it is called. As well as sending data to functions they can also return data.
Here it returns the value of the variable result
2. Converting user’s input into an integer – this is done by wrapping it in
int(input……). This is because anything coming from keyboard input is received
as strings. So it has to be converted to a different type of data – this is called casting.
Some useful Python resources
Python Scheme of Work for Upper KS2
http://primarycomputing.co.uk/2013/08/21/python-scheme-of-work-for-upper-key-stage-2/
Coding at School
http://codingatschool.com/python/
Python Programming Guide – Simon Haughton
http://www.simonhaughton.co.uk/2013/07/python-programming-guide.html
Python Tutorial for Schools
http://www.pyschools.com/
Coding Club
http://codingclub.co.uk/index.php
Learn Street
https://www.learnstreet.com