Use Of Variables In Python Language

Watch video presentation and remember to subscribe to our channel;

Watch now!

In this tutorial we will discuss how you can create variables, various types of variables and how you can assign multiple values and how to output variables giving out relevant examples. You will find that python has no command for declaring variable, the variable is created once a value is assigned to it first.

variable- it refers to the quantity that can change anytime within the context of a mathematical problem, they represented by letters the commonly used one are x, y, z. variables are just used to store values. let’s have an example of variables;


x = 8  #this is a int type
y = 12.5  #this is a float type
z = "philloh"  #this is a string type
print(x)
print(y)
print(z)

How to write variable names?

In this part we will discuss how you can write variables, there are various ways on how you can effectively write your variable but before that we will discuss various rules governing writing of variable names;

  • Variables are case sensitive, this means that if you define your variable in small caps then you should print out in small caps, for instance NAME and name are very different variable names.
  • You need to know that the variable name cannot start with a number, for instance 9name is wrong. So if you have write this then you need to include a underscore before 9 as follows _9name now this is correct.
  • Otherwise your variable should start with any letter from A-Z or a underscore(_).
  • You can as well write your variable with just a single letter like x, y, z, k, l, m, a, g etc.

Let's have example of this variables for better understanding;


y = 9
age = 20
my_country = 'kenya'  #you can also use single quotes when writing string type variables
_7hobby = "programming"
MYNUMBER = 0746092415

How to write variables for more than one word?

It may come to a point when you want to write variables for more than one word, therefore we see various ways on how you can write them as follows;

i) snake case - in this case every word is separated from each by a underscore(_), for instance we may have; my_country_name = "kenya"

ii) camel case -in this case every word starts with a capital letter after the first word, for instance; myCountryName = "kenya"

iii) pascal case - in this case every word of the variable begins with a capital letter for instance; MyCountryName = "kenya"

Assigning multiple values in variables

You may wish to assign multiple values when writing variables in one line. when writing you need to ensure that the number of variables equals to the number of values as given in the following example, therefore you will have to write like this;

example 1


x, y, z = "Javascript", "python", "PHP"
print(x)
print(y)
print(z)

Example 2

It may come a point where you assign the same value to various variables, let's see this example;

x = y = z = "Python"

print(x)

print(y)

print(z)

Example 3

In other case you can extract values into variables where by you have lists of values, you can probably extract them through process known as unpacking. You assign the values the variables then you print out, let's have an example here;


languages = ["Javascript", "Python", "Java"]

a, b, c = languages  #you assign the values the variables

print(a)

print(b)

print(c)

How to output variables?

Let's now discuss how you can output variables, whereby you will find that we uses + sign variables with either text and a variable or just variables see this examples;

Example 1


y  = "python"

print("We are learning " + y)

It will output
We are learning python

Example 2

How you can combine variables with a + sign


y  = "We are learning"

z = "python"

a = y + z

print(a)

It will output
We are learning python

Example 3

You can combine numbers with a + sign but it serve as a mathematical number and adds up the numbers as we will be discussing in our operators tutorial. When you combine a number and a string with a + sign it brings an error. Let's have an example here;


a = 8

b = 9

print(x + y)

It will output 17

Various types of variables

There are two types of variables;

  • global variables - these refers to the variables that are created out of the function, they can be utilized by anyone either inside or outside the functions.
  • local variables -these are variables that are created inside the function and they are only used within that function.

Example 1

We can create a variable outside the function but still use it inside the function, let see here;


y = "python"

def myfunc():

print("We are learning " +  y)

myfunc()

Example 2

You can create also create a variable inside the function with the same name as the variable, therefore this variable will be local and can only used inside the function but the global variable with same name will remains as it was with its original value, let's have an example here;


y = "python"

def myfunc():

y = "Javascript"

print('We are learning " + y)

myfunc()

print("We are learning " + y)

Example 3

As we have discussed in the previous example we have seen that you can create a global variable inside a function but it will be local therefore can only be used within the function , therefore you can use a global keyword global to create a global variable inside a function as follows;


def myfunc():

global y

y = "python"

myfunc()

print("We are learning " + y)

Thanks for reading through the tutorial, hopes you understand use of variables in python language. Lets meet in our next tutorial.