High Level Programming Language
High Level Programming Language
Hinthar Education
Python 2023
About Python
Python is a programming language created by Guido van
Rossum in 1991. It is widely used in education to teach
programming, and in
business and industry to create programs.
YouTube, Google, Instagram and Spotify all use Python.
The language was created so that the code could be read and
understood easily.
Page 1
Python
Using Python
Python is normally installed on a computer with an Integrated Development
Environment (IDLE). The IDLE provides lots of functions that are useful when writing
Python code. Python can operate in two modes: the first is interactive mode. When using
Python code you need to ensure that you type commands using lower case letters. Python
also provides help at the command prompt. You can type help() and find out how to use
commands in a Python program. To close Python type exit(). When a command is typed
it must be ‘translated’ so that the computer can understand it. The IDLE has an
interpreter which does this. Each command is changed into a form of machine code so
that the computer can carry out or execute the command. Typing commands directly
into the Python shell is called programming in interactive mode. One disadvantage of
using interactive mode is that the code cannot be saved when using Python in this way.
Page 2
Python 2023
Practice
Open the Python IDLE.
Enter this command at the interactive prompt (>>>), and press ENTER: print (“Welcome to the Python Shell”)
Now enter a command to print your name onto the screen and press ENTER: print (“add your name”)
Now try this command: print (“Hello World) An error should occur because you have left out a set of quotation
marks. Correct the error and execute the command again.
Try entering this calculation: (7 + 3 * 2 Look at what has happened this time.
Correct the error and execute the command again. The answer should be 20.
Get Python to add 150 and 23 together, and then multiply the result by 3.
Page 3
Python
HTML5
Java Script
Page 4
Python
Using the Python IDLE in Script mode
Save a program
Create a program
From the text editor menu select ‘File’.
From the Python Shell select ‘File’.
Then select ‘Save as’.
Then select ‘New File’. Correct errors
A new window will open and you can
If there are errors in the code, they will be
enter lines of code into this text editor.
highlighted in red after you Run or execute
the program:
Add comments
Adding comments to code allows you to explain Run a program
what the code does or to provide extra From the text editor menu select ‘Run’.
information about the code. In Python you can do Then select ‘Run module’ (or Press F5). This will cause
this by starting the comment with a hash (#) the Python interpreter to execute the program.
symbol. Any text on a line after a # symbol will be The output from the program will appear in the window
ignored by the interpreter of the Shell.
Page 5
Python 2023
Page 6
Python 2023
To store the player’s name you will need a variable which can
hold letters – this is called a string.
Page 7
Python 2023
Page 8
Python 2023
Page 9
Python 2023
Python uses the int() function to change the string value from the input
statement into an integer
the following statement to convert the input string to an integer
Page 10
Python 2023
Page 11
2023
Page 12
2023
Adventure choices
Selection
Sometimes decisions have to be made in a program. This is usually because the
program has reached a point where there is more than one option or choice.
Then there are a number of possible pathways for the program to follow
depending on how the player answers certain questions. Selection occurs when
a question is asked.
It is important in programming because the programmer can offer the
user choices about the way in which they move through the program.
Selection is achieved by using IF statements.
Page 13
2023
Adventure choices
The structure of an IF statement
Page 14
Adventure choices
The indentation is very important because this is how Python decides to group statements together. The
statements with the same indentation belong to a group and are called a suite. Python will indent the
statements automatically after you type the ‘:’ symbol. If indentation is not correct you will get an error
because Python needs to know which statements to group together.
Page 15
Combining conditions
Boolean operators.
AND and OR are called Boolean operators. AND needs all of the conditions to be true to execute the statement
that follows OR needs only one of the conditions to be true to execute the statement that follows.
Page 16
Practice
Look at these IF statements relating to the playerage:
Using the IF statements above, copy and complete the table by writing which print statement will be
executed for each of the player ages entered. The first one has been completed for you.
Discuss with a friend, how this IF statement will change the age rules for playing the game.
Create a new IF statement which will ensure that players aged 7 to 11 can play the game. Use the following
partially completed IF statement to help you.
Page 17
Combining conditions
The player has now entered data for: playername, playerage, gamesplayed, ownername and robotname.Now they
must decide whether or not to enter the Digital Sweet Shop. The program should ask the user if they want to
enter the shop. The user will have to answer this question. If they enter ‘Y’ that means ‘Yes’. If they enter ‘N’ that
means ‘No’. We must store the user’s response in a variable. Let’s call the variable entershop. An IF statement is
used to check the value of entershop. Look at the code below and discuss what happens if the player enters ‘Y’ (for
yes) or ‘N’ (for no).
Page 18
Combining conditions
Note the condition in this case:
When checking the value of a variable in a condition, you should use the == symbol.
When you want to assign a value to a variable, you should use the = symbol.
Page 19
2023
Practice
Copy and complete the following table by inserting either = or ==.
Page 20
syntax error
Look at the code shown below.
A syntax error has occurred. Python has rules for spelling and grammar. These are called
syntax rules. A syntax error occurs when a statement is written incorrectly. For example,
a misspell word or variable name or missing brackets. A program cannot run if there are
syntax errors, they must be corrected first.
Page 21
Logic error
Programs can also contain logic errors. These occur when there is an error in the logic of
the program. For example, parts of the program may be missing, in the wrong order or
could be using the wrong data. The program will run but may not produce the correct
output.The section of code below contains a logic error
Page 22
Adventures with lots of choices
In one of adventure game ,What if the player had more than two directions to take at the top of the stairs? Python
uses the ELIF statement to allow you to check multiple conditions for TRUE. This means that if a condition
evaluates to true, the block of code following that particular ELIF statement will be executed.
Adventures with lots of choices
ELIF makes writing code easier when we have to think about a lot of options.
Like the ELSE statement, the ELIF statement is optional. That is, you do not need to have one in an IF
statement.
However, whereas the ELSE statement can only be used once in an IF statement, the ELIF statement can be
used as many times as needed.
The structure of a statement containing ELIF is shown below
Adventures with lots of choices