codescracker


python

Python Data Types



« Previous Tutorial Next Tutorial »


Basically data type represents the type of value and determines how the value can be used in a python program. Since all the data values are encapsulated in relevant object classes.

Everything in Python, is simply an object and every object has an identity, a type and a value. There are following five standard data types available in Python programming:

Python Number Data Type

Python number data type is used to store the numeric values. Here are some examples:

i = 10
f = 23.43
total = 987

You will learn in detail about number data type in python in Python Number tutorial.

Python String Data Type

Strings in Python, are contiguous set of characters between quotation marks. You are free to use either pairs of single or double quotes in Python programming. Here is an example of string.

str = "Hello World, I am Python Data Types"

You will learn in detail about string data type in Python in Python String tutorial.

Python List Data Type

A list in Python, contains items separated by commas and enclosed within square brackets. Here is an example of list data type in Python:

list = [10, 20, 30, 'hi', 'hello', 43.23, 432.42, 'python']

You will learn in detail about list data type in Python in Python List tutorial.

Python Tuple Data Type

A tuple in Python, is simply a sequence data type, is similar to the list data type. A tuple consists of a number of values separated by comms. Here is an example of tuple data type in Python:

tuple = (10, 20, 30, 'hi', 'hello', 43.23, 432.42, 'python')

You will learn in detail about tuple data type in Python in Python Tuple tutorial.

Python Dictionary Data Type

Dictionary in python, are the type of hash table type. They works like an associative arrays or hashes found in perl language and consist of key-value pairs. Dictionary are enclosed within curly braces {} and values can be assigned and accessed using the square brackets []/ Here is an example of dictionary data type in Python:

dict = {'number': 10, 'code': 20, 'name': 'python'}

You will learn in detail about dictionary data type in Python in Python Dictionary tutorial.

How to Determine Variable's Type in Python ?

You can use the function type() available in Python, to determine the type of variable in Python. Here is an example showing the use of type() function in determining variable's type in Python:

# Python Data Types - Example Program
i=10
print(type(i))

f=324.423
print(type(f))

b=True
print(type(b))

str="Python Data Types"
print(type(str))

Here is the sample output of the above Python program:

python data types

Examples

Here are some conversion program examples, you may like:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test