Python Variables With Examples

This article entitle Python Variables is a continuation of the previous article entitled Python Comment.

Python Variables is a reserved memory location that is used to store values in a program in Python. It simply means that when you create a variable you reserved some spaces in memory.

What are Variables in Python?

In Python, the variable is a symbolic name that served as a reference or a pointer to an object. Once the object is assigned to a variable you can refer to it or call it an object in that name.

What are the 4 types of variables in Python?

  • String
  • Number
  • Tuple
  • List

Creating Variables in Python

Further, a variable can be created manually when you assign a value to it, and the equal ( = ) sign was used for assigning a value to a variable.

You can easily identify a variable and the value by just seeing the left and right of the equal ( = ) operand, the operand on the left of the equal ( = ) operator is the name of the given variable, and the right of the equal ( = ) operator is the value that stored on the given variable.

Example

number = 200         # Creates an integer variable
name    = "Glenn Magada Azuelo"   # Creates a string variable
miles   = 1200.5       # Creates a floating point variable

Printing variables in Python

Once we have already created a variable and have already assigned a value to it, we can easily print the assigned value using a print() function.

Example

number = 200         # Creates an integer variable
name    = "Glenn Magada Azuelo"   # Creates a string variable
miles   = 1200.5       # Creates a floating point variable

print(number)
print(name)
print(miles)

Output

200
Glenn Magada Azuelo
1200.5

Delete a variable in Python

The del statement allows us to delete references to an object number.

Syntax

del num1[,num2[,num3[....,numN]]]]

Example

del num
del num_a, num_b

Multiple assignments in Python

Python allows you to create multiple variables in one simultaneously.

Example

x = y = z = 300

print (x)
print (y)
print (z)

Output

300
300
300

Variable names in Python

Each variable name should be unique such as x, y, and z. or a variable should be relevant to the assigned value such as gender, name, last name, and soon.

Further, there are certain rules that could be taken care of on naming a variable in Python.

  • Variable names should only contain an alphanumeric characters and underscores (0-9, A-z, and _ )
  • Variable names should not be started with any special character or a number ( $, (, * %, etc. )
  • The reserved keywords in Python cannot be used as a variable name.
  • Variable names must start with an underscore character or a letter.
  • Variable names is case sensitive it simply means that the Gender and GENDER were two different variables

Example

value = 200
_counter  = 1
programmer1 = "Jude"
programmer2 = "Glenn"
Age  = 26
salary = 50000

print (value)
print (_counter)
print (programmer1)
print (programmer2)
print (Age)
print (salary)

Output

200
1
Jude
Glenn
26
50000

Local variable in Python

Local variables are specified inside the function. It means that we cannot access that variable outside of the function.

Example

def sum(a,b):
   sum = a + b
   return sum
print(sum(20, 30))

Output

50

Global variable in Python

A global variable is any variable that has been created outside of the function and can be accessed within the entire program.

Example

a = 20
b = 30
def sum():
   sum = a + b
   return sum
print(sum())

Output

50

Summary

In summary, you have read about Python Variables. We also discussed in this article what are Variables in Python, 4 types of variables, creating variables, printing variables, deleting a variable, multiple assignments, variable names, local variables, and global variables.

I hope this article about Python Variables could help you a lot to continue pursuing learning this powerful programming language.

If you want to learn more check out my previous and latest articles for more career-changing articles.

Leave a Comment