Data Types

Python Data Types

Python data types are actually classes, and the defined variables are their instances or objects. Since Python is dynamically typed, the data type of a variable is determined at runtime based on the assigned value.

In general, the data types are used to define the type of a variable. It represents the type of data we are going to store in a variable and determines what operations can be done on it.

Each programming language has its own classification of data items.With these datatypes, we can store different types of data values.

Types of Data Types in Python

Python supports the following built-in data types −

data_types

1. Python Numeric Data Types

Python numeric data types store numeric values. Number objects are created when you assign a value to them. For example −

var1 =1# int data type
var2 =True# bool data type
var3 =10.023# float data type
var4 =10+3j# complex data type

Python supports four different numerical types and each of them have built-in classes in Python library, called int, bool, float and complex respectively −

  • int (signed integers)
  • float (floating point real values)
  • complex (complex numbers)

A complex number is made up of two parts – real and imaginary. They are separated by ‘+’ or ‘-‘ signs. The imaginary part is suffixed by ‘j’ which is the imaginary number. The square root of -1 (\sqrt{-1}), is defined as imaginary number. Complex number in Python is represented as x+yj, where x is the real part, and y is the imaginary part. So, 5+6j is a complex number.

>>>type(5+6j)<class'complex'>

Here are some examples of numbers −

intfloatcomplex
100.03.14j
0O77715.2045.j
-786-21.99.322e-36j
08032.3+e18.876j
0x17-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2-E124.53e-7j

Example of Numeric Data Types

Following is an example to show the usage of Integer, Float and Complex numbers:

# integer variable.
a=100print("The type of variable having value", a," is ",type(a))# float variable.
c=20.345print("The type of variable having value", c," is ",type(c))# complex variable.
d=10+3jprint("The type of variable having value", d," is ",type(d))

2. Python String Data Type

Python string is a sequence of one or more Unicode characters, enclosed in single, double or triple quotation marks (also called inverted commas). Python strings are immutable which means when you perform an operation on strings, you always produce a new string object of the same type, rather than mutating an existing string.

As long as the same sequence of characters is enclosed, single or double or triple quotes don’t matter. Hence, following string representations are equivalent.

>>>'TutorialsPoint''TutorialsPoint'>>>"TutorialsPoint"'TutorialsPoint'>>>'''TutorialsPoint''''TutorialsPoint'

A string in Python is an object of str class. It can be verified with type() function.

>>>type("Welcome To TutorialsPoint")<class'str'>

A string is a non-numeric data type. Obviously, we cannot perform arithmetic operations on it. However, operations such as slicing and concatenation can be done. Python’s str class defines a number of useful methods for string processing. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator in Python.

Example of String Data Type

str='Hello World!'print(str)# Prints complete stringprint(str[0])# Prints first character of the stringprint(str[2:5])# Prints characters starting from 3rd to 5thprint(str[2:])# Prints string starting from 3rd characterprint(str*2)# Prints string two timesprint(str+"TEST")# Prints concatenated string

This will produce the following result −

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

3. Python Sequence Data Types

Sequence is a collection data type. It is an ordered collection of items. Items in the sequence have a positional index starting with 0. It is conceptually similar to an array in C or C++. There are following three sequence data types defined in Python.

  • List Data Type
  • Tuple Data Type
  • Range Data Type

Python sequences are bounded and iterable – Whenever we say an iterable in Python, it means a sequence data type (for example, a list).

(a) Python List Data Type

Python Lists are the most versatile compound data types. A Python list contains items separated by commas and enclosed within square brackets ([]). To some extent, Python lists are similar to arrays in C. One difference between them is that all the items belonging to a Python list can be of different data type where as C array can store elements related to a particular data type.

>>>[2023,"Python",3.11,5+6j,1.23E-4]

A list in Python is an object of list class. We can check it with type() function.

>>>type([2023,"Python",3.11,5+6j,1.23E-4])<class'list'>

As mentioned, an item in the list may be of any data type. It means that a list object can also be an item in another list. In that case, it becomes a nested list.

>>>[['One','Two','Three'],[1,2,3],[1.0,2.0,3.0]]

A list can have items which are simple numbers, strings, tuple, dictionary, set or object of user defined class also.

The values stored in a Python list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

Example of List Data Type

list=['abcd',786,2.23,'john',70.2]
tinylist =[123,'john']print(list)# Prints complete listprint(list[0])# Prints first element of the listprint(list[1:3])# Prints elements starting from 2nd till 3rd print(list[2:])# Prints elements starting from 3rd elementprint(tinylist *2)# Prints list two timesprint(list+ tinylist)# Prints concatenated lists

This produce the following result −

['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']

(b) Python Tuple Data Type

Python tuple is another sequence data type that is similar to a list. A Python tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses (…).

A tuple is also a sequence, hence each item in the tuple has an index referring to its position in the collection. The index starts from 0.

>>>(2023,"Python",3.11,5+6j,1.23E-4)

In Python, a tuple is an object of tuple class. We can check it with the type() function.

>>>type((2023,"Python",3.11,5+6j,1.23E-4))<class'tuple'>

As in case of a list, an item in the tuple may also be a list, a tuple itself or an object of any other Python class.

>>>(['One','Two','Three'],1,2.0,3,(1.0,2.0,3.0))

To form a tuple, use of parentheses is optional. Data items separated by comma without any enclosing symbols are treated as a tuple by default.


Comments

Leave a Reply

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