Numerical
1, declare variables
age = 20
f = 3.14 #浮点型
f = 0.3
f = .3
2, expression
- Mainly to do some simple arithmetic operations, direct results
1.1 + 2.2
2.2 + 3.0
3.14 * 2
5.0 / 2
3.3000000000000003
5.2
6.28
2.5
In the example above, mainly to see the simple '1.1 + 2.2' output in python is not as simple as we thought in 3.3, but there is a long decimals '3.3000000000000003'.
3, the display (using .format () to replace characters)
- I: '{0: .2f}'. format (3.3333)
'{0}'.format(20)
'3 + 2 ={0}'.format(5)
f = 3.333
'f = {0}'.format(f)
'f={0:.2f}'.format(f)
d = {'name':'Tom', 'age':20, 'job':'dev'}
'姓名:{0},年龄:{1}'.format(d.get('name'), d.get('age'))
The above code is output as follows:
'20'
'3 + 2 =5'
'f = 3.333'
'f=3.33'
'姓名:Tom,年龄:20'
In the example above we see .format () of power of, dynamically replacing the existing string of characters, and we can also added in {0} ': .2f' need to be added to a predetermined value data types and retention of decimal places type object.
4, comparison, returns a Boolean result: True, False
- Common operators>, <,> =, <=, ==,! =
5, divided
10 / 4
10 // 4
10 // 4.0
Output is as follows:
2.5
2
2.0
The first one is above normal peacetime use of the division, behind a common "floor except", also known as divisible, is derived quotient (without remainder).
The third addition result of the formula to obtain the float.
6, three methods of rounding
- floor()、trunc()、round()
- floor () rounding
import math math.floor(3.4) # 取整的函数,直接截取整数 math.floor(3.9) math.floor(-3.4) math.floor(-3.9) # 假如把数值放在坐标轴上,那么floor是取数值左边的整数,往左移动坐标轴
3 3 -4 -4
- the trunc () rounding
math.trunc(3.14) # tuanc函数是往坐标轴0中间移动 math.trunc(3.94) math.trunc(-3.14) math.trunc(-3.94)
3 3 -3 -3
- round () rounding
round(3.14) #常规意义上的四舍五入 round(3.94) round(-3.14)
3 4 -3 -4
7, integer: infinite precision, limited only by memory and computer precision
- Hex 0o, octal, 0x, binary 0b
- Octal 0o
0o3 # 0o开头表示八进制,但是八进制只能表示0-7 0o7 0o8 # 报错
3 7
- Hex 0x
0x3 #十六进制 0x9 0xa 0xf
3 9 10 15
- Binary 0b
0b1 # 二进制 0b0 0b11 0b1011
1 0 3 11
- Several functions convert hexadecimal
oct(64) # 转换64进制的写法
hex(64) # 十六进制的写法
bin(64) # 二进制的写法
'0o100'
'0x40'
'0b1000000'
- Other trivial
math.pi # math里内置的一些函数
math.sqrt(144)
3.141592653589793
12.0
- About decimal
import decimal
decimal.Decimal('3.14')
decimal.Decimal('1.1')+decimal.Decimal('2.2') # 得到需要的精度
Decimal('3.14')
Decimal('3.3')
String
1, the statement
- ''
- ""
- "" "##" "" explanatory document
to deal with the problem quoted strings can reference methods
word = "What's your name?"
word
'what\'s your name?'
"What's your name?"
"what's your name?"
2, escapes
- Escapes
\
\'
\"
- Newline \ n
- Backspace \ b
- Form four spaces \ t
- Ignore escapes r '...'
directly indicate the path will be given:
path = 'C:\abc\xyz.txt'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 6-7: truncated \xXX escape
You can use the following methods to transfer ""
path = 'C:\\abc\\xyz,txt'
path
'C:\\abc\\xyz,txt'
The best escape character is ignored:
path = r'c:\abc\xyz.txt'
path
'c:\\abc\\xyz.txt'
You can use "" "##" "" comment in the code:
def avg():
"""计算平均分"""
pass
# 这是一行注释
3, the basic operation
name = 'tom'
len(name)
'abc' + 'xyz'
'ok' * 5
print('-' * 30)
3
'abcxyz'
'okokokokok'
------------------------------
- If I wanted to literally print it, it can be:
s = 'hello'
for c in s:
print(c)
h
e
l
l
o
- Want to add something literally in the middle? :
for c in s:
print(c, end = ' ')
h e l l o
- I want to call a character in a field:
s[0]
s = 'sfdsfsfsdf'
s[0:4] # 后面的数字坐标是不包括的,不会引用出来
'h'
'sfds'
- Call two methods last field
s[-1]
s[len(s)-1]
'f'
'f'
- Fancy call:
s[:]
s[::2] # 以2作为间隔获取
h = 'hello'
h[::-1]
int('42')+1 # 使得字符串与数字相加
'sfdsfsfsdf'
'sdffd'
'olleh'
43
- About ASCII table
ord('c') # 查看ascii表里位置
chr(99) #查看相应位置对应的字符
chr(75)
99
'c'
'K'
- Use replace () a field replacement string
h = 'hello'
h = h.replace('e', 'a')
h
'hallo'
4, Boolean
- True、False
type(True) # 区分大小写
type(3)
isinstance(True, int) # 这个函数是判断某个字符是否是某个类型
True == 1
False == 0
result = True + 3
result
bool
int
True
True
True
4
5, the basic operation of Sao
- As an intermediary in the string is changed by one field list
- Becomes first string list
s = 'sdhishd.com' l = list(s) l
['s', 'd', 'h', 'i', 's', 'h', 'd', '.', 'c', 'o', 'm']
- Replace fields in the list
l[len(l)-1] = 'c' # 或者可以用 l[-1] = 'c' l
['s', 'd', 'h', 'i', 's', 'h', 'd', '.', 'c', 'o', 'c']
- Then into a string
s = ''.join(l) #通过一个空的字符串 s
'sdhishd.coc'
s = '|'.join(l) #将字符串中的每一个字段都用符号隔开 s url = 'sadfhsk.com,sdfjl.cc,sfhioho.com' # 使用逗号隔开字符串中的字段,前提是知道字符串中存在的分隔符 l = url.split(',') l
's|d|h|i|s|h|d|.|c|o|c' ['sadfhsk.com', 'sdfjl.cc', 'sfhioho.com']
- len (l) # This is actually a function, not attached to the object
- s.split () # This is a method, when the python is defined string type is set a good method.
url = 'sdfi.cc'
url.startswith('url') # 判断字符串的开头方法
url.endswith('cc') # 判断字符串结尾的方法
url.find('sdf') # 返回字段在字符串中的位置,返回寻找字段首字符位置
url.find('.')
False
True
True
0
4
- About Assignment
a,b = 1,2
a
b
1
2
a,b = b,a #交换对象
a
b
2
1
- 交换过程的展示
'{0} = > {1}'.format(a, b) #交换的过程表示
'{} = > {}'.format(a, b) #也可以不写索引
'{name}=>{salary}'.format(salary = 9000.00,name = 'Tom' )
#可以不按顺序,但是要指定变量与对象
url.upper() #大写变量
'2 = > 1'
'2 = > 1'
'Tom=>9000.0'
'SDFI.CC'
List
- Ordered set of arbitrary objects
- Access the elements by index index
- Variable length
- Belonging to the variable sequence
- Common operations:
len([3, 4, 5]) #求列表长度的时候,会计算列表中的对象数量
[1,2,3]+[3,4,5]
l = list('dfasihdi') #这里的list是个函数,要用小括号
l
'k' in l
3
[1, 2, 3, 3, 4, 5]
['d', 'f', 'a', 's', 'i', 'h', 'd', 'i']
False
for c in l:
print(c)
for c in l:
print(c,end = '|')
d
f
a
s
i
h
d
i
d|f|a|s|i|h|d|i|
- append()
l = [1,2,3,4,5,6]
res = []
for i in l:
res.append(i**2) #append方法就是扩展
res
l1 = [i**2 for i in l] # 外侧的中括号决定返回的是一个列表
l1
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36]
[c*3 for c in 'code']
l = [1,2,3]
l[1:2] = [4,5] #替换列表
l
['ccc', 'ooo', 'ddd', 'eee']
[1, 4, 5, 3]
- extend()
l.extend([9,8,5]) # 扩展一个列表,连接起来
l
[1, 4, 5, 3, 7, 9, 8, 5]
- Sequence
l.sort() #正向排序
l
l.reverse() #反向排序
l
[1, 3, 4, 5]
[5, 4, 3, 1]
- Removing elements
l.pop() #删除最后一个元素
l
del(l[0])
l
[5, 4, 3]
[4,3]
l.index(5) #返回列表中元素的索引
l.count(5) #统计列表中元素5的个数
2
1
- Note: reference types, support for in-situ change. Copies can be used: [:] ,. copy ()
l1 = [1,2,3,4,5]
l2 = l1
l2
l1[1] = 9 #改变l1中的元素,然后l2中相应的元素也会改变的
l1
l2
[1, 2, 3, 4, 5]
[1, 9, 3, 4, 5]
[1, 9, 3, 4, 5] #l2也跟着改变了
Avoid changing the method of
l3 = l1[:] #第一种避免l3改变的方法
l3
l4 = l1.copy() #使用.copy()方法
l4
[1, 9, 3, 4, 5]
[1, 9, 3, 4, 5]