1.进入python环境:
python 2:py -2
python 3:py -3
2.退出python环境 exit()/quit()/ctrl+z+enter ctrl+z+enter没有尝试成功
3.pip命令:(要在非pyhton环境下执行)
升级命令:
py -3 -m pip install --upgarde pip /py -3 -m pip install -u pip(pip为包名)
安装命令:
py -3 -m pip show nose#非python环境下命令
import nose#python环境下命令
4.基本的数据类型
>>> a = 1 #整型
>>> a
1
>>> type(a)
<class 'int'>
>>> type(1.2)#浮点型
<class 'float'>
>>> type("acb")#字符串
<class 'str'>
>>> type(True)# 布尔型 True和False在python里首字母必须大写 否则会报错
<class 'bool'>
>>> type(Flase)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Flase' is not defined
>>> type(False)
<class 'bool'>
>>> type([1,2,3])#列表
<class 'list'>
>>> type((1,2))#元祖
<class 'tuple'>
>>> type({1,2})
<class 'set'>
>>> type((1,2,3))
<class 'tuple'>
>>> type({'a':1,'b':3})#字典
<class 'dict'>
>>> s ={1,2}#集合
>>> type(s)
<class 'set'>
>>>
>>> dir([])/>>> dir(__builtins__)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
Help on built-in function round in module builtins:
round(number[, ndigits]) -> number
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
布尔类型的假:[]/()/{}/空字符串/False/0/none
5.运算:+,-,*,/,%,//,**
>>> 1+1
2
>>> 1-1
0
>>> 1*2
2
>>>3/2#真除
1
>>> 8//5#整取(向下取整),必反除
1
>>> 2 ** 4#幂运算
16
>>> pow(2,4)#幂运算
16
>>> import math#函数向上/下取整 先导入python自带的math
>>> math.floor(2.555)#向下取整
2
>>> math.ceil(1.2)#向上取整
2
round取整的特殊用法:
>>> round(0.5)
0
>>> round(1.5)
2
>>> round(2.5)
2
>>> round(3.5)
4
Help on built-in function round in module builtins:
round(number[, ndigits]) -> number
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
(3, 1)
divmod() 函数
说明:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
用法:divmod(a,b)
7//2#结果为商
7%2#结果为余数
>>> 7//2#结果为商 3 >>> 7%2#结果为余数 1
>>> import math
>>> math.pow(2,3)#次方
8.0
>>> math.sqrt(4)#开平方
2.0
>>> math.sqrt(8)
2.8284271247461903
>>> math.pi#π
3.141592653589793
>>>
'A'
>>> chr(97)
'a'
>>> ord('a')
97
>>>
... print(chr(i))
...
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
>>> for i in range(65,91):
... print(chr(i),end=' ')# end抑制换行
...
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z >>>
divmod(a, b)
参数说明:
- a: 数字
- b: 数字