Python - Data Types

Every value in Python has a data type. There are different types of data types in Python are Numbers, Sequences (List, Tuple, Strings), Dictionary, Sets etc.

Standard data types:

Data Type Name Data Types
Number Int, float, complex
Sequence List, tuple, range
Mapping (Dictionary) dict
Set Set, frozenset
Text str
Boolean bool
Binary bytes, bytearray, memoryview

In Python, there is a built-in function ‘type’ to get the type of the data.

Numbers

Numbers are numeric values which are three different types.

  • Integers
  • Float
  • Complex

Python creates Number objects when assign a value to variable.

a = 10;
print("The type of the data is:",type(a));# output is int
a = 10.5
print(“The type of the data is:”type(a)); #output is float
a=2j
print(“The type of the data is:”type(a));#output is complex


The data types mostly used in Python are Strings, Lists, Tuples, and Dictionaries.

String (or text)

The string is denoted as a sequence of characters represented in quotation mark (single quotation or double quotation).


Python has the built-in functions and operators to handle the string operations.

Example:

atr1= "Hello";
print(str1); # Output is “Hello”


The operator + is used to concatenate the strings together.

Example:

str1= "Hello"
str2=" World";
Print(str1+str2); # Output is "Hello World"


Three double quotes or three single quotes are used for handling the multiline string.

Example 1:

str1=""" Python language is easy 
to learn"""
print(str1) # Output is "Python language is easy to learn"

Example 2:

str1='''Python language is easy 
to learn''';
Print(str1) # returns "Python language is easy to learn";


The operator * is used to iterate the string for specified times.

Example:

str1="Hello.."
print(str1 * 3); # returns "Hello..Hello..Hello.."

Lists

Python List data structure is mutable which means it can be changeable by adding or deleting the items.

The List can have the sequence of items in square brackets [].

The List items are in insertion order.

The List items start from index zero (0).

The List can have duplicate items.

The List can contain different data types of items which includes Integers, Strings, and Booleans.

Python List works like a Strings use the len() function and square brackets [] to access the data.

Examples:

  1. Create a Python List
  2. colors = ['blue','red','green','yellow']
    print(colors[0])		## blue
    print(colors[1])		## red
    print(colors[2])		## green
    print(colors[3])		## yellow
  3. Assign colors list to another variable ‘c’
  4. colors = ['blue','red','green','yellow']
    c = colors


    Assigning colors to variable c, it doesn’t copy the new list. Instead, the two variables colors, and c points to the same list.

  5. Create a List with duplicate items
  6. colors = ['blue','red','green','red']
    print(colors[0])		## blue
    print(colors[1])		## red
    print(colors[2])		## green
    print(colors[3]) 		## red
  7. Find the number of items in the Python List
  8. colors = ['blue','red','green','yellow']
    ## Print the number of items in colors list
    print(len(colors)) 		## 4
  9. Create a list with different data types
  10. employee = ['Rohan',30,'Male',False]                        
                                


Tuples

Python Tuple data structure is immutable which means it is unchangeable which means it can not be changed by adding or deleting the items.

The Tuple can have the sequence of items in parenthesis or round brackets ().

The Tuple items are in defined order that means the items are in same order when the Tuple has created.

The Tuple items start from index zero (0).

The Tuple can have duplicate items.

The Tuple can contain different data types of items.

Examples:

  1. Create a Python Tuple
  2. colors = ('blue','red','green','yellow')
    print(colors[0])		## blue
    print(colors[1])		## red
  3. Create Python Tuples for different data types
  4. numtuple = ( 1, 2, 3.5, 8)
    booleantuple = (True, False, True, True)
    stringtupele = ('blue','red','green','yellow')
  5. Create a Python Tuple with data types of items
  6. employee = ('Rohan',30,True,'Male')                   
                                
  7. Find the number of items in the Tuple
  8. colors = ('blue','red','green','yellow')
    print(len(colors))		## 4
  9. Create a Python Tuple with duplicate items
  10. colors = ('blue','red','blue','yellow')
    print(colors[0])		## blue
    print(colors[1])		## red
    print(colors[2])		## blue
    print(colors[3])		## yellow


Dictionary

Dictionary data structure is a more powerful data type.

Dictionary is used to store the one or more key, value pair data.

Dictionary does not allow the duplicate items with the same key.

Dictionary can be altered by adding, removing the items.

Dictionary can have the items in defined order.

Dictionary represents in curly brackets {}.

Examples:

  1. Create a Python Dictionary
  2. empdict = {
        'name':'John',
        'id':'50055',
        'email':'abc@gmail.com',
        'phone': ['955-55-95555','955-55-95556']
    }
    print(empdist) 	 ## Output is {'name': 'John', 'id': '50055', 'email': 'abc@gmail.com', 'phone': ['955-55-95555', '955-55-95556']} 
  3. Access the values by using key from Python Dictionary.
  4. empdict = {
        'name':'John',
        'age': 30
    }
    print(empdict['name'])		## John
    print(empdict['age']) 		## 30



Related Tutorials