In programming language Data Type is an important concept. Python has built in data types. Variables can hold values of different data types.

Python provides us the type() function which returns the type of the variable passed.

Here is the example program to checking its type.

Eg:

a=10
b="svrrtech"
c='s'
d=12.45
print(type(a))
print(type(b))
print(type(c))

print(type(d))

OUTPUT:

<class 'int'>
<class 'str'>
<class 'str'>
<class 'float'>

Standard Data Types:


In Python there some standard Data Types, which defines the storage method on each of them. they are.

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

1. Numbers:

Number stores numeric values. In python there are three types of numbers 

  1. int
  2. float
  3. complex

1.1 int

The number 12 is an example for numeric value or integer value. Integer are whole numbers, integers have no fractional parts, the integer may be positive, negative, or zero. (Eg: 6, -3, 0, -908). Hence 4.8 is not an integer, since it is a whole number.

Eg:

a=10
print(type(a))
print(a)

OUTPUT:

<class 'int'>
10

1.2 float


Numbers created using a float variable declaration will have digits on both sides of a decimal point. This is in contrast to the integer data type, which houses an integer or whole number.The float method is used to return a floating point number.

Eg:

a=9.34
print(a)
print(type(a))
print(float(6))
print(float(-45))
print(float( -56.8))

OUTPUT:

9.34
<class 'float'>
6.0
-45.0
-56.8

1.3 complex


A complex number is represented by "2+5i". Python convert the real number into complex number using complex() function. We can use complex() function by importing "cmath" function.

We can access the real part using the real() function and the imaginary part can be accessed by imag().

Eg:

import cmath
a=8
b=6
c=complex(a,b);
print("The real part is:",c.real)
print("The imaginary part is:",c.imag)

OUTPUT:

The real part is: 8.0
The imaginary part is: 6.0

2. String:


Strings are defined either with a single quote, a double quotes or triple quotes. 

Eg:

a='svrr tech'
b="python tutorial"
c="""Hello World"""
print(a)
print(b)
print(c)

OUTPUT:

svrr tech
python tutorial
Hello World

3. List


Lists are similar to arrays in java, list can contain data of different types. In Python List can be created just by placing the sequence inside the square brackets[] and the items stored in the list are separated with a comma.

Eg:

mylist=["regu","ram","hari"]
print(mylist)

OUTPUT:

['regu', 'ram', 'hari']

4. Tuple


A tuple is just like lists the main difference is a tuples cannot be changed.In Python tuples are written with round brackets[] and the tuples stored in the list are separated with a comma.

Eg:

mytuple=("regu","ram","hari")
print(mytuple)

OUTPUT:

('regu', 'ram', 'hari')


5. Dictionary


Dictionary in python is an unordered collection of data values, and it is changeable and indexed. Dictionaries are written with curly brackets and dictionaries have keys and values.

Eg:

mydict={"name":"Regu Ram","age":19,"city":"Coimbatore"}
print(mydict)

OUTPUT:

{'name': 'Regu Ram', 'age': 19, 'city': 'Coimbatore'}



Post a Comment

أحدث أقدم