Data Types in Python

Understand Data Types in Python For Sure

Table of Contents

Introduction

Data types are basically the type of data that can be stored in a variable. There are five standard data types in python described below.

Data Types in Detail

Numbers:

Python numbers data type store numeric values. Python supports integers (whole numbers: int and long), floating-point numbers (numbers with decimals: float), and complex numbers. When assigning the numeric values to a variable, number objects are created.

a = 10        # integer value 
b = 10.57     # floating point value 

In the example above, variable ‘a’ is assigned an integer value of 10 while variable ‘b’ is assigned a value of 10.57 which is a decimal value.

Strings:

Python strings are a set of characters written within quotation marks (single or double quotes). The example below demonstrates the assignment of a string variable in python.

message = 'Hello from Python'    # string value

Lists:

Python Lists are similar to arrays. Lists can contain any type and number of variables. Similar to arrays, the index in lists also starts from 0. Hence, the first element of the list has an index of 0.

Lists are enclosed in brackets ( [ ] ). Below is the assignment of a list and the method of accessing values from a list.

numbers = [5,10,15,20,25]                    # numbers list
names = ['John', 'Mary', 'Jane']             # strings list
mix = ['1', 'Hey', '10', '20', 'Hi']         # list containing numbers and strings

# accessing and displaying values from list
print numbers[0]               # displays 5, first element
print numbers[4]               # displays 25, fifth element
print names[1]                 # displays 'Mary', second element
print mix[2]                   # displays 10, third element
print mix[4]                   # displays 'Hi', fifth element

Tuples:

Tuples in Python are similar to lists however, unlike lists, tuples cannot be updated and are enclosed in parentheses( ( ) ). Below is the assignment of a list.

values = (10, 'Hello', 'Hi', 50)                # assigning values tuple
print values[0]                                 # displays 10, first element 
print values[2]                                 # displays 'Hi', third element

Note: The index of elements in the list and tuple starts from 0.

Dictionary:

Dictionary is one of the data types in Python similar to associative arrays that contain values in the form of key-value pairs. Dictionaries are enclosed by curly braces ( { } ). The elements are not accessed by using an index rather the key determines the value. Below is the example to demonstrate the assignment of a dictionary and accessing values.

new_dict = {}
new_dict['name'] = 'John'

next_dict = {'name': 'Jane', 'age': 25}

print new_dict['name']      # displays John; the value associated with the name key
print next_dict['age']      # displays 25; the value associated with the age key
Find More on Python Here

Leave a Comment

Your email address will not be published. Required fields are marked *