On common built-in objects in Python

Time:2021-11-25

1、 Common Python built-in objects

object type Type name Example Brief description
number int,float,complex 1234,3.14,3+4j There is no limit on the size of numbers, and the built-in support of complex numbers and their operations
character string str ‘swfu” Use single quotation marks, double quotation marks and three quotation marks as delimiters to represent the original string with R / R guidance
Byte string bytes b”hello world” Guided by the letter B, you can use single quotation marks, double quotation marks and three quotation marks as delimiters
list list [1,2,3] All elements are placed in []. The directions of elements are separated by “,” and the elements can be of any type
Dictionaries dict {1:‘food”,2:‘taste”} All elements are placed in {}, separated by “,” and in the form of “key: value”
tuple tuple (2,-5,6) All elements are placed in (), and the elements are separated by “,”. If there is only one element in the tuple, the following comma cannot be omitted
aggregate set、frozenset {‘a”,‘b”,‘c”} All elements are placed in a pair of {}, separated by “,” and elements are not allowed to be repeated; In addition, set is variable and frozenset is immutable
Boolean bool True,False The value of the expression composed of logical value, relational operator, member test operator and identity test operator is generally true or false
Empty type NoneType None Null value
abnormal Exception、ValueError、TypeError   Python has a large number of built-in exception classes, which correspond to different types of exceptions
file   f=open(‘data.dat”,‘rb”) Open is a built-in function of Python. It uses the specified mode to open the file and return the file object

2、 Definition and use of variables

Name = "orange cat is not fat"

On common built-in objects in Python

After multiple assignments, the variable name points to the new space

Name = "orange cat is not fat"

On common built-in objects in Python

3、 Integer

Print ("decimal", 118) # default decimal

On common built-in objects in Python

4、 Floating point number

When using floating-point numbers for calculation, the number of decimal places may be uncertain
Solution: import the module decimal

n1=1.1
n2=2.2
print(n1+n2)
from decimal import Decimal
print(Decimal("1.1")+Decimal("2.2"))

On common built-in objects in Python

5、 Boolean type

Used to represent true or false values. True is 1 and false is 0

f1=True
f2=False
print(f1,type(f1))
print(f2,type(f2))
print(f1+1)
print(f2+1)

On common built-in objects in Python

6、 String type

STR1 = "orange cat is not fat"

7、 Notes

#Single line note

This is the end of this article about common built-in objects in Python. For more information about Python built-in objects, please search for previous articles on developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!