- Basic Operations
- Data-Type conversion
- Building A Simple Calculator
This is a tutorial aimed to provide basic knowledge about the Python programming language. It also assumes you have some knowledge of computer science.
- Python 3 interpreter
- Basic knowledge of computer science
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
You are probably wondering what
"The price is £14.00".
The “print” statement is a function, we will cover this in a later tutorial.
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:
some_var = "Hello World!"
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:
||This is a string|
||This is a integer|
||This is a float|
||This is a list|
||This is a boolean|
Now let’s see how we can output some defined variables in Python.
name = "Leo" age = 18 print("Hello", name, "you are", age, "years old")
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.
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.
num_a = 2 num_b = 4 num_c = num_a + num_b print("The answer is:", num_c)
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.
num_a = 2 num_b = 4 num_subtr = num_a - num_b num_multi = num_a * num_b num_divid = num_a / num_b print(num_subtr) print(num_multi) print(num_divid)
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.
num_a = "1" num_b = "2" num_c = num_a + num_b print(num_c)
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.
Next I will tell you how to get user input.
name = input("What is your name? ") print("Hello", name)
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:
What is your name? Leo Hello Leo
After getting input from the user it is a string, but what happens if we want a number. Well we can convert it.
age = input("What is your age? ") age = int(age) age = age + 1 print("Next year you will be", age)
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:
What is your age? 18 Next year you will be 19
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.
num_a = input("Enter Number A? ") num_b = input("Enter Number B? ") num_a = float(num_a) num_b = float(num_b) answer = num_a + num_b print("The sum of", num_a, "+", num_b, "is", answer)
Let’s test our calculator with the numbers 14 and 20, this should give the answer of 34.
Enter Number A? 14 Enter Number B? 20 The sum of 14 + 20 is 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.