How to declare variable in Python

What is variable in Python?

In Python, a variable is an user defined name that is used as a container to store data values.

Variables are used to store and manipulate data in Python program.

The data type of a variable in Python needs not to be specified explicitly.

In Python, you can declare a variable by simply assigning a value to a variable name.

The interpreter automatically determines the data type based on the value assigned to a varibable.

Declaring variable in Python

Declare a variable name message and assign the text message "Welcome to Python World" using assign operator'=' as shown in the following example.

Use Python built-in 'print()' function to display the message in the output.

Please take a closer look at the following example:

Example of Python Multi-line Comment


Output of the above example

Welcome to Python World

The output displays the variable value.

Creating more variables of different data types

Declare a variable name age and assign integer number 25 using assign operator'='.

Declare a variable name height and assign float number 1.75 using assign operator'='.

Please take a closer look at the following example:

Example of Python Variables


Output of the above example

John is 25 and his height is 1.75

Live Code Playground

In the following Python code editor, you can practice the above code by declaring more variable. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output