0% found this document useful (0 votes)
61 views

Tg-Lesson 1

Python is a programming language that can be used to write simple programs. This lesson teaches the basics of Python, including how to write code in the Python shell or in a new window. It covers creating variables, declaring data types, using operators like addition and multiplication, and printing output. Students are assigned a homework to practice these skills by creating some basic Python programs.

Uploaded by

Osman Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Tg-Lesson 1

Python is a programming language that can be used to write simple programs. This lesson teaches the basics of Python, including how to write code in the Python shell or in a new window. It covers creating variables, declaring data types, using operators like addition and multiplication, and printing output. Students are assigned a homework to practice these skills by creating some basic Python programs.

Uploaded by

Osman Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Python

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.

• All of you will:


– Create simple code including the input and print scripts.
• Most of you will:
– Create variables and declare the data types for variables.
• Some of you will:
– Use operators to effectively create programs to solve a
number of scenarios.

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?

• You can type in calculations into the Shell exactly as


you would normally.
• What symbols would you use for multiply and divide?

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):

• What do you think this code will do?


• Save the code as Hello.py (It is important to
add the .py at the end as your code will not
work without it).
• To run the code in the Shell hit the F5 key on
your keyboard.
10
Change your code
Change your code to the following:
Variable Name tells Input is used to tell
Python that you want it Python that you want
to remember the the user to enter a
answer you type in and response
call that “Firstname”

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

variable These are pieces of data that Python needs to


remember for later use

placeholder This tells Python that you want to insert


something (e.g. the data stored in the variable)

14
What will this code do?

• Try it out.
• You can use more than one placeholder as shown below:

• Placeholders are numbered {0}, {1}, {2} … etc in the


order that the variable names appear in the format
brackets.

15
Data types
• Try entering the following:

• When you run the script it will not work as you


may expect. Try to work out what it is actually
doing.

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?

int( tells Python that ) closes the


the input is going to “int” bracket
be treated as an
integer (number).

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.

Make sure you save them


with sensible names.

21

You might also like