Data types in 1.python
Python uses the object model to store data, each data type has a built-in class, and each new data is actually initialized to generate an object, that is, all the data is an object
Object Three Properties
- Identity: Memory address, can be obtained with ID ()
- Type: Determines what type of value the object can hold, what action to perform, what rules to follow, and the type () to get
- Value: Real data saved by the object
standard type |
Other types |
number |
|
string |
null |
list |
file |
|
collection |
dictionary |
Function/method |
  |
Class |
  |
module |
1.1 Numbers
Definition: a=1
Characteristics:
1. Only one value can be stored
2. Once defined, cannot be changed
3. Direct Access
Categories: Integer, Long, Boolean, floating point, plural
1.1.1 Integral type
Python's integer is equivalent to Long in C, and integers in Python can be expressed in decimal, octal, and hexadecimal notation.
>>> 1010 ---------> Default Decimal >>> Oct (ten)'012' ---------> When octal represents an integer, the value is preceded by a prefix of "0" >>> hex "0xa"---------> Hexadecimal denotes an integer, preceded by a prefix of 0X or 0x
string to int type:
>>> a = "123"
>>> A
' 123 '
>>> b = Int (a)
>>> b
123
float-to-int type:
>>> A = 1.75
>>> b = Int (a)
>>> b
1
1.1.2 BOOL Boolean
True and False
1 and 0
1.1.3 Float type float
Floating-point numbers are decimals, such as 3.14,1.23,-0.77.
But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23*109 is 1.23e9, or 12.3e8,0.000012
Can be written in 1.2e-5, and so on. Integers and floating-point numbers are stored internally in different ways, and integer operations are always accurate, and floating-point arithmetic may have
The rounding error.
1.1.4 number-related built-in functions
1.2 String
Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '
2.unicode string with R must precede r, e.g. Name=ur ' L\THF '
1.2.1 Factory functions
Capitalize () #字符串首字母大写
>>> "Hello". Casefold ()
' Hello '
Enter () #原来字符居中, not enough to complete
>>> ' Hello '. Center (20,)
' Hello '
>>> ' Hello '. Center (20, ' * ')
' *******hello******** '
Count () #从一个范围内的统计某str出现次数
>>> "Hello". Count (' l ')
2
>>> "Hello". Count (' l ', 0,6)
2
>>> "Hello". Count (' l ', 0,2)
0
>>> "Hello". Count (' L ', 0, 3)
1
EndsWith () #判断字符串是否以某字符结尾
>>> "Hello". EndsWith ("O")
True
>>> "Hello". EndsWith ("Lo")
True
>>> "Hello". EndsWith ("Hello")
True
>>> "Hello". EndsWith ("K")
False
Expandtabs () #将字符串中包含的tab转换成自定义数量的空格
>>> "He\tllo". Expandtabs ()
' He llo '
>>> "He\tllo". Expandtabs (0)
' Hello '
>>> "He\tllo". Expandtabs (4)
' He llo '
Find () #返回字符串中指定字符的下标, if there are duplicates, returns only the first found
>>> "Hello". Find ("L")
2
>>> "Hello". Find ("L", 0,6)
2
>>> "Hello". Find ("L", 0, 3)
2
>>> "Hello". Find ("K") #如果字符不在字符串中, return "1"
-1
Format () #格式化输出
>>> print (' {0},{1} '. Format ("Hel", "Lo"))
Hel,lo
>>> print (' {0}{1} '. Format ("Hel", "Lo"))
Hello
>>> print (' {} {} '. Format ("Hel", "Lo")
Hello
>>> A, B = "hel", "Lo"
>>> print (' {0}{1} '. Format (A, b))
Hello
>>> print (' {} {} '. Format (b))
Hello
Index () #返回字符的下标, if there are more than one, returns only the first
>>> "Hello". Index ("L", 0,4)
2
>>> "Hello". Index ("L", 0, 3)
2
>>> "Hello". Index ("L")
2
>>> "Hello". Index ("L", 0,2) #如果字符不在字符串中, the exception is reported
Traceback (most recent):
File "<stdin>", line 1, in <module>
Valueerror:substring not found
Isalnum () #字符串只由字母和数字组成返回true, otherwise returns false
>>> "Hello". Isalnum ()
True
>>> "hello123". Isalnum ()
True
>>> "123hello". Isalnum ()
True
>>> "Hello". Isalnum ()
False
>>> "". Isalnum ()
False
Isalpha () #都是字母才返回true
>>> "Hello". Isalpha ()
True
>>> "Hello". Isalpha ()
False
>>> "hello123". Isalpha ()
False
>>> "123". Isalpha ()
False
IsDigit (), Isdecimal (), Isnumerice () #判断字符串是否为数字
num = "1" #unicode
Num.isdigit () # True
Num.isdecimal () # True
Num.isnumeric () # True
num = B "1" # Byte
Num.isdigit () # True
Num.isdecimal () # attributeerror ' bytes ' object has no attribute ' isdecimal '
Num.isnumeric () # attributeerror ' bytes ' object has no attribute ' IsNumeric '
num = "Four" # Kanji
Num.isdigit () # False
Num.isdecimal () # False
Num.isnumeric () # True
===================
IsDigit ()
True:unicode number, byte digit (single byte), full-width digit (double byte), Roman numerals
False: Chinese numerals
Error: None
Isdecimal ()
True:unicode number, full-width digit (double-byte)
False: Roman numerals, Chinese numerals
Error:byte number (single byte)
IsNumeric ()
True:unicode numbers, full-width numerals (double-byte), Roman numerals, Chinese numerals
False: None
Error:byte number (single byte)
islower (), Isupper (), lower (), Upper () #判断大小写及转换
>>> "Hello". Isupper ()
False
>>> "HELLO". Isupper ()
True
>>> "Hello". Upper ()
' HELLO '
>>> "HELLO". Islower ()
False
>>> "HELLO". Lower ()
' Hello '
Istitle () #判断是否为标题格式
>>> "H". Istitle ()
True
>>> "Hello World". Istitle ()
True
>>> "He1". Istitle ()
True
>>> "He". Istitle ()
True
>>> "Hello World"
' Hello World '
>>> "Hello World". Istitle ()
False
>>> "Hello World". Istitle ()
False
Ljust (), Rjust () #设定宽度, left and right aligned incomplete, like center ()
>>> "Hello World". Ljust (30, "*")
' Hello world******************* '
>>> "Hello World". Ljust (30, "*")
' Hello world******************* '
>>> "Hello World". Rjust (30, "*")
' *******************hello World '
Split () #以某个字符作为条件, split string, return list
>>> "Hello World". Split ()
[' Hello ', ' world ']
>>> "Hello World". Split ("L")
[' He ', ' ', ' o wor ', ' d ']
1.3 List
Definitions: [] separated by commas, by index, holding various data types, each position represents an element
Characteristics:
1. can store multiple values
2. Can modify the value of the specified index position, variable
3. Define list elements in left-to-right order, order access from 0, ordered
1.3.1 Factory functions
Append () #添加元素到尾部
>>> a = [+ +]
>>> A.append (4)
>>> A
[1, 2, 3, 4]
Clear () #清空列表
>>> A
[1, 2, 3, 4]
>>> A.clear ()
>>> A
[]
Copy () #复制一个列表, open a new memory address
>>> A
[0, 1, 2, 3]
>>> B = A
>>> ID (a)
7413032 #列表a的内存地址
>>> ID (b)
7413032 #列表b的内存地址与a相同
>>> C = a.copy ()
>>> ID (c)
7412512 #列表c的内存地址与a, B different
>>> a[3] = 4 #修改列表a
>>> A
[0, 1, 2, 4] #
>>> b
[0, 1, 2, 4] #列表b也跟着变了
>>> C
[0, 1, 2, 3] #列表c没有变
Extend () #列表扩展, merging two lists
>>> A
[1, 2, 3, 1]
>>> b
[1, 2, 3, 1]
>>> A
[1, 2, 3, 1]
>>> A.extend (b)
>>> A
[1, 2, 3, 1, 1, 2, 3, 1]
Insert () #往指定位置前插入元素
>>> A
[1, 2, 3, 1, 1, 2, 2, 3, 1]
>>> A.insert (2, "Hello")
>>> A
[1, 2, ' Hello ', 3, 1, 1, 2, 2, 3, 1]
Pop () #删除元素并打印该元素
>>> A
[1, 2, ' Hello ', 3, 1, 1, 2, 2, 3]
>>> A.pop (2)
' Hello '
>>> A
[1, 2, 3, 1, 1, 2, 2, 3]
Remove () #删除元素, parameter is element name, not subscript
>>> A
[1, 2, 3, 1, 1, 2, 2, 3]
>>> A.remove (1)
>>> A
[2, 3, 1, 1, 2, 2, 3]
>>> A.remove (3)
>>> A
[2, 1, 1, 2, 2, 3]
Reverse () #反转列表中元素顺序
>>> A
[0, 1, 2, 3, 4]
>>> A.reverse ()
>>> A
[4, 3, 2, 1, 0]
Sort () #排序
>>> A
[0, 3, 5, 1, 4, 7]
>>> A.sort ()
>>> A
[0, 1, 3, 4, 5, 7]
>>> a #python3中不可以对int和str混合类型的列表排序
[0, 1, 3, 4, 5, 7, ' hello ']
>>> A.sort ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:unorderable types:str () < int ()
2. Operator 2.1 arithmetic operations
2.2 Comparison operations
2.3 Assignment Operations
2.4-bit arithmetic
2.5 Logical operations
2.6 Member operations
2.7 Identity Operations
2.8 Operator Precedence: top-down, high-to-low priority
Python 3 Learning Notes (ii)