Python – Variables

Learn about variables in python, declaring local and global variables. Also, learn about global keyword used inside the functions in python.

1. Creating variables

1.1. Simple assignment

Python language has no keyword for declaring a variable. A variable is created in place immediately as we first assign a value to it.

i = 20
blogName = "howtodoinjava"

print(i)			# prints 20
print(blogName)		# prints howtodoinjava

A variable of string type can be created with either single quotes and double quotes, both.

author = 'Lokesh'
blogName = "howtodoinjava"

print(author)	# prints Lokesh
print(blogName)	# prints howtodoinjava

1.2. Chained assignment

Python also allows chained assignment, which makes it possible to assign the same value to several variables simultaneously.

i = j = k = 20

print(i)			# prints 20
print(j)			# prints 20
print(k)			# prints 20

1.3. Multiple assignments in single line

Python allows you to assign values to multiple variables in one line.

x, y, z = "A", "B", 100

print(x)	# prints A
print(y)	# prints B
print(z)	# prints 100

1.2. Variable re-declaration

As variables do not need datatype information, we can reassign a new value of any type without any problem. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type.

index = 10
index = 20
index = "NA"

print(index)	# prints NA

2. Naming Conventions

The rules for creating variables in Python are:

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive. For example – name, Name and NAME are three different variables.

Note: Python 3 has full Unicode support, which allows for Unicode characters in a variable name as well.

3. Local vs global variables

3.1. Creating local and global variables

A variable created inside a function is called local variable.

A variable created outside a function is global variable. Global variables can be used by everyone, both inside of functions and outside.

x = 10		# global variable

def myfunc():
  y = 10	# local variable
  print("Sum of x and y = " + str(x + y))	# prints Sum of x and y = 20


print("Sum of x and y = " + str(x + y))		# NameError: name 'y' is not defined

3.2. Local variables are bounded within function scope

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 = 10		# global variable

def myfunc():
  x = 20	# local variable
  print("x is " + str(x))		# prints x is 20


print("x is " + str(x))			# prints x is 10

3.3. The ‘global’ keyword

To create a global variable inside a function, we can use the global keyword.

x = 10		# global variable

def myfunc():
  global y 
  y = 10	# global variable created inside function
  print("Sum of x and y = " + str(x + y))	# prints Sum of x and y = 20


print("Sum of x and y = " + str(x + y))		# prints Sum of x and y = 20

Drop me your questions related to python variables.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.