Variables, Strings & Arithmetic

Variables

The first part of Python we are going to talk about is variables. What are they and what are they used for, in simple terms a variable is a box where you can store any information you like and then return to that box to retrieve the item.

In this example the name of the variable is name and the name Maria in stored in that box to then retrieve later. In Python, we do that by doing the following:

name = "Maria"

As you can see its easy enough to do. A string is surrounded by double quotes ” ” and numbers or known as integers are just created without doubles quotes. See example below:

current_count = 14
planned = 5

Once the variable has been created, you can then delete the variable by using the keyword del  this will look for the variable in memory and remove it. If you run the following code you will see at the end you wont be able to access the variable:

# Variable my_variable is created with the value "Daniel"
my_variable = "Daniel"

# The print command is used to print the values to the screen
# At this point the print(my_variable) will print Daniel
print(my_variable)

# Will delete the variable
del my_variable

# Then if you run the print() command again the variable no longer exists and you will get the follwoing error
print(my_variable)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'my_variable' is not defined

Arithmetic

In Python 3 there are 3 data types, these include:

  • Int – This is whole numbers such as 1 2 3 or 45
  • Float – Numbers with a decimal point, such as 3.4 4.5 14.5 ect…
  • Complex – (Back back to this)

The first slight difference you will find in Python 2 compared to Python 3 is that the results for the basic operation of division. When you divide whole numbers on Python 2 it will return an int. However, in Python 3 it will always return a float. Here is an example:

# Python 3 Divison Output
>> 8/2
>> 4.0
# Python 2 Division Output 
>> 8/2 
>> 4

If you were to divide by 0 on Python 3 you will receive a “ZeroDivisionError: integer division or modulo by zero” so dividing by 0 is not possible in Python 3. Going back to using basic data types in Python, there is a function round() this is a very useful function which will round a float to the nearest whole number. Here is an example of how this works:

# Python round() function
round_me = 5.4
round(round_me)
>> 5.0

There is a function which is also useful and that’s the int()  this will return an int value, here is an example of how the int() function works:

# int() - returns an int from a float
>> int(5.4)
>> 5

Then we have a float() function, this allows us to do the opposite of int(). With the float() function it allows us to convert an int such as 1 or 2 to be converted to 1.0 or 2.0. Here is an example of how this works:

# float() - Converts int's to floats
>> float(4)
>> 4.0

Along with all these functions Python, of course, understands basic arithmetic operations such as adding 2 numbers together, multiplying, subtraction or division as you have seen previously. Here are some examples of addition, multiplying and division.

# Addition
>> 5+4
>> 9
# Mutlpying 
>> 5*4 
>> 20
# Division 
>> 5/4 
>> 1

You will find the common and basic arithmetic operators are as follows:

  • 4+5  addition
  • 54  Subtraction
  • 5/4 Division
  • 5*4 Multiplication

As you probably have learnt at school the famous BODMAS, which relates to the order of precedence in arithmetic operations in algebra etc… well Python understands the order of precedence but you have to explicitly have to change the order and you can do this by using the parentheses () Here is an example of how you can do this:

# Without order of precedence
>> 5*6+5
>> 35
# With Order of Precedence 
>> 5*(6+5) 
>> 31

As you can see the results differ and this is because I explicitly changed the order I want the calculations to be performed

Strings

Strings is a group of characters between double or single quotes, like so:

# String example assigned to a variable
world_message = "Hello World"

However, the one thing that needs to be taken into consideration is when creating strings, is the when it comes to having single quotes for names for the word such as He’s right 

# This will generate an error, 
variable = "He's right"

# The way to avoid this by placing a backslash before the single quote
variable = "He\'s right"

# Another way to ignore the single quote
variable = """He's right"""

As we have seen earlier, we can also the + operator to add 2 or more numbers together, however, we can also use the + to join strings together this is called concatenation, as an example:

# Join string together with +
>> "test" + "123"
>> test123

You will also notice that there is no space between test and 123 this is because you need to leave a space either at the end of the first string or at the begining of the second string, like so:

# Concatenating
>> "test " + "123"
>> test 123
# Or 
>> "test" + " 123" 
>> test 123

You can do this with the basic operators:

# Multiplying a string
>> "="*20
>> ====================

However, say you want to add multiple variables in a string based on what the user input? You will want to enter that into the string for example, The user enters their first and last name in your program and you want to output a message to the user saying something like “Welcome Daniel your last name is Maia and this is not a common name” You can do this using the format() function. You use this function as such as:

# format() function
fname = "Daniel"
lname = "Maia"
print("Welcome {} your last name is {} and this is not a common name".format(fname, lname))

>> Welcome Daniel your last name is Maia and this is not a common name

You will notice that I used a function called print()  this will allow you to print to the screen.

If there is anything you did not understand, drop a comment below or drop me an email and I will be happy to answer your questions 🙂

Leave a Comment