Variables in Python

Hrishikesh Deshmukh
2 min readFeb 5, 2022

--

  • Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.
  • It is helpful to think of variables as containers that hold information.
  • Their sole purpose is to label and store data in memory.
  • This data can then be used throughout our program.
  • As Python is Dynamically typed language there is no need to use data types explicitly while creating the variable.
  • Depends on the value that we initialise interpreter decides its data type and allocates memory accordingly.

Integer:-

  • In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python. Integers can be binary, octal, and hexadecimal values. All integer literals or variables are objects of the int class.
print("Demonstration of Variables in Python")no = 11# Considered as integer

Float:-

The float type in Python represents the floating point number. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. eg:- 1.5,10.5,50.87

fvalue = 3.14# Considered as float

String:-

String is a collection of alphabets, words or other characters. It is one of the primitive data structures and are the building blocks for data manipulation. Python has a built-in string class named str . Python strings are “immutable” which means they cannot be changed after they are created.

name = "Hrishikesh Deshmukh"# Considered as String

Complex:-

A complex number has two parts, real part and imaginary part. Complex numbers are represented as A+Bi or A+Bj , where A is real part and B is imaginary part. Python supports complex data type as built-in feature which means we can directly perform different operations on complex number in python.

cvalue = 10 + 5j#considered as Complex number

You can check the type of variable by using type() function:-

print(type(no))
#Above code will result <type 'int'>
print(type(fvalue))
#Above code will result <type 'float'>
print(type(name))
#Above code will result <type 'str'>
print(type(cvalue))
#Above code will result <type 'complex'>

--

--

Hrishikesh Deshmukh
Hrishikesh Deshmukh

Written by Hrishikesh Deshmukh

I am a Data analyst with hobby of writing blogs

No responses yet