Python two articles computer knowledge (data type)
Basic data types
Digital Type
Python language provides three numeric types: type integer, floating-point type and complex type, respectively mathematics integer, real and complex.
- Integer type
Ary species | Pilot symbols | description |
---|---|---|
Decimal | no | By default, for example: 1010, -1010 |
Binary | 0b or 0B of characters 0 and 1, | Example: 0b1010, 0B1010 |
Octal | 0o or 0O composed by the characters 0 to 7, | Example: 0o1010, 0O1010 |
Hex | 0x or 0X by the characters 0 to 9, a to f or compositions A to F, | Example: 0x1010 |
- Floating point numbers
represented in decimal form and generally represent scientific notation: There are two floating-point representation. In addition to decimal, binary floating-point number that no other representation
-1.3e3 -2.5E5 - Complex types
- Python language, can be seen as a plurality of binary ordered pair of real numbers (A, b), expressed as: a + bj, where, a is a real part, referred to as a real part, b is the imaginary part, referred to as the imaginary part. Imaginary part "J" or "j" is represented by suffix. Note that when b is 1, 1 can be omitted, i.e. 1j represents a complex, and the variable j indicates a Python program.
- Type complex real and imaginary part is a floating point, for a plurality of Z, it is possible to obtain a real part and an imaginary part, respectively, with z.real and z.imag
Python provides nine basic value operator that
Operators | description |
---|---|
x + y | and the sum of x and y |
x - and | The difference between x and y |
x * y | The product of x and y |
x / y | x and y quotient |
x // and | Integer quotient of x and y, namely: List of x and y is no greater than the maximum integer |
x% and | I quotient of x and y, also known as modular arithmetic |
-x | x is negative, that: x * (- 1) |
+x | x itself |
x ** and | x to the power y, i.e.: xy |
Numerical calculation result may change the data type, and the types of changes related to the operator, the following basic rules:
- Mixed integer and floating point arithmetic, floating-point result is output;
- Operation between integers, the result type associated with the generation operator, / is the result of floating point operation;
- And a plurality of integer or floating point operation, the output plural.
Built-in functions
function | description |
---|---|
abs(x) | Absolute value of x |
divmod(x, y) | (X // y, x% y), the output in the form of a binary group (also referred to as a tuple type) |
pow(x, y[, z]) | (X ** y)% z, [..] indicates that the parameter can be omitted, namely: pow (x, y), which is the same x ** y |
ound(x[, ndigits]) | X rounded to retain ndigits decimal places. round (x) returns the integer values rounded |
max(x1, x2, …, xn) | x1, x2, ..., xn to a maximum value, n is not limited |
min(x1, x2, …, xn) | x1, x2, ..., xn of the minimum value, n is not limited |
String type
Python language escape character:
- \ N newline, \ represents a backslash 'represents a single quotation mark "represents double quotes, \ T for a tab (TAB) and the like.
String operator
- Providing several basic operators for string, Python language
Operators | description |
---|---|
x + y | Connecting the two strings x and y |
x * n or n * x | Copy the string x n times |
x in s | If x is a substring of s, returns True, otherwise False |
>>>name = "Python语言" + "程序设计"
>>>name
'Python语言程序设计'
>>>"等级考试!" * 3
'等级考试!等级考试!等级考试!'
>>>"语言" in name
True
>>>'Y' in name
False
String handling functions
Python language provides a number of built-in functions for string handling
function | description |
---|---|
len (x) | Returns the length of the string x, and returns the number of elements may also be other combinations of data types |
str(x) | Returns a string corresponding to any type of x |
chr(x) | Returns Unicode encoding single character corresponding to x |
words (x) | Unicode character encoding single return x represented |
hex(x) | Returns the integer x corresponding to the lowercase hexadecimal string |
oct(x) | Returns the integer x corresponding to the octal string lowercase |
am (x) | Returns the integer x corresponding to the binary string lowercase |
>>>len("全国计算机等级考试Python语言科目")
19
>>>chr(10000)
'✐‘
>>>hex(1010)
'0x3f2'
String processing method
method | description |
---|---|
str.lower() | Returns the string str copy all lowercase characters |
str.upper() | Returns a copy of the string str, all uppercase characters |
str.split(sep=None) | It returns a list, according to a sep divided portions constituting str |
str.count(sub) | Returns the number of sub substring appears |
str.replace(old, new) | Returns a copy of string str, all old substring is replaced with a new |
str.center(width, fillchar) | 字符串居中函数, fillchar参数可选 |
str.strip(chars) | 从字符串str中去掉在其左侧和右侧chars中列出的字符 |
str.join(iter) | 将iter变量的每一个元素后增加一个str字符串 |
# split()
>>>"Python is an excellent language.".split()
['Python', 'is', 'an', 'excellent', 'language.']
>>>"Python is an excellent language.".split('a')
['Python is ', 'n excellent l', 'ngu', 'ge.']
>>>"Python is an excellent language.".split('an')
['Python is ', ' excellent l', 'guage.']
# count()
>>>"Python is an excellent language.".count('a')
3
# replace()
>>>"Python is an excellent language.".replace('a', '#')
'Python is #n excellent l#ngu#ge.'
>>>"Python is an excellent language.".replace('Python', 'C')
'C is an excellent language.'
# center()
>>>"Python".center(20, "=")
'=======Python======='
>>>"Python".center(2, "=")
'Python'
# str.strip(chars)
# 从字符串str中去掉在其左侧和右侧chars中列出的字符。 chars是一个字符串,其中出现的每个字符都会被去掉。
>>>" ==Python== ".strip(' ')
'==Python=='
>>>" ==Python== ".strip(' =')
'Python'
>>>" ==Python== ".strip(' =n')
'Pytho'
# str.join(iter)中iter是一个具备迭代性质的变量, 该方法将str字符串插入iter变量的元素之间, 形成新的字符串
>>>" ".join('PYTHON')
'P Y T H O N'
>>>",".join('12345')
'1,2,3,4,5'
>>>",".join(['1', '2', '3', '4', '5'])
'1,2,3,4,5'
format()方法的使用
- 字符串format()方法的基本使用格式是:
<模板字符串> .format( <逗号分隔的参数> )
>>>"{}曰: 学而时习之, 不亦{}。 ".format("孔子","说乎")
'孔子曰: 学而时习之, 不亦说乎。 '
>>>"{1}曰: 学而时习之, 不亦{0}。 ".format("说乎","孔子")
'孔子曰: 学而时习之, 不亦说乎。 '
- format()方法的格式控制
format()方法中模板字符串的槽除了包括参数序号, 还可以包括格式控制信息。
{ <参数序号> : <格式控制标记> }
其中, 格式控制标记用来控制参数显示时的格式 。 格式
控制标记包括: <填充> <对齐> <宽度> ******<.精度> <类型> 6个字段,这些字段都是可选的,可以组合使用
>>>s = "等级考试"
>>>"{:25}".format(s) #左对齐, 默认
'等级考试 '
>>>"{:^25}".format(s) #居中对齐
' 等级考试 '
>>>"{:>25}".format(s) #右对齐
' 等级考试'
>>>"{:*^25}".format(s) #居中对齐且填充*号
'**********等级考试***********'
>>>"{:+^25}".format(s) #居中对齐且填充+号
'++++++++++等级考试+++++++++++'
>>>"{:十^25}".format(s) #居中对齐且填充汉字“十”
'十十十十十十十十十十等级考试十十十十十十十十十十十'
>>>"{:^1}".format(s) #z指定宽度为1, 不足变量s的宽度
'等级考试
<.精度>由小数点(.) 开头。 对于浮点数, 精度表示小数部分输出的有效位数。对于字符串, 精度表示输出的最大长度。 小数点可以理解为对数值的有效截断
>>>"{:.2f}".format(12345.67890)
'12345.68'
>>>"{:>25.3f}".format(12345.67890)
' 12345.679'
>>>"{:.5}".format("全国计算机等级考试")
'全国计算机'
<类型> 表示输出整数和浮点数类型的格式规则。
对于整数类型, 输出格式包括6种:
• b: 输出整数的二进制方式;
• c: 输出整数对应的Unicode字符;
• d: 输出整数的十进制方式;
• o: 输出整数的八进制方式;
• x: 输出整数的小写十六进制方式;
• X: 输出整数的大写十六进制方式;
>>>"{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425)
'110101001,Ʃ,425,651,1a9,1A9'
对于浮点数类型, 输出格式包括4种:
• e: 输出浮点数对应的小写字母e的指数形式;
• E: 输出浮点数对应的大写字母E的指数形式;
• f: 输出浮点数的标准浮点形式;
• %: 输出浮点数的百分形式。
>>>"{0:e},{0:E},{0:f},{0:%}".format(3.14)
'3.140000e+00,3.140000E+00,3.140000,314.000000%'
>>>"{0:.2e},{0:.2E},{0:.2f},{0:.2%}".format(3.14) # 对比输出
'3.14e+00,3.14E+00,3.14,314.00%'
数据类型转换
- Python语言提供type(x)函数对变量x进行类型判断, 适用于任何数据类型
n = eval(input('请输入一个数字:'))
if type(n) == type(123):
print("输入的数字是整数。 ")
elif type(n) == type(11.3):
print("输入的数字是浮点数。 ")
else:
print("无法判断输入类型。 ")
- 数值运算操作符可以隐式地转换输出结果的数字类型, 例如, 两个整数采用运算符“/”的除法将可能输出浮点数结果。
- 此外, 通过内置的数字类型转换函数可以显式在数字类型之间进行转换
函数 | 描述 |
---|---|
int(x) | 将x转换为整数, x可以是浮点数或字符串 |
float(x) | 将x转换为浮点数, x可以是整数或字符串 |
str(x) | 将x转换为字符串, x可以是整数或浮点数 |