Home  >  Article  >  Backend Development  >  Reminiscing about Python basic data types

Reminiscing about Python basic data types

coldplay.xixi
coldplay.xixiforward
2021-01-26 09:56:452298browse

Reminiscing about Python basic data types

Free learning recommendation: python video tutorial

data Type is an essential attribute of every programming language. Only by assigning a clear data type to the data can the computer process the data. Therefore, it is very necessary to use the data type correctly. Different languages ​​have similar data types, but the specific representation methods Different, the following are commonly used data types in Python programming:

Commonly used data types:

  1. Integer type int;
  2. Float Point type float;
  3. Boolean type bool;
  4. String type str;

Integer type

  • English is integer, abbreviated as int, which can represent positive numbers, negative numbers and zero
  • Different base representation methods of integers:
    (1) Decimal system-> Default base system
    (2) Binary-> Starts with 0b
    (3) Octal-> Starts with 0o
    (4) Hexadecimal-> Starts with 0x

The code is as follows (example):

print('十进制',118)print('二进制',0b10101111)print('八进制',0o176)print('十六进制',0x1EA3)

Floating point number type

  • Floating point number consists of integer part and decimal part
  • Inaccuracy of floating-point number storage
  • When using floating-point numbers for calculations, the number of decimal places may be uncertain

The code is as follows (example):

n1 = 1.1n2 = 2.2print(n1+n2) 输出的结果是3.3000000000000003解决方案:
	导入模块decimal
from decimal import Decimalprint(Decimal('1.1') + Decimal('2.2')) 输出的结果是3.3

Boolean type

  • The value used to represent true or false
  • True represents true, False represents falseThe first two letters must be capitalized
  • Boolean values ​​can be converted to integers
    • True -> 1
    • False -> 0

The code is as follows (example):

f1 = True
f2 = Falseprint(f1+1) 输出的结果是2print(f2+1) 输出的结果是0

String type

  • The string is again A sequence of characters called immutable
  • Can be defined using single quotes ''double quotes"" or """"""
  • Single quote and double quote strings must be on one line
  • The string defined by triple quotes can be distributed on multiple consecutive lines

The code is as follows (example):

str1 = '我还年轻,吃苦趁现在'str2 = "我还年轻,吃苦趁现在"str3 = """我还年轻,吃苦趁现在"""

Related free learning recommendations: python tutorial(Video)

The above is the detailed content of Reminiscing about Python basic data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete