Home > Backend Development > Python Tutorial > What are the different built-in types in Python?

What are the different built-in types in Python?

王林
Release: 2023-08-19 14:53:19
forward
1228 people have browsed it

What are the different built-in types in Python?

In this article, you will learn about Python data types and their use in writing Python programs. You'll learn their purpose, syntax, and how to apply them in your program through examples. The Python language needs no introduction. It's very powerful, adaptable, fast and easy to learn.

One of the languages ​​that continues to expand and gain more and more popularity every year is Python. Python is an object-oriented, interpreted computer language used for general-purpose programming. This tutorial will teach us about various data types in Python programming language.

Built-in data types in Python

There are different types of data types in Python. Some built-in Python data types include -

  • Numeric data types - int, float, complex

  • String data type - str

  • Sequence type - list, tuple, range

  • Binary types - bytes, bytearray, memoryview

  • Mapped data type - dict

  • Boolean type - bool

  • Set data type - set, frozenset

Python Numeric Data Type

In Python, the numeric data type is used to save numerical values.

Integers, floating point numbers, and complex numbers belong to Python’s number categories. They are defined in Python as int, float and complex classes.

  • int − Stores a signed integer of unlimited length.

  • float − Save a floating point number, accurate to 15 digits after the decimal point.

  • complex − Stores complex numbers.

Python string data type

A string is a collection of Unicode symbols. In Python, the name of a string is str. Use single or double quotes to represent strings. It is acceptable to use three quotes """ or "' to represent multiple strings. Between quotes, each character is part of the string.

The only limitation is the memory resources of the machine system, any number of characters can be used. In Python programming, deleting or updating a string will result in an error. Therefore, the Python programming language does not allow changing strings.

Python sequence data type

  • List − List is the only flexible data type available in Python. It is similar to an array in C/C in some ways. However, what’s noteworthy about lists in Python is that they can store many types of data at the same time. A list is an ordered collection of information represented by commas and square brackets ([]). (,)

  • Tuple − Lists and tuples are comparable in many ways. Tuples hold collections of elements of various data types, just like lists. The components of a tuple are separated by commas (,) and brackets (). Tuples are read-only data structures since the size and value of elements cannot be changed.

  • Range − The range() method in Python returns a list of integers contained within the specified range. It is most commonly used to iterate over a series of integers using Python loops.

Python data binary type

  • bytes − A bytes object is generated through the bytes() function. It can generate an empty bytes object of the required size, or convert the item to a bytes object. bytes() and bytearray() return different types of objects: bytes() returns an immutable object, while bytearray() returns a mutable object.

  • bytearray − The bytearray() function returns a byte array object of specified bytes. Provides a modifiable sequence of numbers from 0 to x to 256.

  • memoryview − Python programs can use memoryview objects to access the internal data of objects that implement the buffer protocol without copying. You can use the memoryview() method to directly read and write an object's byte-oriented data without copying.

Python mapping data types

  • dict − In Python, a dictionary is a collection of data items stored in an unordered manner, similar to a map. Dictionaries consist of key-value pairs, as opposed to other data types that can only contain a single value. To improve the efficiency of the dictionary, key-value pairs are included in the dictionary. A comma "separates each key", whereas each key-value pair of a dictionary data type is separated by a colon.

Python Boolean Data type

  • bool − True and False are the two predefined values ​​provided by the Boolean type. Use these values ​​to determine the truth or falsity of the provided statement. It is identified by bool class. Any non-zero integer or the letter "T" can be used to represent true, while the number "0" or the letter "F" can represent false.

Python Set data type

  • set − An unordered collection of data types is called a Python Set. It has unique, iterable, and mutable (can be changed after creation) components. The order of items in a collection is undefined; it may produce a modified sequence of elements. Use the built-in method set() to build a set, or give a comma-separated list of elements enclosed in curly braces. It can contain multiple types of values.

  • frozenset − The frozenset() method returns an immutable frozenset object whose initial elements come from the provided iterable. Frozen collections are immutable versions of Python collection objects. The elements of a collection can be changed at any time, but once a frozen collection is created, its elements cannot be changed.

in conclusion

In this section, we studied Python’s data types. In more detail, we looked at two of the data types, None and Numeric. As we have seen, numeric data comes in four different forms: integers, floating point numbers, Boolean values, and complex numbers. We have an overall understanding of the various Boolean operators and comparison operators for the Boolean type. Unlike statically typed languages ​​like C or Java, Python does not require an explicit declaration of the data type of a variable. In dynamically typed languages ​​like Python, the interpreter infers the data type of a variable based on the type of the value passed to it.

The above is the detailed content of What are the different built-in types in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template