How To Declare Variable In Python?

Variables are an important part of the programming languages where they are used to store different types of data temporarily. Variables can be created in different ways with different types but in general, they have very similar declaration syntax which is generally assigning initialization data to the newly created variable.

Declare Variable

Python has a very simple variable declaration syntax where the variable is assigned with a value that will be stored inside this variable. In the following example, we create different types of variables with different types.

x = 10

y = "python"

z = [1,2,3,4]

Declare Integer Variable

An integer is one of the most popular and used variable types which is used to store integers. Integers can be used to make a mathematical calculation using different arithmetic operators.

a = 1
b = 2
c = 3

d = a+ b

e = d + c

Declare Floating Point Variable

The floating-point is another popular variable type where floating-point numbers are stored. Just assign a floating-point number to a variable which creates a floating-point variable.

a = 1.1
b = 0.5
c = -3.4

Declare String Variable

A string variable is used to store multiple characters in a variable which is called a string. string variables can be not used in mathematical operations. String variables can be created by assigning a string to a variable.

name = "ismail"

surname = "baydan"

Declare List Variable

The list is another popular type that is used to store multiple items or values in a single variable. List variables can be created by assigning the list items to them. The list items can be in different types like integer, string, floating point even a list.

a = [1,2,3,4]

b = ["ismail","baydan",1,2]

Check Variable Type

Python is a dynamic programming language where variable types are also dynamic too. The type of the variable can be checked or displayed with the type() function by providing the variable name.

x = 10

y = "python"

z = [1,2,3,4]



<class 'int'>
<class 'str'>
<class 'list'>

Leave a Comment