Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.2Variables in Python

Variables

A variable is a placeholder that can contain a value. It's value can be modified at any time during the running of the application and it can be used to apply to another variable or passed to a method.

Python, unlike many languages out there, does not instantiate variables with a type. When a variable is created in Python it is often also initialised. This means it is given an initial value. Python is also a loosely typed language. This means that a variable can be assigned multiple values with different types each time.

Variables are declared using the following:

Python
variable = "value"
    

This article refers to a variable with the name v.

Numeric types

There are multiple numeric types out there, but Python is limited to the Java equivalent int and float.

To declare an integer type, the number is just simply typed.

Python
v = 403
    

Similarly, a float can be defined as follows:

Python
v = 6.34
    

Integers can be cast(converted) to floats very easily as shown below:

Python
v = (float)403
    

Strings

Strings in Python work similarly to most other languages. Strings are defined between quotation marks("") or apostrophes('').

Python
v1 = "Hello world!"
v2 = "403"
v3 = 'Hello world!'
    

Placing quotation marks and apostrophes in strings

This will be covered in more depth in the article in this tutorial on strings.

Telephone numbers should be stored in a string because a 0 at the start of a number causes the program to crash in Python! Aside from that, a 0 at the start of a number in real mathematics, e.g. 0193 is discarded and it becomes 193.

Boolean variables

A Boolean variable is a variable that can be on or off. In programming terms, this is a binary value, a one or a zero. In programming languages like Python the True and False.

Boolean variables are used for comparison and conditions.

Python
v1 = True
v2 = False
    

Global variables

A global variable is a variable that is accessible anywhere with the current scope. This means, in most cases, that it can be accessed at all times.

Python uses the global keyword to indentify that the variable being used in the current context is the global variable. Take a look at the following sample to see how it works:

Python
v1 = True
v2 = True

def main1():
  global v1
  print (v1)
  # Try uncommenting the line below:
  # print (v2)
  v1 = False
  v2 = False
  print (v1)
  print (v2)

def main2():
  print (v1)
  print (v2)

main1()
main2()
    

Combining variables

String and numeric values cannot be combined directly, they must cast to one or the other first.

The following example shows what cannot be done in Python.

Python
#Joining a string and a number
v = "Hello ID number " + 403
#Joining a number and a string
v = 403 + " welcome to the system."
print (v)
    

The plus can join a string to another string but it cannot join a string to a numeric value like 403 and vice versa.

To do this, the values must be cast:

Python
#Joining a string and a number
v = "Hello ID number " + str(403)
#Joining a number and a string
v = str(403) + " welcome to the system."
print (v)
    
Feedback 👍
Comments are sent via email to me.