Learn Python – How to declare a variable in Python- Basic and advance

Python is a dynamic-typed language, which skill we do not want to mention the variable kind or declare earlier than the usage of it. It makes to Python the most environment friendly and handy to use language. Every variable is handled as an object in Python.

Before declaring a variable, we should observe 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 announcement of a few primary variables.

Numbers

Python supports three kinds of numbers – integer, floating point numbers, and complex. We can declare a variable with any length, there is no restrict proclaims any size of the variable. Use the following syntax to declare the quantity type variables.

Example –

num = 25  
print("The type of a", type(num))  
print(num)  
  
float_num = 12.50  
print("The type of b", type(float_num))  
print(float_num)  
  
c = 2 + 5j  
print("The type of c", type(c))  
print("c is a complex number", isinstance(1 + 3j, complex))  

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 the use of single quotes, double quotes, or triple quotes. Let’s recognize the following example.

Example –

str_var = 'JavaTpoint'  
print(str_var)  
print(type(str_var))  
  
str_var1 = "JavaTpoint"  
print(str_var1)  
print(type(str_var1))  
  
str_var3 = '''''This is string  
using the triple  
Quotes'''  
print(str_var3)  
print(type(str_var1))  

Output:

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

Multiple Assignments

  1. Assigning a couple of values to multiple variables

We can assign the more than one variable concurrently on the equal line. For instance –

a, b = 5, 4  
print(a,b)  

Output:

5 4

Values are printed in the given order.

2 Assign a single cost to the a couple of variables

We can assign the single fee to the multiple variables on the identical line. Consider the following example.

Example –

a=b=c="JavaTpoint"  
print(a)  
print(b)  
print(c)  

Output:

JavaTpoint
JavaTpoint
JavaTpoint