Tg-Lesson 1
Tg-Lesson 1
Lesson 1
1
Starter
• Create the following Excel spreadsheet and
complete the calculations using formulae:
Add A1 and B1
A2 minus B2
A3 times B3
A4 divided by B4
2
Objective of the lesson
Write simple programs using Python.
3
Homework
• Everybody should complete Tasks
1 to 3 to download Python and
create the program to email to
your teacher.
• Some of you may also want to get
the extra marks by completing the
extension activity.
• This homework is due in next
lesson.
• Make sure you have written your
homework clearly in your planner.
4
Python
• Python is a programming language which was
developed to help you learn programming.
• You will learn about the basics of
programming using Python.
• To use Python load Python (IDLE).
5
The Python Shell
• The first screen you see is called the “Shell”.
• You can type in simple commands straight into the
shell.
6
Typing into the Shell
• Type in 4+2 at the >>> prompt and hit your Enter key.
What happens?
7
Typing calculations into the shell
• Try typing the following calculations into the
shell:
– 7 take away 3
– 4 multiplied by 3
– 10 divided by 3
– 2 add 4 then multiply the total by 5 (remember
how you can specify the order of calculation by
using brackets)
– 12345 take away 321 and then divide by 2
8
Creating a simple program
• If you want to create a
program that has more than
one line of code in it then
you will need to use a new
window to do this.
• Click on the File menu and
select New Window.
9
Type into the new window
• Type the following into the new window (it is
case sensitive):
11
Change your code
{0} is known as a
placeholder. This .format(Variable
tells Python that you Name) tells Python
want to put what you want to put
something in this into the placeholder
place.
12
Change your code
c a re f u l: it is
Be Save and
a se s e n s iti ve
c run your
p u t a n d p rint
(in code (F5)
N O T h a ve to
shou l d try it out
t a l le tt e rs )
capi
13
Keywords
Word What this does
This will show whatever you want in the Python
print Shell
This will ask the user a question in the Python
input Shell and expect a response
14
What will this code do?
• Try it out.
• You can use more than one placeholder as shown below:
15
Data types
• Try entering the following:
16
Data Types
• When the user inputs data it is automatically
classed as text, even if they only enter a
number.
• This is known as a string.
• In Python, if you want to treat the value as a
number you will need to specify the data type
when you create the variable.
17
Declaring a data type
What is different in this line of code?
18
Change your code
• Change the code you have just created so that
it treats both inputs as a number.
• Run the code again and you should see that it
now works.
19
Operators
Operator What it means Example
>>> 4+5
+ Addition >>> 9
>>> 5 – 1
- Subtract >>> 4
>>> 7 * 3
* Multiply >>> 21
>>> 7/4
/ Divide >>> 1.75
>>> 20 // 3
// Whole number division >>> 6
>>> 20 % 3
% Remainder after whole number division >>> 2
20
Python Exercises
Work through the
worksheet creating the
programs you are asked to.
The operator table at the
top of the page will help you
complete the programs.
21