Intro
This is a tutorial aimed to provide basic knowledge about the Python programming language. It also assumes you have some knowledge of computer science.
Requirements
- Python 3 interpreter
- Basic knowledge of computer science
Output
Let’s start with something simple. The first program we are going to write is a simple “hello world” application. This is most likely the most common task every programmer does when starting a new language. What we want to happen is our program will output “Hello World!” onto the terminal. We can achieve this with the following code:
|
|
When run we should get the output Hello World
.
You are probably wondering what print
means this is telling Python to output variables or strings that is put inside the brackets. The " " around our sentence means that it is a string, a string is a data-type that allows the storage of characters this can be anything from numbers, letters or characters. The following for example is a valid string "The price is £14.00"
.
The “print” statement is a function, we will cover this in a later tutorial.
Variables
Let’s move on to learn how to use variables, A variable is a named value of a specific data-type for example string or integer.
We can define a variable as shown below:
|
|
The first part is telling Python that we want a variable called “some_var”, then we are setting it equal to a string value. Notice that I used a underscore to separate the words; this is because variable names cannot have spaces, they also can’t start with a number or contain any symbols.
There are also many other data-types that we can assign to a variable, here is a table of the main ones:
Code | Data-type |
---|---|
my_var = "hello" |
This is a string |
my_var = 1 |
This is a integer |
my_var = 5.5 |
This is a float |
my_var = ["hello", "world"] |
This is a list |
my_var = True |
This is a boolean |
Now let’s see how we can output some defined variables in Python.
|
|
Notice how we can also output data from variables in a print statement. The comma is separating each value we “pass” in. We also don’t need to leave gaps in between the words, this is because Python will automatically add spaces in between the arguments. Also notice how Python will automatically try to convert any values into a string when run.
An argument is a value that is passed into a function
When run the program should output: Hello Leo you are 18 years old
.
Basic Operations
Now I will be showing you how you can do basic operations with strings and numbers.
First we will use integers, just to make it as simple as possible.
|
|
When run you should see:
The answer is: 6
Here we are first defining two variables that are equal to 2 and 4. Then we are creating a new variable that will equal num_a and num_b added together. We are then outputting the result. We have now done a mathematical calculation.
You can also use other operations on numbers, let’s try out subtracting, multiplying and dividing.
|
|
When this is run you should see
-2
8
0.5
See for our division Python has changed the type from a integer to a float, as it did not produce a whole number.
Now let’s try adding two strings together.
|
|
When run you will get 12
. That’s strange it didn’t add correctly; well yes it actually did it correctly. In Python strings are concatenated. To get a integer we would have to first convert the strings into integers or floats. I will show you that in the next section.
Concatenation is what happens when strings are joined together.
Input
Next I will tell you how to get user input.
|
|
See that new keyword “input”, this is how we will gather a input from the user. Also notice that we can also output something before the user input is received.
When run after the user entered their name and presses the enter key it will get assigned to the “name” variable, then we wil print it.
The following is what I got when I entered my name in:
|
|
Data-Type conversion
After getting input from the user it is a string, but what happens if we want a number. Well we can convert it.
|
|
Notice the new keyword “int()”, using this we can convert a string into a integer type. There are also other converters we can use like “float()” or str().
The following is what I got when I entered my age:
|
|
Building A Simple Calculator
Now you are ready to build a addition calculator.
Before looking at the code below have a go at writing your own. Here is what you will need:
- Get two numbers from the user as separate variables
- convert both to float or int data-types
- add them together and store it in another variable
- output the answer
Ok so below is my code, don’t worry if it looks different, as long as it still adds correctly.
|
|
Let’s test our calculator with the numbers 14 and 20, this should give the answer of 34.
|
|
If yours won’t run take a look at my code and check what you have done wrong. If yours does run and calculates the numbers, well done.
Now you should probably find out about if statements next; to extend your calculator to do more mathematical operations like subtraction. You can find that tutorial here.