In the last chapter we introduced the idea of "strings". They are a way of representing normal human words inside a Python script. Strings start and end with double quotes ("
) e.g.,
"Hello from Python!"
is a string with three words and an exclamation mark as content.
Strings can contain numbers as well:
"The Battle of Hastings was in 1066"
and they can even be empty:
""
It is possible in Python to also use single quotes ('
) to make strings, as long as the string starts and ends with the same type of quote, but it is convention to prefer double quotes.
The other most common type of data that you'll find in Python scripts are numbers. There are two main types of number in Python:
When creating numbers in Python, you do not use quotes, you write the number directly. So:
3.14159
is a float and
42
is an integer.
It's important that when writing numbers in your scripts, you do not put quotation marks around them. There is a difference between 42
and "42"
. The first is the integer $42$ (i.e. the number one greater that $41$) and the second is just a pair of digits and has no more meaning to Python than "fourty two"
or "penguin"
do.
Of course, it not much use to have numbers and strings floating about with no connection to each other. We want to be able to give them names and combine them together. We assign names to data using the =
sign. For example if we want to make some data and give it a name we can do it like:
pi = 3.14159
This has created a number 3.14159
and given it a name, pi
. We can now use this name in other parts of the program to refer to that piece of data:
print(pi)
Names in Python can contain upper and lower case letters, numbers and underscores (but can't start with a number). Chosing the correct name for a particular variable is an important task as a non-descriptive name (or worse, an incorrect name) will be very confusing for you and anyone reading your code. It is common in Python to name your variables with all lower case letters and use underscores to separate words.
So, for a variable which contains a number representing a distance in miles, avoid shortened names like dm
, distm
or d
and instead use a name like distance_in_miles
. Remember, code will be written once but read many times so make it easy to read.
These two can be combined into a full Python script:
pi = 3.14159
print(pi)
and run with:
python variables.py
Variables are more that just a way of labelling data, they also make it easier to do things with your data. If you have some numbers you can add, subtract, multiply and divide them as you would expect. The symbol for multiplication is *
and the symbol for division is /
.
distance_in_miles = 30
distance_in_km = distance_in_miles * 1.60934
print(distance_in_km)
Here we created a variable distance_in_miles
with the value of 30
. Then we used that variable in line two and multiplied it by a number (distance_in_miles * 1.60934
) and assigned the result of that calculation to a new variable called distance_in_km
. Finally, we printed out the new variable.
Likewise we can do addition:
temperature_in_celcius = 25.1
temperature_in_kelvin = temperature_in_celcius + 273.15
print(temperature_in_kelvin)
So far we've been giving the print
function a single argument to print a single thing but we can print many things at once if we give it multiple arguments. Arguments to functions in Python are separated by commas. The print
function is designed so that it will print each of the arguments it was provided with, one after another on the same line, separated by spaces.
fav = "red"
print("My favourite colour is", fav)
python colour.py
Edit script.py
so that the two parts of the phrase are passed to print
as separate arguments. Make sure you save the file and rerun it in the terminal.
If you get strange output like ('Hello', 'Python')
rather than Hello Python
, then try running your script with the python3
command as described in this video.
So far, all the code we've run is somewhat static. Every time we run the script, the output will always be the same. What if your favourite colour is not red? The power of programming is that it is dynamic and we can write code which responds and reacts. We'll see a lot more of this throughout this workshop, but for now we'll introduce one more function that Python provides, input
.
The print
function is how we get information out of our program, and the input
function is how we get data into it.
The input
function will pause the program and wait for you to type something in. Whatever you type, followed by enter will be assigned to whatever variable name you put on the left hand side of the =
.
print("What is your favourite colour?")
fav = input()
print("My favourite colour is", fav)
Now, if we run this script, it will print the first message, and then wait for you to type something. If you type "red" and then press enter, it will assign "red" to the variable fav
and then use that variable in the final print
function:
python colour.py
Likewise, you can run the script one more time and give a different answer and get a different output:
python colour.py
One small improvement we can make is that instead of printing our message "What is your favourite colour?", prompting the user of the script to type something, we can do this directly with the input
function by giving the prompt as an argument:
fav = input("What is your favourite colour? ")
print("My favourite colour is", fav)
Note that there is a space after the question mark so that when you're typing in your answer it's not butting directly against the character:
python colour.py
Edit script.py
so the value of the variable name
is set using the input
function. Be sure to set a prompt as well so that the user know that they should type something.
The script should, when run with python script.py
, then print out:
What is your name?
and wait for you to type your name like:
What is your name? Matt
After pressing enter, it should then print out:
What is your name? Matt
Hello Matt