Python 2

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

Computing: Introduction to Python

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

Success Criteria
• To write simple programs that respond to user input.
Incredible Inputs
So far we’ve learned:
•How to start Python IDLE.
•How to open and run a Python program.
•How to use the print command.
Phew –
•How to do calculations in Python.
well done!

Computer programs become much more interesting and useful when they
can respond to user input.

Let’s find out how to do this in Python.


Type this code in the Interactive window, and then press the Enter key:
Incredible Inputs
You should have seen something like this:

That’s my name – did


you type in your name?

• input is our next Python command. It waits for the user to type in
something, in this case we are asking the user to type in their name.
• What happens to the name that is typed in?
• Answer: not much at the moment!
• There is something that happens though, we just can’t see it yet…
Incredible Inputs
To understand what’s going on, let’s look back at the Python
code we entered:

• name is a variable. We can think of variables


as like boxes in computer memory.
Each box has a label, and we can store
information in each box.
• In our Python code above, we have created
a new variable with the label: name

• name = input(“What is your name?”)


prints the question “What is your name” on screen,
then waits for the user to type something in.
• Whatever text they type in is then stored in the box
labelled name. Let’s see how this works…
Incredible Inputs
This is what happens when we type in our Python code:

>>> name = input(“What is your name?”)


What is your name? Monty
>>>
“Monty

Incredible Inputs
One last thing about using print and input…
We can do so much more than just print out someone’s name.

Try out the following code in Python and see what happens:

• Take care with your typing, watch our for the commas and quotes!

You should be able to see something like this:


Incredible Inputs
>>> print(“Hello”, name, “, how are you today?”)
Hello Monty, how are you today?
>>>

“Monty

nam
e

Well I’m fine and dandy,


thanks for asking!
Incredible Inputs
Let’s put all of our new knowledge and skills into action.
This time however we will be using the Script mode window:

Interactive Script

On the left is the


On the right is
Interactive mode
the Script mode
window.
window.
Incredible Inputs
Type the following Python code into the Script mode window:
Incredible Inputs
We’ll need to save our program as follows.

•Click on File

•and then Save As …

•Give your file the name: Greetings.py


Incredible Inputs
To see your program in action, either click the F5 function key,
or click on the Run menu item and then Run Module.

Type in your answers and


see what happens:
Incredible Inputs
This is what happens when we run our Python code:

“Monty” “Monty”
What is your name? Monty
How are you today?“notnotbad”
bad at all
What is your favourite food? “chips”
fish and chips
nam
Greetings Monty e
I hear that you are feeling not bad at all
Maybe you need tasty fish and chips to eat?
“not bad”

feelin
g

“chips”

name, feeling and foo


food are variables d
Run
time Se
gic ma
Lo nti
c

C o on
t ax

mp
n

ila
Sy

ti
Error Types
Error type Definition How to avoid !!!
⮚ mistakes in the code structure, ⮚ it is important to familiarize yourself with
such as using incorrect syntax or the syntax of the programming
forgetting to close a bracket. language you are using.
⮚ You can also get a good grip on the
⮚ Are usually caught by the compiler error messages that the compiler or
Syntax or interpreter, so they are easy to interpreter provides.
fix. ⮚ A good integrated development
environment (IDE) would always
provide syntax highlighting and code
completion, features that can help you
spot syntax errors as you type.
⮚ mistakes in the logic of the code, ⮚ To avoid logic errors, make sure to
which can result in the program break your code down into small,
behaving unexpectedly or failing to manageable chunks, and test each
produce the desired output. piece separately before moving on to
Logic
⮚ Are often more difficult to spot the next.
compared to syntax errors as they ⮚ You can also make use of debugging
don’t interfere with the running of tools to fix logic errors
the code.
Error type Definition How to avoid !!!
⮚ Mistakes that occur while the ⮚ test your code thoroughly and
program is running. For example, use error-handling techniques
attempting to access an index of such as try-catch blocks to
an array that is out of bounds or handle unexpected conditions
Run time dividing by zero.
⮚ Are specifically difficult to debug,
as they may not be immediately
apparent in the written code.
Semantic ⮚ mistakes in the meaning of the ⮚ One way to avoid semantic
code. For example, you can use errors is to use clear, descriptive
the wrong variable name or call a variable and function names. It is
function with the wrong arguments. also a good idea to use a linter,
which is a tool that checks your
⮚ These errors are difficult to spot as code for semantic errors and
they do not hamper the code from other style issues.
running.
Error type Definition How to avoid !!!

Compilation ⮚ Compilation errors occur when the The best way to avoid a compilation
computer is not able to make the error is to run the compiler often to
conversion reliably. This prevents get early feedback. Once you’ve
the software from being launched made many changes and see lots of
or tested. compiler warnings or errors, it can
get very daunting. By running your
compiler often, you would be able to
spot the errors sooner
Let’s Bring It All Together
Key Term
We can think of variables as like boxes. Each box has a label,
and we can store data (information) in each box. This data
can be either text or numbers, and can vary during the running of a
program. When the program ends, the boxes are emptied of data.

Pause for Thought


Python is a ‘high level language’. This means that it is closer to human
language than it is to ‘machine code’, the language that all computer
processors understand. (Machine code is made up of binary instructions,
lots of ones and zeroes).
In order to convert our Python programs into machine code we use a
translator, which is simply another computer program. In other words, we
need a computer program to translate our computer programs! Thinking of
the chicken and the egg idea, how then did programmers make the first
translators? See if you can find out…
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.
•To write simple programs that respond to user input.
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
Practical
Write codes to Ask the user the flowing questions :
- What is your name ?
- How are you today ?
- What do you do for living ?
- What is you favorite movie name?
- What is your birthdate ?
Codes example :

You might also like