1基本数据类型
(1).int 理论是可以无限大,没有上限,不过受限于机器内存的大小
(2).flot(小数)
3.14 10/3 10//3(地板除)
注 : 10=3*3+1
10//3 -->3 取商(整数)
10%3 --> 1 取余数
(3). complex 复数
(4).boolean 布尔类型 判断 真(True) 和 假(flase)
2.变量命名规则: _ 或者字母开头 包含 _ 、字母、数字 且不能为系统内置关键字
3.python 查看内置关键字
import keyword
print (keyword.kwlist)
a=keyword.kwlist
print(len(a))
4. = 赋值符号 == 等于符号
5. 3!=2 不等于
a=1
a+=2 等价于 a=a+2
6. del 删除变量 例如 :del a
7. in , not int
8. 数字的进制表示 权重
万 千 百 十 个
1 2 3 4 5 1*10000+2*1000+3*100+4*10+5*1
二进制
101 从右往左 权重依次为 2**0 + 2**1+ 2**(n-1)
数字表示 进制, 位置 这个位置的权重。
二进制 bin(2)
八进制 oct(9)
十六进制 hex(17) 0-9 A 10 B 11 C 12 D 13 E 14 F 15
9. input 获取到的都是字符串
x = input("请输入你的年龄:")
print(x)
print(type(x))
age=int(x)
print(age)
print(type(age))
10.
11. 输出print函数(可以接受多个参数,逗号隔开)
12.round函数保留n位小数,4舍6入,5判断 前奇进 ,前偶舍。(存在误差)
4.2 round(4.2) 4
4.26 round(4.26,1) 4.3
4.21 round(4.21,1) 4.2
4.25 round(4.25,1) 4.2
4.15 round(4.25,1) 4.1
精度保留小数点
import decimal
a = decimal.Decimal(1.2456)
b = a.quantize(decimal.Decimal("1.00"))
print(b)
能力扩展
作业1.使用input函数,记录键盘输入的内容,打印输出该值的类型,并转换为数字型。
判断数值num大小,如果num>=90,输出打印:你的成绩为优秀;
如果num>=80 and num<90,输出打印: 你的成绩为一般;
如果num<60,输出打印:你的成绩为不合格。
score = int(input("请输入你的成绩:") )
if score>=90:
level="优秀"
elif score>=80:
level="良好"
elif 80>=score>=60:
level="及格"
else:
level="不及格"
print("你的成绩为%s"%(level))
作业2.班级有男生23人,女生15人,计算男女生占班级总人数的百分比,保留两位小数。
male,fmale = 23,15
totle= male+ fmale
rate_male = male / totle
rate_fmale = fmale / totle
import decimal
str_rate_decimal = str(decimal.Decimal(rate_fmale*100).quantize(decimal.Decimal("1.00")))
print("男生占比:%.2f%%,女生占比:%s%%" % (rate_male*100,str_rate_decimal))