Python Variables

  • Variables are containers for storing data values.

  • There is no any concept  for declaring a variable in python unlike other programming languages.

  • A variable is created the moment you first assign a value to it.

How to Declare and use a Variable

Python variable defined using simple ways like you can see it below given example and print it–

>>> a = 5

>>> print(a)

You can get result 5.

Concatenate Variables

We can concatenate Variables using below given logic:

>>> a = 5

>>> b = 4

>>> print(“The Result is”, str( a)+ “” +str(b))

Naming convention of Variable in python

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


Rules for Python variables:

  • 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 (age, Age and AGE are three different variables)

The common naming styles in Python code and when you should use them:

  • Variable: Use a lowercase single letter, word, or words. Separate words with underscores to improve readability.   Example: x, var, my_variable

  • Constant: Use an uppercase single letter, word, or words. Separate words with underscores to improve readability. Example: PI, MAX_VALUE, MIN_VALUE

  • Function: Use a lowercase word or words. Separate words by underscores to improve readability. Example: display, show, my_function

  • Class: Start each word with a capital letter. Do not separate words with underscores. This style is called camel case. Example: Employee, Student, Person, Model, MyClass

  • Module: Use a short, lowercase word or words. Separate words with underscores to improve readability. Example:,

  • Package: Use a short, lowercase word or words. Do not separate words with underscores.Example: package, mypackage

Assign Value to Multiple Variables once in one line

  •  Assign different values to multiple variables in one line:

x, y, z = 5 ,10 ,15

  •  Assign same value to multiple variables in one line:

x, y, z = 10

Global Variables

Global variable is used to access command value through out the program. Global variable can be accessed from inside or outside from function, class or methods.  

Global variable in python can be defined by two ways:

  • Without global keyword.

  • Using global keyword.

  1. Without global keyword

college_id = 10

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


Note: Always try to define global variable top line of program so that rest of the code after that can use it.

 2. Using global keyword

def myfunc():

     global college_id 

     college_id = 10

print("Python is " + college_id)

What will happen If you create a variable with the same name inside a function

college_id = 10

def myfunc():

    college_id = 20
    print("Id inside function " + college_id)


print("Id outside function " + college_id)


Id inside function  20

Id outside function  10

local value will be used if we create variable with the same name as global

Thanks for reading this, In the next tutorial, we will learn about “python data types

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram