接上节课
-
format格式化输出
''.format() 是 使用 {}去代替传统的%
格式:<模板字符串>.format(<逗号分隔的参数>)
print('当前使用英雄为{},当前等级为{}级'.format(name,grade))
当前使用英雄为鲁班七号,当前等级为15级
# 可以通过序号的方式指定模板字符串中参数的使用,参数从0开始编号
print('当前使用英雄为{1},当前等级为{0}级'.format(grade,name))
当前使用英雄为鲁班七号,当前等级为15级
print('当前使用英雄为{name},当前等级为{grade}级'.format(name=name, grade=grade))
当前使用英雄为鲁班七号,当前等级为15级
-
f-string
f-string 是Python3.6中定定义的一种参数化字符串的方式,主要目的是让字符串格式化更加便捷*
用{}标识变量,但是不是使用{}进行站位,而是直接写入变量名
print(f'当前使用英雄为{name},当前等级为{grade}级')
当前使用英雄为鲁班七号,当前等级为15级
-
课堂练习
name=input('姓名')
company= input('公司')
position = input('职位')
phone = input('电话')
email = input('邮箱')
print('*'*40)
print(f'公司名称:{company}')
print(f'名字(职位):{name}({position})')
print(f'电话:{phone}')
print(f'邮箱:{email}')
print('*'*40)
****************************************
公司名称:东软
名字(职位):杨玖林(学员)
电话:166*****994
邮箱:359717535@qq.com
****************************************
一、变量的命名
1、标识符和保留字
标识符就是程序员自己命名的变量名,原则上标识符的命名要有见名知意的效果,不要随意起名
# c='东软'
# en=232323 不好变量名字
company = 'neusoft'
employeeNum = 321313
标识符可以有字幕、下划线数字组成
不能以字母开头
不能以保留字重复
保留字,关键字
关键字就是Python中内部已经定义好的标识符
具有特殊的功能和含义
不允许定义与关键字相同的标识符
#查看python中的关键字
import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
2、变量的命名规则
规范的命名是为了增加代码的可读性
2.1、下划线命名法 原则:每个单词都要小写,单词之间使用_链接
first_name = 'yang'
qq_number = 359717535
2.2、驼峰式命名法
- 小驼峰:第一个单词小写字母开始,后续单词首字母大写
first_Name = 'yang'
- 大驼峰:每个单词首字母都采用大写的方式
First_Name = 'yang'
二、运算符
1、算术运算符
运算符 | 描述 | 实例 |
---|---|---|
+ | 加 | 10 + 20 = 30 |
- | 减 | 10 - 20 = -10 |
* | 乘 | 10 * 20 = 200 |
/ | 除 | 10 / 20 = 0.5 |
// | 取整除 | 返回除法的整数部分(商) 9 // 2 输出结果 4 |
% | 取余数 | 返回除法的余数 9 % 2 = 1 |
** | 幂 | 又称次方、乘方,2 ** 3 = 8 |
a=10
b=5
print(a+b)
15
print(a-b)
5
print(a*b)
50
print(a/b)
2.0
print(a**b)
100000
print(a//b)
2
print(a%b)
0
2、比较(关系)运算符
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | a 等效于 c = c / a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
%= | 取 模 (余数)赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c = a 等效于 c = c a |
a=10
b=20
a==b
False
a!=b
True
a>b
False
a<b
True
a>=b
False
a<=b
True
3、逻辑运算符
运算符 | 逻辑表达式 | 描述 |
---|---|---|
and | x and y | 只有 x 和 y 的值都为 True,才会返回 True;否则只要 x 或者 y 有一个值为 False,就返回 False |
or | x or y | 只要 x 或者 y 有一个值为 True,就返回 True;只有 x 和 y 的值都为 False,才会返回 False |
not | not x | 如果 x 为 True,返回 False;如果 x 为 False,返回 True |
a=True
b=False
a and b
False
a or b
True
not a
False
not -1
False
not 0
True
4、赋值运算符
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c = a 等效于 c = c a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
%= | 取 模 (余数)赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c = a 等效于 c = c a |
a=10
b=20
c=0
c=a+b
print(c)
30
c+=10
print(c)
40
c-=a
print(c)
30
c*=a
print(c)
300
c/=a
print(c)
30.0
c%=a
print(c)
0.0
c=a**5
print(c)
100000
c//=b
print(c)
5000
print(b)
20
5、运算优先级
运算符 | 描述 |
---|---|
** | 幂 (最高优先级) |
* / % // | 乘、除、取余数、取整除 |
+ - | 加法、减法 |
<= < > >= | 比较运算符 |
== != | 等于运算符 |
= %= /= //= -= += = *= | 赋值运算符 |
not or and | 逻辑运算符 |
三、条件语句
条件判断语句 Python中任何非0 和非空None 的值都为True; 0或者None就为false
1、if语句基本形式
#if 判断语句:
# 执行语句.....';
# else:
# 执行语句....;
result = 59
if result >= 60:
print('及格')
else:
print('不及格')
# 下面这么写同样成立
num = 20
if num:
print('if执行了')
num = 20
if True:
print('ifTrue执行了')
chepiao = 1 #1代表有车票,0代表没车票
if chepiao == 0:
print('有车票了,可以上火车')
print('终于可以见到她了,美滋滋~~~')
else:
print('没有车票,不可以上火车')
print('亲爱的,那就下次再见吧,一票难求啊~~~(>_<)')
练习:
- 从键盘中输入刀子的长度,如果刀子的长度小于10cm,则允许上火车,否则不允许
length = int(input('请输入刀子的长度:'))
if length < 10:
print('您好,刀的长度符合规定,您可以上火车了')
else:
print('不好意思,不能携带此刀上火车')
1.1、if 语句多个判断的形式
很多编程语言使用switch去判断多个条件
Python并没有提供switch语句,而是使用elif来代替
- 格式
if xxx1:
事情1
elif xxx2:
事情2
elif xxx3:
事情3
else:
以上都不满足执行的事情
score = int(input('请输入您的成绩'))
if score >= 90 and score <= 100:
print('您的成绩为优秀')
elif score <90 and score >=75:
print('你的成绩为良好')
elif score <75 and score >=60:
print('您的成绩为及格')
elif score <60 and score >=0:
print('您的成绩不及格')
else:
print('您输入的成绩不合法')
# 注意 elif 必须和if 一起使用,否则会出错
# else 可以选择性使用,是以上条件都不满足是要执行的事情
1.2、 if 的嵌套
if 条件1:
满足条件1执行的事情
if 条件2:
满足条件2执行的事情
# 说明
# 外层的if判断,也可以是if else
# 内层的if判断,也可以是if else
- 应用
chepiao = 1
daolength = 9
if chepiao ==1:
print('有车票,可以上车')
if daolength < 10:
print('通过安检')
print('终于可以见到她了,美滋滋~~~')
else:
print('没通过安检')
print('刀子超过规定,等待警察处理。。。。。')
else:
print('没有车票,不可以上火车')
print('亲爱的,那就下次再见吧,一票难求啊~~~(>_<)')
- 猜拳游戏
i=0
while i<1:
player = int(input('请输入剪刀(0),石头(1),布(2):'))
# 电脑随机生成[0,2]只看的数字
computer = randint(0, 2)
if player not in [0,1,2]:
print('输入错误,请重启代码')
else:
# 赢得情况
if (player == 0 and computer==2) or (player == 1 and computer==0) or (player == 2 and computer==1):
print('这是高手,你赢了')
i=1
elif player == computer:
print('哈哈,平局,要不再来一局')
else:
print('你输了,不要走,洗洗手,决战到天亮')
2、循环
break 用于跳出本层循环 continue 用于跳出本次循环,继续执行下一次循环
pass 是空语句 用于占位
2.1、while循环格式
while 循环条件:
循环体
- 求1~100之间的累加和
定义一个用于循环中自增长的变量
i = 1
定义一个储存累加和变量
sum_res = 0
while i<=100:
print('当前执行的循环是第{}次'.format(i))
sum_res += i
#自增操作
i += 1
print('当前i的值是{}'.format(i))
print(sum_res)
- 计算1~100之间偶数的累加和
while i<=100:
if i%2==0:
sum_res += i
i += 1
print(sum_res)
- 累加和大于1000时输出累加和变量
while i<=100:
print('当前执行的循环是第{}次'.format(i))
sum_res += i
if sum_res >1000:
break
#自增操作
i += 1
print('当前i的值是{}'.format(i))
print(sum_res)
- 计算1~100之间奇数的累加和
while i<=100:
if i%2==0:
i += 1
continue
sum_res += i
i += 1
print(sum_res)
else可有可没有
while i<=100:
if i%2==0:
i += 1
continue
else:
sum_res += i
i += 1
print(sum_res)
while 循环的嵌套
# 在控制台中输出下面图形
# *
# * *
# * * *
# * * * *
# * * * * *
#1、
i = 1
while i<=5:
print('* '*i)
i+=1
#2、
#里层循环负责行的输出,外层负责换行
i = 1
while i <=5:
j = 1
while j<=i:
print('* ',end='')
j+=1
print(end='\n')
i+=1
- 打印九九乘法表
i = 1
while i<=9:
j=1
while j<=i:
print( j,'x',i,'=',j*i,'',end='')
j+=1
print()
i+=1
2.2、for 循环
语法格式 iterable 可迭代的
for iterating_var in sequence:
statements(s)
2.2.1、取出字符串
1、
for zifuchuan in '杨玖林':
print(zifuchuan,end='\t')
2、
name = '杨玖林'
i = 0
while i<len(name):
print(name[i])
i += 1
2.2.2、for 循环的计数(上面的循环无法进行计数)
print(range(10))
print(list(range(10)))
for x in range(10):
print(x)
#等价
for x in [0,1,2,3,4,5,6,7,8,9]:
print(x)
- 九九乘法表改成for循环形式
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(j,i,j*i),end=' ')
print()
- 猜数字游戏
1、范围数字(0,100)
电脑生成数字
猜这个数字,提示猜大了还是猜小了,直到猜对结束
from random import randint
computer = randint(0,100)
while True:
num = int(input('请输入数字:'))
if num > computer:
print("您猜大了")
elif num < computer:
print('您猜小了')
else:
print('恭喜你,猜对了')
break
练习
- 九九乘法表
row = 1
while row<=9:
col=1
while col<=row:
print( '%d*%d=%d'%(col,row,col*row),end='\t')#\t等同于tab键
col+=1
print('\n')
row+=1
- 猜拳
from random import randint
while True:
player = int(input('请输入剪刀(0),石头(1),布(2):'))
# 电脑随机生成[0,2]只看的数字
computer = randint(0, 2)
if player not in [0,1,2]:
print('输入错误,请重启代码')
else:
# 赢得情况
if (player == 0 and computer==2) or (player == 1 and computer==0) or (player == 2 and computer==1):
print('这是高手,你赢了')
break
elif player == computer:
print('哈哈,平局,要不再来一局')
else:
print('你输了,不要走,洗洗手,决战到天亮')