Mon Oct 15 2018

Data types and its utility in Python

Data types and its utility in Python

In computer programming, a data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. A data type provides a set of values from which an expression (i.e. variable, function...) may take its values. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. Most programming languages support various types of data such as - Python, Java, C, etc.

Here, in this post, we will discuss the data types and its utility in Python.

So, let's get started -

All data values in Python are encapsulated in relevant object classes. Everything in Python is an object and every object has an identity, a type, and a value. Like another object-oriented language such as Java or C++, there are several data types which are built into Python. Extension modules which are written in C, Java, or other languages can define additional types.

Types of data in Python

Numbers

Python's built-in core data types are in some cases also called object types. Numbers are created by numeric literals. Numeric objects are immutable, which means when an object is created its value cannot be changed. There are four built-in data types for numbers: integers, long integers, floating point numbers, and complex numbers. Integers represent negative and positive integers without fractional parts whereas floating point numbers represent negative and positive numbers with fractional parts. In addition, Booleans are a subtype of plain integers. Long integers numbers are of unlimited size e.g.420000000000000000L. A complex number generally used in engineering. It's a number of the form A+Bi where i is the imaginary number. Complex numbers have a real and imaginary part. Python supports complex numbers either by specifying the number in (real + imagJ) or (real + imagj) form or using a built-in method complex(x, y).

Strings

Another important data type besides numbers is strings. A string in Python consists of a series or sequence of characters - letters, numbers, and special characters. Strings can be indexed - often synonymously called subscripted as well. Similar to C, the first character of a string has the index 0.

Strings are marked by quotes:

Wrapped with the single-quote ( ' ) character :- 'Strings are marked by quotes'

Wrapped with the double-quote ( " ) character :- "Strings are marked by quotes"

Wrapped with three characters, using either single-quote or double-quote:- '''A String in triple quotes can extend

over multiple lines like this one, and can contain

'single' and "double" quotes.'''

Escape Sequences

The backslash (\) character is used to escape characters, i.e. to "escape" the special meaning, which this character would otherwise have. Examples of such characters are the newline, backslash itself, or the quote character. Raw strings use different rules for interpreting backslash escape sequences.  See the following -

\newline - Ignored

\\ - Backslash (\)

\' - Single quote (')

\" - Double quote (")

\a - ASCII Bell (BEL)

\b - ASCII Backspace (BS)

\f - ASCII Formfeed (FF)

\n - ASCII Linefeed (LF)

\N{name} - Character named name in the Unicode database (Unicode only)

\r - ASCII Carriage Return (CR)

\t - ASCII Horizontal Tab (TAB)

\uxxxx - Character with 16-bit hex value xxxx (Unicode only)

\Uxxxxxxxx - Character with 32-bit hex value xxxxxxxx (Unicode only)

\v - ASCII Vertical Tab (VT)

\ooo - Character with octal value ooo

\xhh - Character with hex value hh

Immutable Strings

Like strings in Java and unlike C or C++, Python strings cannot be changed. Trying to change an indexed position will raise an error.

'in' operator in Strings

The 'in' operator is used to check whether a character or a substring is present in a string or not. The expression returns a Boolean value.

Indices and accessing string elements

Strings are arrays of characters and elements of an array can be accessed using indexing. Indices start with 0 from the left side and -1 when starting from the right side.

String Slicing

To cut a substring from a string is called string slicing. Here two indices are used separated by a colon (:). For instance, a slice 3:7 means indices characters of 3rd, 4th, 5th and 6th positions. The second integer index i.e. 7 is not included. You can use negative indices for slicing.

Boolean (bool)

The simplest built-in type in Python is the bool type, it represents the truth values False and True. These values are constants and can be used to assign or compare boolean values.

Tuples

A tuple is a container which holds a series of comma-separated values (items or elements) between parentheses. Tuples are immutable and can hold mix data types. So, you cannot change its content once created.

You can create an empty tuple or create a tuple with a single element. Elements of a tuple are indexed like other sequences. Tuples are immutable which means it's items values are unchangeable.

Slicing a tuple

Like other sequences like strings, tuples can be sliced. Slicing a tuple creates a new tuple but it does not change the original tuple.

Using + and * in Tuples - Use + operator to create a new tuple that is a concatenation of tuples and use * operator to repeat a tuple.

Lists

A list is a container which holds comma-separated values (items or elements) between square brackets where Items or elements need not all have the same type. A list without any element is called an empty list.

List indices

List indices work the same way as string indices, list indices start at 0. If an index has a positive value it counts from the beginning and similarly, it counts backward if the index has a negative value. As positive integers are used to index from the left end and negative integers are used to index from the right end, so every item of a list gives two alternatives indices.

If you give an index value which is out of range then interpreter creates an error message.

List Slices

Lists can be sliced like strings and other sequences.

The syntax of list slices is easy :- sliced_list = List_Name[startIndex:endIndex]

This refers to the items of a list starting at index startIndex and stopping just before index endIndex. The default values for list are 0 (startIndex) and the end (endIndex) of the list. If you omit both indices, the slice makes a copy of the original list.

Mutable Lists

Items in the list are mutable i.e. after creating a list you can change any item in the list.

Using + and * in List - Use + operator to create a new list that is a concatenation of two lists and use * operator to repeat a list.

None

This type has a single value. There is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don't explicitly return anything. Its truth value is false.

Sets

A set is an unordered collection of unique elements. Basic uses include dealing with a set theory which support mathematical operations like union, intersection, difference, and symmetric difference, or eliminating duplicate entries.

Dictionaries

Python dictionary is a container of the unordered set of objects like lists. The objects are surrounded by curly braces { }. The items in a dictionary are a comma-separated list of key:value pairs where keys and values are Python data type. Each object or value accessed by key and keys are unique in the dictionary. As keys are used for indexing, they must be the immutable type (string, number, or tuple). You can create an empty dictionary using empty curly braces.

Bytes

The byte is an immutable type in Python. It can store a sequence of bytes (each 8-bits) ranging from 0 to 255. Similar to an array, you can fetch the value of a single byte by using the index. But you can not modify the value. Byte objects contain a sequence of bytes whereas the strings store sequence of characters.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.