Download Try typing the following in the Python shell and press return after

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Date: ___________
Name: ________________
PYTHON - INTRODUCTORY TASKS
Instructions:
Open Idle (the program we will be using to write our Python codes).
We can use the following code in Python to work out numeracy calculations.
Try typing the following in the Python shell and press return after each
calculation.
Write the answer the program displays next to the sums below.
300+400
987-653
12*9
30/6
Tick here if you managed to do it
Instructions:
Next, select File > New Window to create a window which you can code into and
save
Click File > Save and name the file helloworld.py (make sure you’ve saved it to an appropriate location - ask your teacher if you are unsure).
Practice task - when using the statement “print” programs will display the text within the
quotation marks. Try this by writing the following code:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
print (“Hello world!”)
TIP: Python is case sensitive so check you have used a lower case letter p! If
using version of Python 3.3 or later you must use brackets after the print
command.
Run a simple hello world program by selecting ‘Run’ then ‘Run Module’.
TIP: Remember to run a program you must first save the changes you’ve made to
your file.
NOTE: The program will run in the ‘outer’ Python window we used earlier (the Python
shell). If you have typed the code correctly the program will display the words ‘Hello
World!’
Tick here if you managed to do it
Well done you have written your first Python program!
Can you write a program which displays longer text and on different lines? For
example:
Hello, how are you today?
I hope you are enjoying this lesson.
What shall we code next in Python?
TIP: Use the ‘print’ statement on each new line to display text over several lines
Tick here if you managed to do it
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
Date: ___________
Name: ________________
PYTHON - TASK SHEET 2
Instructions - Declaring variables:
Variables may be thought of as boxes within our program where we can place data
(numbers or text). We can then use the contents of the boxes within our program
and the values assigned to our variable may change as our program runs.
When we create a variable and assign data (get a box and put something in it) it is
called declaring a variable. To declare a variable we use the following code for
example:
variable1 = 6
In this example above we have created a variable called ‘variable1’ and
given it the value of 6. We can create more than one variable and we can
assign text as well as numbers to variables
e.g.
name = John
score = 6
Q. What are the 2 variables that I have created using the code above?
________________________________________________________
Q. What is the value of each variable?
________________________________________________________
Your teacher will now reveal the two answers. Tick here if you got them correct
……………………………………………………………………………………
Instructions - Assigning variables:
Now create a new Python file called variables.py
a) Create a variable called ‘dogs’ with a value of 12 and a variable called
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
‘biscuits’ with a value of 3. We will be using these variables to write a
program that calculates the total number of biscuits required to give x
number of biscuits to x number of dogs.
Your teacher will now reveal what you should have typed. Tick here if you got it correct
Instructions - Comments
A comment is a line of code that isn’t part of the program but is there to explain to
the programmer what parts of the code are doing. We write comments in regular
English (or whatever language we as programmers may speak) as opposed to the
language of Python. Comments are important as when we write longer pieces of
code we may forget the function of different parts, or we may work on code with
others and therefore we need to explain what we are doing.
However, the trouble with writing in regular English within our program is that our
computer thinks we are still writing in Python and may try and interpret commands
from what we have said. As such, we need to indicate when we are writing a comment and we do that by using ‘#’. The computer will ignore any line starting
with # as it knows this is a comment to the programmer and not part of the code.
b) Can you now add a comment, using the # symbol for the player and points variables you have created to add an explanation about what we are coding?
Your teacher will now reveal what you should have typed. Tick here if you got it correct
Instructions - Using mathematical operations and print statement with variables:
Task: The purpose of this program is to work out the total number of biscuits
required based on the two variables we have declared: the number of dogs and the
number of biscuits they each get.
To write the program to calculate this, add the following code beneath your
variables and comments
total = dogs*biscuits
print total
c)
Q. What new variable is being declared?
Q. What is the name of this new variable?
Q. What mathematical operation can you see here?
Q. How would you explain this section of code, what is it asking the
computer to do?
Q. What do you expect the output of this program to be?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
d) Now save your program and select ‘Run’ then ‘Run Module’. Remember,
the window you have been coding in will close and your program will run in
the ‘outer’ Python window we used earlier. If you have entered the code
correctly your program will display the total number of biscuits required for all
dogs - 36
Tick here if you have achieved this
Extension: Practice editing your program by changing the values for the two
variables.
TIP: To modify your program you should return to the window it was written
in and make changes before selecting ‘Save’ then ‘Run’ then ‘Run Module.’
……………………………………………………………………………………….
a) Can you write a program to calculate the number of biscuits needed
for a children’s party if there are 30 children and they each get 4 biscuits each?
Tick here if you have achieved this
b) Can you write a program to calculate the total number of points 4
teams got in a tournament. Choose your own numbers for how many
points each team got!
Tick here if you have achieved this
c) 5 students make £420 by selling their products. Their total costs
were £100. Write a program to calculate how much money they make
each after they have paid their costs.
Tick here if you have achieved this
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
Name: ________________
PYTHON - TASK SHEET 3
Date: ___________
Python has statements, which can be used to accept an input of data from the
user. The data which is entered can be used to declare variables. So in other
words, rather than saying what the value of the variable is, we can ask the user to
enter the data instead.
For example, you could type in the following code:
name = input ("hello, please enter your name: “)
print ("hello, nice to meet you " +name)
(Tip: I’ve left a space before the last quotation mark to give the user space to enter their
name).
Q. What do expect to happen when this program is run?
Now try running the program and see what happens.
Q. What happens? Was your prediction correct? YES/NO
TASK: Now try programming a different question which requires the user to input
text data, such as the one below.
age = input ("how old are you? ")
print ("wow you are " +age)
Write your own similar code below. Then when you are happy with it type it into
Idle and select Save (you could save it as input.py) then select - Run - Run
Module.
TIP: Just like the example above, make sure;
a) The question is inside quotation marks
b) The question is also inside brackets
Tick here if your program ran correctly
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
Inputs and mathematical operations:
You are now going to write a program which will:
1. Allow the user to enter the number of children in a class
2. Allow the user to enter the number of house points they require each
3. Output the total number of house points required.
TASK: Jot down below how you could tackle this coding problem.
TIP 1: When typing in the maths calculation you want Python to do, you will
need to declare that these are numbers (intergers), otherwise it will
recognise them as text (a string) and it won’t work. Here is an example…
children = input ("how many children are there?")
sweets = input ("how many sweets do they want?")
total= int(children)*int(sweets)
print (total, “is the number of sweets you will need”)
TIP 2: Remember if you want the user to type something in you must use the
code
=Input (
)
TIP 3: Remember to use # symbol for comment lines (this is to help you or
others and doesn’t form part of the program).
IDEAS - jot your solution ideas below
Now try running the program.
Q. Is the user asked for the number of children and the number of house
points each child requires before the program then outputs the total number
of house points required?
YES/NO
TASK 1: Can you write a program which asks the user to enter how many
children are in a class and how many children are in each group? The
program should then output how many whole groups there will be.
TASK 2: Can you write a program that asks the user how many days there
are until half term. You will need to input the right code so that the program
output displays this in seconds?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
Date: ___________
Name: ________________
PYTHON - TASK SHEET 4
Conditional statements
What the code does sometimes depends on a condition being met or not met.
These are called conditional statements (similar to using an ‘if, else’ block in
Scratch).
The table below shows the syntax (python language) for comparison operators
(what we are comparing the condition to). We will require these to work with our
conditional statements.
The short piece of code below uses conditional statements and comparison
operators. Try inputting the code below into Idle and then save your program and
select ‘Run’ then ‘Run Module’
#Declare name variable using input from user
name = input (“Hi, What is your name? ”)
print (“It is lovely to meet you, ” +name)
# Declare feeling variable using number input from user
feeling = int (input (“How are you feeling today? Excited =
1, Happy = 2, Sad = 3”))
# Use conditional statement with ‘equal to’ comparison to
determine which response is given
if feeling == 1:
print (“Fantastic. I wonder what you’re excited about?”)
elif feeling == 2:
print (“I am happy too. It’s a great day!”)
elif feeling == 3:
print (“I am sorry to hear that you are miserable. How
can I cheer you up?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
TASK: In your own words, can you explain below how
the above code works?
Programming a calculator: Here is a second example which uses a range of
comparison operators. In this example the user’s inputs also cause the program
to perform mathematical operations on the variables declared, like a calculator.
Try inputting and running the following code in Idle…
num1 = input (“Please enter a number”)
num2 = input (“Please enter a second number”)
if num1 > 100:
print (“Your first number is greater than 100”)
else:
print (“Your first number is less than 100”)
if num2 > 100:
print (“Your second number is greater than 100”)
else:
print (“Your second number is less than 100”)
op = input (“Select an operation: 1. Add 2. Subtract 3. Multiply”)
if op == 1:
add = num1 + num2
print add
elif op == 2:
sub = num1 – num2
print sub
elif op == 3:
mul = num1*num2
print mul
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.
TASK: In your own, words write an explanation below of what is
happening in this piece of code.
TASK: Can you program a French dictionary that provides the user
with a range of English words to choose from and returns the translation in French? Plan it out below.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by JEcomputing.