My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
DATA TYPES AND DATA STRUCTURES IN PYTHON

DATA TYPES AND DATA STRUCTURES IN PYTHON

Idayat Oshodi's photo
Idayat Oshodi
·Jun 12, 2020

Data Types

In computer programming, a data type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. A data type constrains the values that an expression, such as a variable or a function, might take. This 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. Some examples are stated below:

• Text Type: Strings

Strings in Python are shown as the variable type (str). You can define a string with either double quotes (") or single quotes ('). If the string you are creating actually has one of these two values in it, then you should include a " \ " in your string to be able to use quotations in string:

string.JPG

• Numeric Type: (int, Float). Int is for whole number values like 4, 7, 10, while float is for decimal values like 4.7, 8.9

• Boolean Type: bool. Data types with only two possible outcomes (True/False, 1/0)

• Binary Types: bytes, bytearray, memoryview

Data Structures

Data structures are containers that organize and group data types together in different ways. There are two things to keep in mind when working with data structures: Mutability and Order.

Mutability is about whether or not we can change an object once it has been created. If an object (like a list or string) can be changed (like a list can), then it is called mutable. However, if an object cannot be changed, then the object is considered immutable.

Order is about whether the position of an element in the object can be used to access the element. Types of Data Structures: Lists, Tuples, Sets, Dictionaries, Compound Data Structures, etc.

Lists: Lists are created square brackets [ ]. Lists can contain any mix and match of the data types. Lists are mutable and ordered. E.g: [1, 3.4, 'a string',True]

Tuples: A tuple is used for immutable ordered sequences of elements. They are created with parenthesis. Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indices. Unlike lists, however, tuples are immutable - you cannot add and remove items from tuples, or sort them in place.

Dictionaries A dictionary stores mappings of unique keys to values. Below is an example of a dictionary that stores elements and their atomic numbers.

elements = {"hydrogen": 1, "helium": 2, "carbon": 6}

All the data structures mentioned above are indeed complex data types. Data structure in itself is wider than all mentioned above.

Summary of data structures:

data structures.JPG

You can get the data type of any object by using the type() function.

getting data type.JPG

References:

w3schools.com/python/python_datatypes.asp

en.wikipedia.org/wiki/Data_type

Introduction to Python programming– free course on Udacity.