from math import *
print(pi)
print(e)
3.141592653589793
2.718281828459045
print(id(11))
print(id('python'))
print (type(12))
print(type(1.2))
140733182862288
2498184443808
<class ‘int’>
<class ‘float’
a=112
print(id(a))
print (id(112))
b=112
print(id(b))
140733182865520
140733182865520
140733182865520
python变量的特色:
1 不需要声明
2 可以随时赋不同的值
3可以使用一个赋值符号给多个变量赋值
x,t,z=1,2,3
print(x)
print (t)
print (z)
1
2
3
a=5
print (a.bit_length())
b=8
print(b.bit_length())
3
4
from decimal import Decimal#高精度数
a=Decimal(1)/Decimal(3)
print(a)
0.3333333333333333333333333333
from fractions import Fraction#分数
print (Fraction(1,2))
print (Fraction(1.25))
print (Fraction(12,15))
a=Fraction(2,3)
b=Fraction(1,3)
print(a+b)
1/2
5/4
4/5
1
序列类型
不可变序列:
数据不能再改变。字符串,元组,字节序列
可变序列:列表,字节数组
字符串: ‘abc’ “123” ‘‘‘qwer’’’
元组类型:(1,“2.3”,“c”)//可以有多种数据类型
字节序列:
数据是一系列的字节。以’b‘开头的字符串
str="abcd字节序列"
print(str)
a=str.encode("utf-8")
print(a)
a=str.encode("gb2312")
print(a)
列表:[1,2] .[“Feb”,[1,2]]
l=[1,2,3]
print(l)
l[0]='zxc'
print(l)
[1, 2, 3]
[‘zxc’, 2, 3]
集合数组 无序 不重 {1,2,3} 数据可变
字典 键和值 值是可变的
c={'name':"zc",'age':"74"}
print(c)
c['name']="lyf"
print(c)
c['wugong']="ssp"
print(c)
{‘name’: ‘zc’, ‘age’: ‘74’}
{‘name’: ‘lyf’, ‘age’: ‘74’}
{‘name’: ‘lyf’, ‘age’: ‘74’, ‘wugong’: ‘ssp’}
运算符:
lambda
f=lambda x,y:x+y
print(f(1,2))
if…else
x=1
y=0
z=3
m=x if y else z
print(m)
m=3
x=1
y=3
z=3
m=x if y else z
print(m)
m=1
x=0
y=3
z=3
m=x if y else z
print(m)
m=0
x=1
y=3
z=0
m=x if y else z
print(m)
m=1
逻辑运算符 or and not
or 有1为1
and 都是1才是1
not 取反,not 非零常数 False
not 0 True
x=0
y=2
print(x or y)
print(x and y)
print(not x)
print(not y)
2
0
True
False
关系符号运算符
< <= >= != ==
返回值是bool类型的
成员运算符
in, not in, is,is not
x=0
y={0,1,2}
print(x in y)
True
位运算符
| :按位或
x=1
y=2
print(x|y)
3
^:按位异或
x=2
y=3
print(x^y)
1
&:按位与
x=2
y=3
print(x&y)
2
:左移
x=8
print(x>>1)
4
<<: 右移
x=3
print(x<<2)
12
~:取反
0按位取反是-1
x=3
print(~x)
-4
/:除号
x=1
y=3
print(x/y)
0.3333333333333333
//:整除
x=1
y=3
print(x//y)
:幂
print(23)
8
小偷问题
for x in range(1,5):
if(((x!=1)+(x==3)+(x==4)+(x!=4))==3):
print(x)
内置函数:
abs():取绝对值
bin():转化为二进制
bool():非零常数为True 0 为False
complex(a,b) a+bj
divmod(a,b) 返回 a除以b的商和余数
eval(s) 返回s所表示的值
s="1+2"
print(eval(s))#3
pow(x,y,z) 返回x的y次方除z的余数
pow(x,y) 返回x的y次方
print(pow(2,3))#8
print(pow(2,3,5))#5
sorted() 返回有序列表
s=[4,5,1]
print(sorted(s))#[1, 4, 5]