变量 None
None是一个特殊的常量。
None和False不同。
None不是0。
None不是空字符串。
None和任何其他的数据类型比较永远返回False。
None有自己的数据类型NoneType。
可以将None复制给任何变量,但是不能创建其他NoneType对象。
连续输入多个
x1,y1,x2,y2 = eval(input(" 输入两个点坐标"))
print(x1,y1,x2,y2)
字符串加法
print("1"+"2")#结果为12,字符串将1,2相当于结合在一起
print(int("1")+2)#结果为3,全部转换成数值型变量
print("1a" + "2b“) #结果为1a2b
数学函数
记住常用的就行,不需要死记硬背。
import math
print(abs(-5))
print(abs(5)) #绝对值
print(max(-1,5,12,45,158,-25,-854,888)) #最大值
print(min(-1,5,12,45,158,-25,-854,888)) #最小值
print(pow(2,4))
print(math.sqrt(9)) #平方根
print(math.sin(3.145926535)) #sin函数
快速查阅函数帮助
-
打开命令行工具
-
输入python(已配置环境变量)
-
调用math函数
-
输入 dir(math)
-
获取使用方法:例如,输入help(math.sqrt)
字符串的三种风格
import os
#os.system('calc')
#os.system("calc")
os.system("""calc""") #三引号可以换行输入
字符与ASCII
ord:求字符的ASCII,字符串长度只能为1
chr:求ASCII为某某的字符
ch1 = 'A'
ch2 = 'AB'
print(type(ch1),type(ch2))#Python里没有字符,只有字符串
ch1 = 'A'
print(ord(ch1))#ord求字符的编号,字符串长度只能为1
#print(ord(ch2)) #报错
print(chr(97))#chr 求编号为97的字符
中文字符ASCII问题
任何一个字符都有ASCII
通过ASCII也可以求出对应汉字。
注:汉字主要用统一码表示
ch = '我'
print(ord(ch))
print(chr(25105)) #‘我’,ASCII为25105
统一码
#print(char(25105)) #25105转换成16进制为6211
print("\u6211") #\u统一码
ASCII码用途
- 加密
- 大小写转换
ch = 'A'
print(ord(ch))
print(ch + 32) #结果为a
转义字符
\:转义字符
print("He said,\" John's program is easy to read\"")# \" 代表输入双引号
print("AAA\nBBB") #换行
print高级用法
1.end默认为\n换行,end =","
2.print连续输出时,默认以空格作为分割,可以用sep="",更改其中分割符号,例如sep=",'
print("A",end="")#end默认为\n换行,不输入不换行,end =","
print("A",end="")
print("A")
print(1,2,3)
print(1,2,3,sep=",")#print连续输出时,默认以空格作为分割。
#可以用sep="",更改其中分割符号,例如sep=",'
字符串加法和转换
myStr1 = "note"
myStr2 = "pad"
print(myStr1+","+myStr2) #字符串加法
print(myStr1+","+myStr2)
对象和类
字符串格式化
d:整型
s:字符型
f:实数
format(12,"19d")#19代表宽度
format("12","19s")#19代表宽度
format(12.221,"19.2f")#19代表宽度,.2表示保留两位小数
print(10.32,12,15.2,23,13,21,21.2)
print(1.5,152,415.2,2453,163,2451,21.2)
print(45,23,5,4,21,54,1,21,5,42,1)
#10.8f,10宽度统一为10,小数点8位,float 需要对齐
#有< 左对齐,没有 < 右对齐,即,顶尖指向哪里,就往那里对齐。
print(format(10.32,"<20.8f"),12,15.2,23,13,21,21.2)
print(format(1150.32,"<20.8f"),12,15.2,23,13,21,21.2)
print(format(15470.32,"<20.8f"),12,15.2,23,13,21,21.2)
print(format(7170757.32,"<20.8f"),12,15.2,23,13,21,21.2)
#d显示整数,f显示实数,19代表宽度
print(format(10,">19d"),12,15.2,23,13,21,21.2)
print(format(1150,">19d"),12,15.2,23,13,21,21.2)
print(format(15470,">19d"),12,15.2,23,13,21,21.2)
print(format(7170757,">19d"),12,15.2,23,13,21,21.2)
#d显示整数,f显示实数,s显示实数,19代表宽度
print(format("10.32","19s"),12,15.2,23,13,21,21.2)
print(format("1150.32","19s"),12,15.2,23,13,21,21.2)
print(format("15470.32","19s"),12,15.2,23,13,21,21.2)
print(format("7170757.32","19s"),12,15.2,23,13,21,21.2)
#format 返回值为str
print(format(1,"19d"))
print(type(format(1,"19d")))
进制
num = 0 * 10 #16进制
num = 10 #10进制
num = 0b10 #2进制
num = 0o10
print("num")
图形绘制
turtle.screensize(3024,2768)#屏幕大小
turtle.write("Hello",font=("华文琥珀",20,"normal"))#设定字体大写
颜色填充
turtle.begin_fill() #填充开始
turtle.circle(100,steps=6)#steps=4,意思是有几个角,为4的话时四边形
turtle.color("blue") #填充颜色
turtle.end_fill() #结束填充
隐藏箭头
turtle.hideturtle() #隐藏箭头
turtle.reset() #重置
import turtle
turtle.screensize(3024,2768)#屏幕大小
turtle.write("Hello",font=("华文琥珀",20,"normal"))#设定字体大写
turtle.begin_fill() #填充开始
turtle.circle(100,steps=4)#steps=4,意思是有几个角,为4的话时四边形
turtle.color("blue") #填充颜色
turtle.end_fill() #结束填充
turtle.reset() #重置
turtle.pensize(20) #画笔变粗
turtle.begin_fill() #填充开始
turtle.circle(100,steps=6)#steps=4,意思是有几个角,为4的话时四边形
turtle.color("red") #填充颜色
turtle.end_fill() #结束填充
turtle.hideturtle() #隐藏箭头
turtle.done()
随机字符生成
import time
timesdate = time.time()
print(timesdate)
timesdate = int(timesdate)
print(timesdate)
timesdate = timesdate%26 #数字限定在0-26之间
print(chr((ord('A') )+ timesdate))
print(ord('A'))
print(ord('Z'))