Python Menu

Data Types in Python


In general, Data Types specifies what type of data will be stored in variables. Variables can hold values of different data types. Python is a dynamically typed or loosely typed language, hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.


I.e. In loosely typed language no need to specify the type of variable because the variable automatically changes it's datatype based on assigned value.

Python provides the type() function which enables us to check the type of the variable.

Python provides following standard data types, those are

  1. Numbers
  2. String

1. Numbers

Number stores numeric values. Python creates Number type variable when a number is assigned to a variable.
There are three numeric types in Python:

  1. int
  2. float
  3. complex

a. int

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

format:
a = 10
b = -12
c = 123456789

b. float

Float or "floating point number" is a number, positive or negative, containing one or more decimals.

format:
X = 1.0
Y = 12.3
Z = -13.4

c. complex

Complex numbers are written with a "j" as the imaginary part.

format:
A = 2+5j
B = -3+4j
C = -6j

 Python Example Program - Number Datatypes in Python


Example: "datatypedemo1.py"
a = 10  
b = 10.5
c = 2.14j
print("Datatype of Variable a :",type(a))   
print("Datatype of Variable b :",type(b))   
print("Datatype of Variable c :",type(c)) 
Output: $python3 datatypedemo1.py
Datatype of Variable a : <class ‘int’>
Datatype of Variable b : <class ‘float’>
Datatype of Variable c : <class ‘complex’>

2. String

The string can be defined as the sequence of characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string.

In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".

format:
S1='Welcome'
S2="to Python"
S3='''world'''

 Python Example Program - String Datatypes in Python


Example:
a = "Welcome"		#using double quotes  
b ='Python'		#using single quotes 
c = '''World'''		#using triple quotes 
print("Datatype of Variable a :",type(a))   
print(a+b)   #to concatenate two strings 
Output:
Datatype of Variable a : <class ‘str’>
Welcome Python

Next Topic :Typecasting in Python