Python 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Computing: Introduction to Python

Lesson one
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.

Success Criteria
• To know how to start Python IDLE and run a Python program.
• To use Python to perform calculations.
What Is Python?
Python is a programming language.
It was invented in the 1980s.
It was named after comedy group ‘Monty Python’.

Python is used by:


• Yahoo (maps); Python is
• Google (search engine); easy to learn.
• Shopzilla (shopping comparison site);
• Battlefield 2, Civilisation 4 (computer games);
• Industrial Light and Magic
(in films such as ‘The Phantom Menace’ and ‘The Mummy Returns’);
• NASA;
• Nokia;
• Facebook, and many more.

Python is used in many universities to teach programming.


Starting Python
We can use Python IDLE to create and run our Python programs.
IDLE is an acronym of Integrated Development Environment.
An Integrated Development Environment contains all the tools needed to
write, test and run computer programs.

• The window that first


appears when you
start Python IDLE is
called the Interactive
mode window.
Starting Python
• From the Interactive mode window, we can click on the File menu item,
and then select New File.
• This opens a new Script mode window.
Starting Python
• Good practice is to have instances of both Interactive and Script mode
windows open side-by-side, something like this:

Interactive On the right is Script


the Script mode
window.

On the left is the


Interactive mode
window.
Starting Python
• The Interactive mode window is easy to identify.
• You will always see >>> at the start of each new line.
Starting Python
Key Term
An Integrated Development Environment contains all the tools
needed to write, test and run computer programs. In Python
we use the acronym IDLE. Other programming languages
sometimes use the shorter acronym IDE for Integrated
Development Environment.

Python IDLE has two modes of running.


The Interactive mode gives immediate feedback to every line of code that
is entered, and is useful for testing ideas and small snippets of code.
The Script mode allows a programmer to type in many lines of Python
code, and then save the code as a single program with a file name. This
file can be saved and run at any time. However, any inputs or outputs of
Python programs will always be shown in the Interactive mode.
Hello World
• Let’s stick with the Interactive mode window for now,
and learn our first Python command: print
• print does exactly what it says: it prints information onto the screen.
• It does not print anything onto a printer.
• There is a long established tradition in computer programming.
The first program that most programmers write is often a
“Hello World” program.
• In the Python Interactive window, type: print(“Hello World”)
and then press the Enter key on your keyboard.
Getting
results
Hello World
• Some important things to notice:
Always remember to use the (brackets) with print.
• We also used “quotes” because we were printing text.

Did you notice the different colours?


• print is purple because IDLE recognises print as a Python command.
• “Hello World” is green because it is a string of text (more on this later).
• The printed output is usually blue.

Try the print command again, but this


time type your own sentences.
For example: print(“Goodbye Monty”)
Cool Calculations
• We can also use the Python print command to help us do calculations.
• The important thing to remember here is that we are dealing with
numbers and not text, therefore we do not use “quotes” as before.
• Try the following print instructions now and see what happens:

Can you make up your own calculations?


Cool Calculations
Let’s look at this example once again:

• Computers often use the * symbol (asterisk) for multiplication.


• Why does Python IDLE give the answer 27? If we add 6 and 3, this
gives us 9. Then multiplying 9 by 7 gives 63, not 27!
• The reason is that Python uses BIDMAS.
Do you remember what BIDMAS means?
• Python knows to multiply the 3 and 7 first, and then add 6 afterwards.
So, 3 * 7 = 21, then we add 6 to 21 giving us 27.
Cool Calculations
Try these last examples, and see if you can figure out what
Python is doing with the numbers:
Cool Calculations
The double-slash // is rounding-down
(or “floored”) division. It divides two
values and rounds down the answer.

The double-asterisk ** here means 5


to the power of 2.

These last ones are modulo-division.


% gives the remainder of a division
between two values.
E.g. 15 / 2 = 7
remainder 1.
Let’s Bring It All Together
Key Term
An Integrated Development Environment contains all the tools
needed to write, test and run computer programs. In Python
we use the acronym IDLE. Other programming languages
sometimes use the shorter acronym IDE for Integrated
Development Environment.

Python IDLE has two modes of running.


The Interactive mode gives immediate feedback to every line of code that
is entered, and is useful for testing ideas and small snippets of code.
The Script mode allows a programmer to type in many lines of Python
code, and then save the code as a single program with a file name. This
file can be saved and run at any time. However, any inputs or outputs of
Python programs will always be shown in the Interactive mode.
Rate Your Progress
Red Light: you have understood some of the objective
and you will think about it some more later on, perhaps
asking a friend or teacher for help.

Amber Light: you have understood most of the objective


and you are happy with your progress.

Green Light: you feel fully confident with this objective and
you understand it well.

Success Criteria:
•To know how to start Python IDLE and run a Python program.
•To use Python to perform calculations.
Nailing It Down
We have learned a lot today about Python IDLE, print and input.
Python is a great programming language to learn.
You can download it yourself at home for free by visiting:
https://www.python.org/downloads/
(or just search for ‘python’ using your favourite search engine).
There are also lots of free online versions of Python which are great for
learning how to code. Try out some of these links now:
• https://snakify.org/
• http://pythontutor.com/
• https://hourofpython.com/
• https://repl.it/languages/python3

You might also like