Enchanted Code

Python 3 for beginners

6 minutes read

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:

1
print("Hello World!")

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:

1
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:

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.

1
2
3
4
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.

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.

1
2
3
4
5
6
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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.

1
2
3
4
5
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.

Input

Next I will tell you how to get user input.

1
2
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:

1
2
What is your name? Leo
Hello Leo

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.

1
2
3
4
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:

1
2
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:

  1. Get two numbers from the user as separate variables
  2. convert both to float or int data-types
  3. add them together and store it in another variable
  4. output the answer

Ok so below is my code, don’t worry if it looks different, as long as it still adds correctly.

1
2
3
4
5
6
7
8
9
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.

1
2
3
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.

See Also

Buy Me A Coffee