1. pip:给python安装各种程序包。在cmd下输入pip获取相关信息。如果没有则查看是否配置环境变量。
- 安装包:pip install selenium
- 安装指定版本包:pip install selenium==XXX #XXX为包的对应版本号
- 卸载包:pip uninstall selenium
- 包列表:pip list
- 查询某包信息:pip show selenium
- 升级包:pip install --upgrade nose
- 升级pip:pip install -U pip
2.isinstance的用法
>>> print (a)
1
>>> a="hello"
>>> type(a)
<class 'str'>
>>> a=1
>>> b="hello"
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> b=input(u'输入:')
输入:2
>>> type(b)
<class 'str'>
>>> print (a+int(b))
3
>>> a=1.1
>>> isinstance(a,float)
True
>>> d=1+3j
>>> type(d)
<class 'complex'>
>>> e=[1,2]
>>> isinstance(e,list)
True
>>> type(isinstance(e,list))
<class 'bool'>
>>> a=true
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> a=1<3
>>> type(a)
<class 'bool'>
>>> print(a)
True
3.地板除
- /:除
- //:保留整数位(地板除)
- int():也可以用来取整
- round(a,b):内置函数,用于截取数。a:待操作数;b:保留小数位数。取的最后一位大于5时进位。
>>> 5/3
1.6666666666666667
>>> 5//3
1
>>> int(5/3)
1
>>> round(0.9,0)
1.0
>>> round(1/3,5)
0.33333
>>> round(0.5,0) #并非四舍五入
0.0
>>> round(2.5,0) #如果左右相同,取离自己近的偶数值
2.0
>>> round(1.5,0)
2.0
>>> round(3.5,0)
4.0
>>> round(4.5,0)
4.0
>>> round(3.51,0)
4.0
>>> round(4.51,0)
5.0
- pow()
>>> help(pow)
Help on built-in function pow in module builtins:
pow(x, y, z=None, /)
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
#x的y次方,结果除以z后,取余数
>>> pow(5,2,5)
0
>>> pow(5,2,10)
5
4.math包
pow和math.pow区别:pow三个参数,math.pow两个参数,只有乘方功能。
>>> import math
>>> math.pow(3,2)
9.0
>>> math.sqrt(8)
2.8284271247461903
>>> math.sqrt(4)
2.0
>>> math.pi
3.141592653589793
>>> help(math.pow)
Help on built-in function pow in module math:
pow(x, y, /)
Return x**y (x to the power of y).
5.ord()和chr()
>>> ord('e')
101
>>> ord('a')
97
>>> ord('A')
65
>>> chr(65)
'A'
6.取余:
- %:直接取余
- dicmod():输出结果为除数整数位,余数
>>> 5%3
2
>>> 5%2
1
>>> divmod(5,2)
(2, 1)
>>> 5/2
2.5
7.ASCII码:
>>> 'a'>'A'
True
#比较时按照ASCII码比较
>>> ord('3')
51
>>> ord(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected string of length 1, but int found
8.print ()
>>> print ('hello world',end='')
hello world>>> hh
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hh' is not defined
>>> print ('hello world',end='XXX')
hello worldXXX>>> print ('hello world',end=' XXX')
hello world XXX>>>
9.input():输入,内容为字符串
>>> age=input('年龄')
年龄5
>>> type(age)
<class 'str'>
>>> age=int(age)
>>> type(age)
<class 'int'>
10.if
#encoding=utf-8
resule=input('输入成绩:')
if int(resule)>=60:
print('及格啦!')
else:
print ("不及格!")
>>> if False:print(2)
... else:print(1)
...
1
>>> type(true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> type(True)
<class 'bool'>
# encoding=utf-8
#输入一个字符串,大于3打印大于3;小于3,打印小于3;if a>0;等于3,打印等于3
a=input('输入字符串:')
if len(a)>3:
print ('大于3')
elif len(a)==3:
print('等于3')
else:
print('小于3')
11.bool() 判断真假
>>> bool([])
False
>>> bool('')
False
>>> bool(())
False
>>> bool({})
False
>>> bool(0)
False
#空,都为假
>>> bool(-1)
True
12.and 和 or,取反 not
注:都是顺序执行,and有短路效应
>>> a=3
>>> a>6 and a<11
False
>>> a>6 or a<11
True
>>> 1 and 1
1
>>> 1 and 0
0
>>> 0 and 0
0
>>> 0 and 1
0
>>> not []
True
>>> not ([])
True
>>> not 1
False
#复杂内容加()决定优先级
#encoding=utf-8
#判断是否能被2和5整除
result=input('输入数:')
if int(result)%2==0 and int(result)%5==0:
print('可以被2和5整除')
else:
print ("不可以被2和5整除!")
#encoding=utf-8
#如果能被3或5整除,打印ok
result=input('输入数:')
if int(result)%3==0 or int(result)%5==0:
print('ok')
else:
print ("不能被3或5整除!")