Python基础学习第二天(1)
赋值
a,b=b,a #互换
常量
Python不支持常量 #实际可以改,但是逻辑上不能改
数据类型
整型、浮点型、布尔型、字符串
8/2=4.0 (浮点型)
7//2(整型)
7%4(余数)
2**3(幂)
divmod(10,3)
#商和余数
整型
0B101
0O10#8进制
0Xff#16进制表示255
浮点数
科学计数法 3.14=314E-2
类型转换和四舍五入round(value)
时间
import time
time.time()
b=int(time.time())
totalMinutes=b//60 #整除`
`
画图求距离
import turtle as t
import math
#定义多个点
x1,y1=100,100
x2,y2=100,-100
x3,y3=-100,-100
x4,y4=-100,100
#绘制路径
t.penup()
t.goto(x1,y1)
t.pendown()
t.goto(x2,y2)
t.goto(x3,y3)
t.goto(x4,y4)
dis=math.sqrt(x1-x4)**2+(y1-y4)**2
t.write(dis)
布尔值
逻辑运算符
or and not
同一运算符
is
is not
#判断是不是一个对象ID,==比较value
字符串
Unicode(16位)
‘’’#多行字符串
len()#查询长度
\n转义换行
b=‘I’m a teacher’
控制台读取字符串myname=input(‘请输入姓名’)
a=a.replace(‘c’,‘高’)
切片:slice
a[:] a[1:5:2][::-1]
#倒序输出
a=‘to be or not to be'
a[: : -1]
#输出所有s
‘sxtsxtsxtsxt’[: : 3]
分割
a.split(‘to’)
拼接
import time
time1=time.time()
a=''
for i in range(1000000):
a+='sxt'
time2=time.time()
print('运算时间'+str(time2-time1))
time3=time.time()
li=[]
for i in range(100000):
li.append('sxt')
a=''.join(li)
time4=time.time()
print('运算时间'+str(time4-time3))
字符串常用方法
find()
strip() rstrip()
大小写转化
格式排版a.center(10,’’) a.ljust(10,’’) a.rjust(10,’*")
格式化-
a=‘我是{0},我的存款有{1:2f}’
a.format(‘高崎’,3234.2323)
a='我是{age},我的存款有{money}
a.format(age=‘高崎’,money=‘12324元’)