Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of five Standard data types of python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "introduction of python five standard data types". In daily operation, I believe many people have doubts about the introduction of python five standard data types. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "introduction of python five standard data types"! Next, please follow the small series to learn together!

Python's five standard data types

Python has five standard data types:

Numbers (numbers)

String (String)

List

Tuple

Dictionary

Among them, the data types belonging to set type are list, tuple and dictionary.

0x00. Numbers (Numbers)

Numeric data types are used to store numeric values.

They are immutable data types, meaning that changing the numeric data type assigns a new object.

When you specify a value, the Number object is created:

12var1 = 1var2 = 2

The del statement deletes references to objects. Its syntax is:

1del var1[,var2[,var3[...., varN]]]]

You can delete references to single or multiple objects by using the del statement. For example:

12del var1del var1, var2

There are four different types of numbers:

int (signed integer)

long (long [can also represent octal and hexadecimal])

float

complex (plural)

a. int (integer)

On a 32-bit machine, the integer has 32 bits, and the value range is-2**31~2**31-1, that is,-2147483648~2147483647.

On a 64-bit system, the number of digits of an integer is 64 bits, and the value range is-2**63~2**63-1, i.e.-9223372036854775808~9223372036854775807.

b. long (long integer)

Unlike C, Python long integers do not specify bit widths, i.e. Python does not limit the size of long integer values, but in fact long integer values cannot be infinite due to limited machine memory.

Note that since Python 2.2, Python automatically converts integer data to long integers if an integer overflow occurs, so not adding the letter L after long integer data doesn't have serious consequences these days.

c. float

Floating-point numbers are used to deal with real numbers, i.e. numbers with decimals. It is similar to the double type in C, which takes up 8 bytes (64 bits), of which 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one bit represents the symbol.

d. complex (plural)

A complex number consists of a real part and an imaginary part, of the general form x+yj, where x is the real part of the complex number and y is the imaginary part of the complex number, where x and y are both real.

Note: Python has a small number pool: -5 ~257

Small integer objects--Pool of small integer objects

In actual programming, small integers, such as 1, 2, 29, etc., may occur very frequently. In Python, all objects exist on the system heap. Think about it? If a small integer occurs too often, Python will have a lot of malloc/free operations, which will greatly reduce the efficiency of the operation, and will cause a lot of memory fragmentation, seriously affecting Python's overall performance.

In Python 2.5 and even 3.3, small integers between [-5, 257) are cached in a pool of small integer objects.

0x01. String

A string is a string of characters consisting of numbers, letters, and underscores.

It is a data type that represents text in a programming language.

Python's list of strings has two value orders:

Index from left to right starts at 0 by default, maximum range is string length less than 1

Index from right to left Default-1 start, maximum range is string start

If you want to get a substring from a string, you can use the variable [head subscript: tail subscript] to intercept the corresponding string, where the subscript starts from 0 and can be positive or negative, and the subscript can be empty to indicate the head or tail.

For example:

1s = 'i love python'

The result of s[2:6] is love. (either left or right)

Example of operation:

str = 'Hello World' print(str) #Output the complete string print(str[0]) #Output the first character in the string print(str[2:5]) #Output the characters from the third to the fifth in the string print(str[2:]) #Output the string from the third to the last print(str*2) #Output the string twice print ('say:'+ str) #Output the concatenated string 0x02. List

List is the most frequently used data type in Python.

Lists complete most of the data structure implementations of collection classes. It supports characters, numbers, strings and even lists (so-called nesting).

Example of operation:

list = ['apple',' jack', 798, 2.22, 36]otherlist = [123, 'xiaohong'] print(list) #Output the complete list print(list[0]) #Output the first element of the list print(list[1:3]) #Output the second to third elements of the list print(otherlist * 2) #Output the list twice print(list + otherlist) #Output the concatenated list 0x03. Tuple

Tuples are another data type, similar to List.

Tuples are identified with "()." Internal elements are separated by commas. However, tuples cannot be assigned twice and are equivalent to read-only lists.

Examples of operations are similar to lists

0x04. Dictionary (Dictionary)

Dictionaries are Python's most flexible built-in data structure type, other than lists. A list is an ordered association of objects, a dictionary is an unordered collection of objects.

The difference between the two is that dictionary elements are accessed by keys, not offsets.

Dictionaries are identified with "{ }." A dictionary consists of a key and its corresponding value.

Example of operation:

dict = {}dict ('one ')= 'This is one'dict[2] = 'This is two'tinydict = {' name':'john',' code': 5762,'dept':' sales'} print(dict ('one ') #Output the value of key'one'print(dict[2]) #Output the value of key 2 print(tinydict) #Output the complete dictionary print(tinydict.keys()) #Output all keys print(tinydict.values()) #Output all values This is the end of the study on "Introduction to Python's five standard data types", hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report