Python Variables

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Python doesn’t require any command for declaring a variable unlike other programming languages.

x = 5  
y = "Ravi"
print(x)
print(y)

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

x = 5   # x is of type int (integers)

y = "Ravi"  # y is of type str (string)
 
#Note - String variables can be declared either by using single or double quotes.

y = 'Ravi'

Declaring Variable Name

A variable can have a short name (like x and y) or a more descriptive name (name, age etc.).

Rules for declaring variables in Python –

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • A variable name must not start with a number.
  • A variable name must start with a letter or the underscore character.
  • A variable names are case-sensitive (name, Name and NAME are three different variables).
  • You cannot use reserved words as variables.
  • There is no length limit for Python identifiers. But not recommended to use too lengthy identifiers.
  • Dollar ($) symbol is not allowed in Python.

Below are the examples of variables names:

123var = "Ravi"    (incorrect)
var123 = "Ravi"    (correct)
var2name = "Ravi"    (correct)
var$name  = "Ravi"    (incorrect)
_abc_abc_ = 'Ravi'   (correct)
def  = "Ravi"    (incorrect)
if  = "Ravi"      (incorrect)

Note:

  • If variable name starts with _  (Single under score symbol ) then it indicates that it is private variable.
  • If variable name starts with __ (two under score symbols) indicating that strongly private variable.
  • If variable name starts and ends with two underscore symbols then the identifier is language defined special name, which is also known as magic method.

Assigning Values to Multiple Variables

You can assign values to multiple variables in one line.

x, y, z = "Amar", "Akbar", "Anthony"
print(x)
print(y)
print(z)

And you can also assign the same value to multiple variables in one line.

x = y = z = 'Ravi'
print(x)
print(y)
print(z)

Global Variables

Variables which are created outside of a function are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

x = 'fantastic'

def myfunc():
print("Python is " + x)

myfunc()

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function.

The global variable with the same name will remain as it was, global and with the original value.

x = 'Love'

def myfunc():
x = 'Love'
print("Python is " + x)

myfunc()

print("Python is " + x)

Output result  a variable

Python print statement is often used to output variables. Python uses the + (plus symbol) character to combine both text and a variable.

x = 'fantastic'
print("Python is " + x)

# You can also use the + character to add a variable to another variable.

x = "Python is "
y = 'fantastic'
z = x + y
print(z)

# For combining numbers, + character works as a mathematical operator.

x = 10
y = 20
print(x + y)

If you try to combine a string with a number, Python will give you type error.

x = 5
y = "Ravi"
print(x + y)

#output

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(x+y)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Hence, you got better understanding of variables in Python programming language.

Previous Read : Python Comments

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

2 thoughts on “Python Variables”

  1. Pingback: Python Data Types
  2. Pingback: Python Booleans

Leave a Comment