第一趟水——基本语法
#第一次做有点简陋
- 变量和类型
- 整形
- int类型
Python中只有一种整型,其他的整型对Python来说意义不大 - 二进制(b)
0b100 4 - 八进制(o)
0o100 64 - 十进制
100 - 十六机制(x)
0x100 256
- int类型
- 浮点型
- 科学技术法
1.23456e21.23456e-2 - 千分号
123,345
- 科学技术法
- 字符类型
- 单双三引号(‘’ “” ‘‘‘ ’’’)
- Unicode字符串表示法
- 布尔型
- True, False
- 复数型
- 3+5j
较少的地方会用到
- 3+5j
- 整形
- 变量的使用
- type
- 可以用type类型对变量的类型进行、检查
1 a = 100 2 b = 12.345 3 c = 1 + 5j 4 d = 'hello, world' 5 e = True 6 print(type(a)) # <class 'int'> 7 print(type(b)) # <class 'float'> 8 print(type(c)) # <class 'complex'> 9 print(type(d)) # <class 'str'> 10 print(type(e)) # <class 'bool'>
- 可以用type类型对变量的类型进行、检查
- 变量类型转换函数
- int()--float()--str()--chr()
- ord()将字符串转换成Unicode编码形式
- type
- 运算符
- [] [ : ]——下标,切片
- **——指数
- ~ + - ——按位取反,正负号
- * / % // ——乘,除,模,整除
- + - ——加,减
- >> << ——右移, 左移
- &——按位与
- ^ | ——按位异或,按位或
- <= < > >= ——小于等于,小于,大于,大于等于
- == != ——等于,不等于
- is is not ——身份运算符
- in not in ——成员运算符
- not or and——逻辑运算符
- 练习
- 华氏温度转换为摄氏温度
1 f = float(input('请输入华氏温度: ')) 2 c = (f - 32) / 1.8 3 print('%.1f华氏度 = %.1f摄氏度' % (f, c))
- 输入圆的面积计算周长和面积
radius = float(input('请输入圆的半径: ')) perimeter = 2 * 3.1416 * radius area = 3.1416 * radius * radius print('周长: %.2f' % perimeter) print('面积: %.2f' % area)
- 输入年份判断是不是闰年
year = int(input('请输入年份: ')) # 如果代码太长写成一行不便于阅读 可以使用\对代码进行折行 is_leap = year % 4 == 0 and year % 100 != 0 or \ year % 400 == 0 print(is_leap)
- 华氏温度转换为摄氏温度

浙公网安备 33010602011771号