Site icon Digital Digest Up2Date

Data Types in Python

Data Types in Python

Data Types in Python

The classification or categorising of data elements is known as data types. It represents the type of value that specifies the operations that can be carried out on a given set of data. In Python programming, everything is an object, hence variables and data types are both instances (or objects) of the same classes.

The standard or built-in data types for Python are as follows:

Numeric : Python’s numeric data types are used to represent information using numerical values. Integer, floating-point, or even complex values can make up a numerical value. Python defines these values as belonging to the int, float, and complex classes.

Integers – The int class is used to represent this value. There are both positive and negative whole numbers in it (without fraction or decimal). An integer value’s length is unrestricted in Python.
Float – The float class is used to represent this value. It is a real number that is represented in floating point. There is a decimal point to indicate it. To designate scientific notation, the letters e or E may be added after a positive or negative number.
Complex Numbers – The complex class is a representation of a complex number. (Real portion) plus (Imaginary part)j is how it is specified. For instance, 2+3j
Note: To determine the type of data type, use the type() method.

# Python program to 
# demonstrate numeric value
  
a = 7
print("Type of a: ", type(a))
  
b = 9.0
print("\nType of b: ", type(b))
  
c = 3 + 6j
print("\nType of c: ", type(c))

Output:

Type of a:  <class 'int'>

Type of b:  <class 'float'>

Type of c:  <class 'complex'>

Sequence Type: Sequence in Python is an ordered grouping of related or dissimilar data types. Sequences enable the ordered and effective storage of several values. In Python, there are various sequence types.

String : Strings in Python are collections of bytes that represent Unicode characters. A single, double, or triple quote around a group of one or more characters is called a string. A character in Python is a one-length string; there is no character data type. The class str is used to represent it. Creating Strings Python allows the creation of strings using single, double, or even triple quotes.

List : Lists are ordered collections of data, similar like the arrays expressed in other languages. Since a list’s items don’t have to be of the same type, it is immensely flexible. List creation in Python is as simple as putting the sequence inside square brackets[ ].

Tuple : Tuple is a similar ordered grouping of Python objects to list. Tuples are immutable, or cannot be changed after creation, which is the only way they vary from lists. Tuple class serves as its representation.
Creating Tuple – In Python, tuples are made by grouping together a sequence of values separated by a “comma” with or without the use of parenthesis ( ). Tuples may include any number of elements and any form of data (like strings, integers, list, etc.).The creation of tuples from a single element is also possible, although it is more difficult. One element in the parentheses is insufficient; a trailing “comma” is required to convert it into a tuple.

Boolean : Contains either True or False as one of its two predefined values. Equal to True Boolean objects are truthy (true), whereas equal to False Boolean objects are false (false). However, non-Boolean items can also be assessed in a boolean environment and classified as true or false. It is represented by the bool class.Note: Boolean values must start with a capital T or F in order to be accepted by Python.

Set : Set is an iterable, changeable, unordered collection of data types with no duplicate items in Python. A set may contain a variety of pieces, but there is no predetermined order for the elements.

Creating Sets – The built-in set() function can be used to generate sets with an iterable object or a series by enclosing the sequence inside curly braces and separating them with a comma. A set’s items don’t have to be of the same type; different values of mixed data types can be be supplied to the set.

Dictionary : In Python, a dictionary is an unordered collection of data values that can be used to store data values like a map. In contrast to other data types, which can only carry a single value, dictionaries can also hold a key:value pair. The dictionary includes key-value pairs to help it become more efficient. In a dictionary, each key-value pair is separated by a colon; in contrast, each key is separated by a comma.
Creating dictionaries – By putting a series of components within curly brackets and separating them with a comma, one can create a dictionary in Python. In contrast to keys, which cannot be repeated and must be immutable, values in dictionaries can be of any datatype and can be duplicated. The built-in function dict also allows for the creation of dictionaries (). You can make a dictionary that is empty.

Exit mobile version