Python Datatypes

In any programming language, a variable can hold different types of data. As Python is a dynamically typed language, we do not need to define the type of variable. The interpreter implicitly binds the value provided with its type.

What are Python standard data types?

Python has five standard data types:-

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

In this tutorial, we will have a brief look at each of the data types.

Python Numbers

Number data types store numeric values. Number objects are created when you assign a value to them. Python supports four different numerical types −

  • int (signed integers)
  • long (long integers, they can also be represented in octal and hexadecimal)
  • float (floating point real values)
  • complex (complex numbers)
TypeFormatDescription
inta = 10Signed Integer
longa = 345LLong integers, they can also be represented in octal and hexadecimal
floata = 45.67Floating point real values
complexa = 3.14JNumber in the form of a+bj

Example of Python Numbers:-

Python Strings

A string is a character of sequence in any programming language. Python strings are declared using a single quote or double quote or triple quote.

We can use a slice operator [] and [:] to get a substring from a string in Python. The index of string starts from 0 and -1 represents the end of the string. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.

Let us explore some examples:-

Python Lists

Python list is ordered data type i.e. with heterogeneous data. List are enclosed in brackets []. The list is the most widely used data type in Python. We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.

Below is an example of list with simple operations:-

Output:-

[‘abcd’, 786, 2.23, ‘john’, 70.2]
abcd
[786, 2.23]
[2.23, ‘john’, 70.2]
[123, ‘john’, 123, ‘john’]
[‘abcd’, 786, 2.23, ‘john’, 70.2, 123, ‘john’]

Python Tuples

Tuples are an immutable sequence of items meaning it cannot be modified once the variable is created.  They can be viewed as read-only lists. The data can be heterogeneous as the list. Tuples are enclosed in small brackets (). We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.

Example of Tuple:-

Python Sets

Sets are unordered collection in Python containg unique items. They are created using braces {} and its items being separated by a comma. The items inside sets are not ordered as in lists and tuples. Sets allows operations like union, intersection on two different sets. The main advantage of set is that it eliminates the duplocates in the items. Sets cannot be accessed using slicing operator as there is nothing called index in a set.

Output:-

set([1, 2, 3, 4, 5])
set([‘mango’, ‘apple’, ‘banana’])

Python Dictionary

In Python, dictionary is one of the useful data type that can contain key-value pairs. This is used when we have different kinds of data in a larger volume. They are optimized for retrieving data and are used by many developers as they provide ease of use. In Python, braces {}  are used to define dictionaries where each item is a pair in the form of key: value. Here, key and value can be of any type.

Let us use an example below to be more famlier with Dictinary data type in Python.

Output:

Alex
[‘surname’, ‘name’, ‘roll’]
True

How to find type of python variable?

We can use type() function in python to know under which data type the variable belongs to.

Output:-

200 is of type <class ‘int’>
(3+5j) is of type <class ‘complex’>
SHARE Python Datatypes

You may also like...

Leave a Reply

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

Share