an introduction to
Contents
Creating Python Programs – IDLE ................................................................................................................................ 3
Printing Text and Calculations ..................................................................................................................................... 6
Variables ..................................................................................................................................................................... 7
Input ........................................................................................................................................................................... 8
Concatonate Strings (connecting strings and variables) ............................................................................................... 9
Strings & Integers...................................................................................................................................................... 10
IF, ELIF and ELSE........................................................................................................................................................ 11
1
2
Creating Python Programs – IDLE
IDLE is a program that comes with Python. This allows us to create our own Python programs using just code.
3
When the “Python Shell” is
then opened in IDLE, simple go
to File and New Window to
begin a new Python project.
This is a blank Python program.
You must save this to run the
program.
Here I have used the Print
code, which prints text onto
the screen.
4
To test this we simple go to
Run, Run Module (or press F5).
You have just programmed and
opened your first program in
Python.
5
Printing Text and Calculations
As you can see, using Print with
text and numbers is different.
We call text “Strings”, or
“Strings of Text”. Ensure you
always surround these with
speech marks.
Basic, none decimal numbers
are called “Integers”. These
can be more difficult to
identify, as they’re not a
different colour.
Python read code from top to bottom. Each line represents a new command, or bit of the code.
As you can see above, we can dissect the code and identify which bit of code does what when the when we
run the program.
6
Variables
Here we have not directly
printed anything. We have
stored each section of data into
what are known as Variables.
Variables as words which
represent small bits of data we
can store in the computer’s
memory, and then call upon, or
‘get’ whenever we like.
Above you can see that each variable is printed (hellowVariable, introVariable and firstCalculation). Python finds
the data we stored with, or under, these Variables and prints that.
In this case it is exactly the same program.
7
Input
This allows the user to enter
something and the program to
store what they have entered
to be used at a later point.
Here we are storing an input as a variable (name).
We then print the variable.
8
Concatonate Strings (connecting strings and variables)
Here we have connected a Text
String and a Variable String into
the same Print.
As you can see, a comer
separates the Text String (Hello )
and Variable (name).
When switching from one type
to another within on Print
always put a comer in the
middle.
Here I have asked two questions
and then in one, single Print,
connected (concatenated) the
String Text and Variables.
Comer Comer Comer String
String Comer
String
Variable Variable
Every time a Variable is put inside a String it needs to be separated
with comers before and after.
9
Strings & Integers
Strings are a data type that can consist of Text, Symbols and
Numbers. As Python views these as nothing more than a series
of characters, it will not be able to do any mathematical
calculations with them.
Integers are whole numbers. As Python sees these as
numerical values, we can do mathematical calculations with
them.
All inputs are Strings, no matter if the
user only enters numbers.
Therefore, when wanting to do
calculations, we have to convert the
inputs to Integers.
We do this by surrounding the Input
with int( )
This converts whatever number the user enters to an
Integer, which can be calculated.
Integers can be multiplied.
10
IF, ELIF and ELSE
The aim of this is to let the user
enter two different Integers (whole
numbers) and then select which
calculations they would like to do
with these numbers.
We have three inputs, two Integers
and one String Text.
The IF statement at the bottom checks whether the “calc” Variable (that the user inputs) is equal to “+”.
If it is an “answer” variable then adds both the “num1” and “num2” Variables (both inputted by the user)
together.
These variables are then printed onto the screen so give a calculation.
NOTE: Indentations are very important in Python. It shows where the If statement starts and ends.
IF the CALC inputted Variable is EQUAL to “+”
Store NUM1 + NUM2 within ANSWER variable
Inden
t
Display NUM1 + NUM2 = ANSWER
Inden
t
11
After the IF we also have elif and
else for the rest of the calculation
options.
elif calc == “*”:
elif calc == “/”:
elif calc == “-”:
else:
ELSE IF CALC EQUALS * Multiply the inputted numbers
ELSE IF CALC EQUALS / Divide the inputted numbers
ELSE IF CALC EQUALS - Subtract the inputted numbers
ELSE CALC EQUALS anything else Print “You must enter *, /, + or –“
12
Read and Append to a .txt File
This codes stores an input as
firstName
It then opens the textfile.txt and
writes this firstName test string to
the end of the text file.
It then opens the text file again,
reads it and prints it into Python.
Input name stored as firstName
Variable
Open text file to Append (“a”) and
print the variable firstName.
Close textfile.txt.
Open textfile.txt file to Read (“r”)
and print each like to the Python
Close textfile.txt.
13