Professional Documents
Culture Documents
Computer, 11th Booklet
Computer, 11th Booklet
Python 1
The first thing we're going to do is print some output in the console using the print statement:
print("Hello, world!")
When typing this code you need to type accurately. Your code won't work if you leave out the ( ) or " ".
Printing numbers
In task 1 we printed text to the screen. In programming chunks of text are called strings and we wrap them
in quotes " ".
When we print numbers, we still need the brackets ( ) but we don't need the quotes " ".
The following code asks the user's age and stores it (using =) in a variable called age:
print(age)
We don't need " " around a variable name, and in Python we write variable names in lower_case.
Joining data in print statements
We can do more than just printing the variable name. For example, we might want to ask this:
But print out "You are 16" or "16 is a great age to be!"
There are lots of ways to do this, but let's start easy by 'adding' the parts together to get the sentences above:
The + does not add any spaces, so make sure you look closely at your output to check there are spaces in all
the right places!
In Python, strings are written inside single or double quotes: " " or ' '. Let's use " " for now.
print("Hello" * 5)
Strings with a " in them give us an error because Python thinks the second " is the end of the string:
Always choose quotes that let you avoid or reduce \ escaping within the string.
The code above works fine, but is often written like this to make it easier to read:
String variables
We can use what we have learned about strings to store data inside variables too:
name = 'Fred'
Right now, the value stored inside the variable called name is a string that says 'Fred'. So we can say that
name is a string variable.
Variables are called variables because the values they store can change during a program.
Math operators
Let's take a closer look at math in Python. Here are the main math operators:
+ Addition
- Subtraction
* Multiplication
/ Division
And we also have ** for powers or exponents. For example, 23 would be written as 2 ** 3
BEDMAS - Order of operations
When you have different operations (+ - / *) in one equation, they have to be done in a certain order. One
common rule is BEDMAS.
This means that anything inside Brackets ( ) is calculated first, followed by Exponents (or powers, **),
followed by Division / and Multiplication *, then Addition + and Subtraction -:
print(3 + 4 * 2)
In the above print statement the computer multiplies 4 * 2 first to get 8, and then adds 3. This gives 11.
print((3 + 4) * 2)
It's very important to check you have closed all of the brackets you have opened once you put brackets
inside a print statement!
Floating point numbers or floats have a decimal point in them like 3.2 or 0.1.
We identify what the datatype of a variable is by what is stored inside it; if it stores an integer, it's an
integer variable, if it stores a float, it's a float variable.
One important thing to know is that if you divide in a calculation, you'll always get a float back, so 8 / 4
will give you 2.0.
print("5 + 4 =", 5 + 4)
5 + 4 = 9
Remember we can use variables that store numbers anywhere that we would use numbers:
Python has a built-in function called .format() which gives us heaps more control over how our stuff is
printed on the screen.
Here is how .format() is used, and you can click here for an explanation:
#Calculate wages
hourly_rate = 15
hours_worked = 35