python_Booklet
python_Booklet
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.
4
To test this we simple go to
Run, Run Module (or press F5).
5
Printing Text and Calculations
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
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.
7
Input
8
Concatonate Strings (connecting strings and variables)
Here we have connected a Text
String and a Variable String into
the same Print.
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.
10
IF, ELIF and ELSE
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.
Inden
t
11
After the IF we also have elif and
else for the rest of the calculation
options.
else:
12
Read and Append to a .txt File
This codes stores an input as
firstName
Close textfile.txt.
Close textfile.txt.
13