Javatpoint Logo
Javatpoint Logo

How to declare a variable in Python

Python is a dynamic-typed language, which means we don't need to mention the variable type or declare before using it. It makes to Python the most efficient and easy to use language. Every variable is treated as an object in Python.

Before declaring a variable, we must follow the given rules.

  • The first character of the variable can be an alphabet or (_) underscore.
  • Special characters (@, #, %, ^, &, *) should not be used in variable name.
  • Variable names are case sensitive. For example - age and AGE are two different variables.
  • Reserve words cannot be declared as variables.

Let's understand the declaration of a few basic variables.

Numbers

Python supports three types of numbers - integer, floating point numbers, and complex. We can declare a variable with any length, there is no restriction declares any length of the variable. Use the following syntax to declare the number type variables.

Example -

Output:

The type of a <class 'int'>
25
The type of b <class 'float'>
12.5
The type of c <class 'complex'>
c is a complex number True

Strings

The string is the sequence of Unicode characters. It is declared using single quotes, double quotes, or triple quotes. Let's understand the following example.

Example -

Output:

JavaTpoint
<class 'str'>
JavaTpoint
<class 'str'>
This is string 
using the triple 
Quotes
<class 'str'>

Multiple Assignments

1. Assigning multiple values to multiple variables

We can assign the more than one variable simultaneously on the same line. For example -

Output:

5 4

Values are printed in the given order.

2. Assign a single value to the multiple variables

We can assign the single value to the multiple variables on the same line. Consider the following example.

Example -

Output:

JavaTpoint
JavaTpoint
JavaTpoint





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA