-
回顾昨天知识
-
数据类型
-
字符串 str
'hello' "hello" '''hello''' """hello"""
转义字符
\n(换行new line) \' \" \\ \t(制表符) \r(回车)
运算
'hello' + ' ' + 'world' # 'hello world' 'hello' * 3 # 'hellohellohello'
-
数字
-
整数 int(字面值)
# 十进制 100 0 -1 3 # 二进制 0b1001 0b11 # 八进制 0o12345670 0o3 # 十六进制 0xA1B2C3 0x3 0Xa1B2C3 0X3
-
浮点数 float
3.14 0.314E1
-
布尔类型数 bool
True False
-
-
-
基本输入输出函数
-
input 函数
name = input('请输入姓名:') age = input('请输入年龄:')
-
print 函数
print(name, age)
-
-
运算符
-
算术运算符
+ - * / // % **
-
-
语句
- 赋值语句
-
PyCharm的使用
- PyCharm 下快捷键
-
Windows 安装PyCharm 的教程
http://tedu.weimingze.com/static/python/pycharm_install.html
del 语句
- 作用:
- 语法:
- 自动化内存管理和引用计数: 每个对象都会记录有几个变量引用自身,当引用的数量为0时则此对象被销毁,此种自动化内存管理的方式叫做引用计数。
比较运算符
-
运算符
< 小于 <= 小于等于 > 大于 >= 大于等于 == 等于 != 不等于
-
示例
>>> 100 + 200 > 3 True >>> 1 + 3 >= 2 True >>> 1 + 3 >= 200 # 表达式 False >>> score = 83 # 这是赋值语句 >>> 60 <= score <= 100 True >>> score = 59 >>> 60 <= score <= 100 False
表达式和语句的概念
-
表达式
是由数字,字符串(文字), 运算符,函数调用等组成,通常用于计算并得到一个结果
-
语句
语句是计算机执行程序的最小单位
-
示例
a = 100 # 赋值语句 print(a) # 表达式语句
-
-
函数调用是表达式
- 学过的函数
函数调用语法规则
- 函数名(传入的参数)
-
数据类型转换相关的函数
函数 说明 str(x) 把传入的x 转化成字符串并返回 int(x) 把 x 转化为整数并返回 float(x) 把 x 转化为浮点数并返回 bool(x) 把 x 转化为布尔类型的数并返回 -
示例:
>>> age = input('请输入年龄: ') # 输入 35 >>> int(age) 35 >>> int("35") 35 >>> int(3.14) 3 >>> int('3.14') # 报错 >>> f = 3.14 >>> str(f) '3.14' >>> int(f) 3 >>> bool(f) True
-
python 中假值对象
None False 0 0.0 '' [] # 空列表 {} # 空字典 ...
-
练习
布尔运算符(也叫逻辑运算符)
-
运算符
and 与运算 or 或运算 not 非运算
and 与运算
- 语法
-
示例
>>> 3 + 4 > 5 and 6 + 7 > 100
- 真值表
x的值 y的值 x and y的值 True True True True False False False True False False False False
- or 或运算
-
真值表
x的值 y的值 x or y的值 True True True True False True False True True False False False
- not 非运算
语法
示例
not True # False
not False # True
not 3.14 # False
not '' # True
not 1 + 2 # False
-
and 示例
>>> True and True # True >>> True and False # False >>> False and True # False >>> False and False # False >>> True or True # True >>> True or False # Ture >>> False or True # Ture >>> False or False # False >>> not False # True >>> not True # Flase >>> 3.14 and 5 # 5 >>> 0.0 and 5 # 0.0 >>> 3.14 or 5 # 3.14 >>> 0.0 or 0 # 0 >>> not 3.14 # False >>> not 0.0 # True
问题
if 语句
- 作用
-
语法
if 条件表达式1: 语句块1 elif 条件表达式2: 语句块2 elif 条件表达式3: 语句块3 ... elif 条件表达式n: 语句块n else: 语句块(其他)
练习
- 练习2
- 练习3
如果成绩在 [80,90) 提示"良好"
如果成绩在 [90,100] 提示"优秀"
如果是其他值,则提示“您的输入有误”
-
if 语句也是语句,他可以嵌套到其他的复合语句中
if xxxx: if yyyy > 0: print('.....') else: print("fjdsfdf") else: print("hello")
pass 语句
- 作用
- 语法
-
示例
# 如果 成绩在 0~100 什么都不做, 其他提示"您的输入有误" score = int(input('请输入成绩:')) if 0 <= score <= 100: pass else: print('您的输入有误!')
条件表达式
- 语法
-
说明
-
执行顺序,
- 先判断 条件表达式C 是真值还是假值,
- 如果 C是真值,则表达式x 执行并返回结果
- 如果 C是假值,则表达式y 执行并返回结果
-
-
示例
>>> score = 69 >>> "及格" if 60 <= score <= 100 else "不及格" '及格' >>> s = "及格" if 60 <= score <= 100 else "不及格" >>> print(s) 及格
练习
- 课后练习1
BMI 值 | 结果 |
---|---|
BMI < 18.5 | 体重过轻 |
18.5 <= BMI <=24 | 正常 |
BMI > 24 | 体重过重 |
- 课后练习2