Friday, August 26, 2022

Understanding the Numeric and Sequence Data Types

Python provides three main numeric types for handling different kinds of numeric data:

• int. This data type is used for storing integer numbers — numbers that do not have a decimal component. For example, 0, 3, 42, and 4817 are all integers. Technically, the bool data type for storing Boolean values is a subtype of int.

• float. This data type is used for storing floating‐point numbers, those that have a decimal component. For example, 9876.54321 is a floating‐point number. 

• complex. This data type is used for storing complex numbers — numbers that consist of a real component and an imaginary component.

Sequence Data Types

In Python, a sequence is a set of data that is ordered — in other words, it has a specific order. Some sequence data types are immutable, or unchangeable, whereas others are mutable, or changeable.

The following list explains the main data types in the sequence category:

• list. This data type contains a sequence of similar items — for example, a list of integers might contain 1, 2, and 3, or a list of strings might contain dog, cat, and snake. Lists are mutable, so you can change their contents, their order, or both. 

• tuple. This data type is used to store an ordered sequence of values. The values do not need to be unique, so a tuple can contain multiple instances of the same value. A tuple is immutable, so you cannot change its contents or its order once you have created it. 

• range. This data type is used to contain an immutable sequence of integer values — for example, from 1 to 10. Ranges are often used to control the number of iterations in for loops.

• str. This data type is used for storing strings of text. Python considers a string to be an immutable —unchangeable — sequence of characters. 

Share:

0 comments:

Post a Comment