Day1、2的Python学习笔记。
包括注释、赋值、运算符、格式化输出、终端输入、流程控制结构(选择分支结构if-else、循环结构for、while)的基本语法。
# 人生苦短,我用python
# print('hellopython')
# print('我是bbg,是个菜鸡')
"""
这里是多行注释
这里是多行注释
这里是多行注释
这里是多行注释
"""
# 赋值,运算符
# 算数运算符 + - * / ** % //
# 比较运算符 == != < > <= >=
# 逻辑运算符 and or not
# 赋值运算符
# a, b, c, d=1, 2, 3, 4
# print(a > b or not c < d)0
# 格式化输出,两种方法,占位符 %+数据类型 和 .format
# name = '白菜'
# classpro = '机械4班'
# age = 2
# print('我的名字是%s,来自%s,年龄%d' % (name, classpro, age))
# print('我的名字是%s,\n来自%s,\n年龄%d' % (name, classpro, age))
# print('姓名:{}'.format(name), '班级:{}'.format(classpro), '年龄:{}'.format(age))
# input练习
# name = input('请输入姓名:')
# print(type(name)) # 查看类型
# classpro = input('请输入班级:')
# print(type(classpro))
# age = int(input('年龄:')) # 强制数据类型转换
# print(type(age))
# print('姓名:{}'.format(name), '班级:{}'.format(classpro), '年龄:{}'.format(age))
# 流程控制结构
# 选择分支结构
'''
单分支
if 条件表达式:
代码
代码
代码
双分支
if 条件:
代码
代码
else:
代码
多分支
if 条件:
代码
elif 条件:
代码
elif 条件:
代码
'''
# if-else可以嵌套使用
# 循环流程
'''
while 条件:
代码
代码
要有初始值,有条件表达式,有变量自增自减
循环次数不确定,依靠循环条件结束循环
for ...in 集合:
代码
代码
遍历操作,依次取集合容器中的值
已知循环次数
'''
# score = int(input('请输入成绩:'))
# if score >= 60:
# print('及格!')
# else:
# print('不及格!')
# print('你这成绩8行')
# print('再接再厉')
# score = int(input('请输入成绩:'))
# if score < 60:
# print('不及格!')
# elif 60 <= score <= 80:
# print('良好')
# elif score > 80:
# print('针不戳')
# 猜拳小游戏
# 石头0 剪刀1 布2
# import random # 导入产生随机数的模块
# person = int(input('请出拳:'))
# computer = random.randint(0, 2)
# if person1==0 and computer==1:
# print('你赢了')
# elif person==1 and computer==2:
# print('你赢了')
# elif person==2 and computer==0:
# print('你赢了')
# elif person==computer:
# print('平手')
# else:
# print('你输了')
# print(computer)
# print('gameover')
# a=1
# while a<=100:
# print(a)
# a+=1
# pass
# 猜拳小游戏连续玩
# 石头0 剪刀1 布2
# import random # 导入产生随机数的模块
# a=0
# while a<=4:
# person = int(input('请出拳:'))
# computer = random.randint(0, 2)
# if person==0 and computer==1:
# print('你赢了')
# elif person==1 and computer==2:
# print('你赢了')
# elif person==2 and computer==0:
# print('你赢了')
# elif person==computer:
# print('平手')
# else:
# print('你输了')
# a+=1
# print('gameover')
# print(a)
# 输出乘法表
# row=9
# while row>=1:
# col=1
# while col<=row:
# print('%d*%d=%d'%(row,col,row*col),end=' ')
# col+=1
# print()
# row-=1
# 打印直角三角形
# row=1
# while row<=7:
# j=1
# while j<=row:
# print('*',end=' ')
# j+=1
# pass
# print()
# row+=1
# 打印等腰三角形
# row=1
# while row<=5:
# j=1
# while j<=5-row: #控制打印空格
# print(' ',end=' ')
# j+=1
# pass
# k=1
# while k<=2*row-1: #控制打印*
# print('*', end=' ')
# k+=1
# pass
# print()
# row+=1
# for 循环
# a='我是菜鸡' # 字符串类型本身就是一个字符类型的集合
# for i in a:
# print(i)
# pass
# range函数,生成一个数据集合列表
# range(起始值,结束,步长),步长不能为零
# for data in range(1,10): # 左包含,右不包含,步长默认是1
# print(data)
# pass
# sum=0
# for data in range(1,10): # 左包含,右不包含
# sum+=data
# pass
# print(sum)
# break中断,结束本层循环 continue结束本次循环,剩下语句不再执行,继续下次循环。
# sum=0
# for item in range(1,51):
# if sum>100:
# print('sum大于100了')
# break
# pass
# sum+=item
# pass
# print('sum=%d'%sum)
# for item in range(1,100): #打印奇数
# if item%2==0:
# continue
# pass
# print(item)
# pass
# for item in '我是菜鸡':
# if item=='是':
# continue
# pass
# print(item)
# pass
# i=1
# while i<=100:
# if i>20:
# break
# pass
# i=i+1
# print(i)
# pass
# 用for做乘法表
# for i in range(1,10):
# for j in range(1,i+1):
# print('%d*%d=%d'%(i,j,i*j),end=' ')
# pass
# print()
# pass
# for-else使用
# for i in range(1,4):
# print(i,end=' ')
# if i>=5:
# break
# pass
# pass
# else:
# print('已经全部遍历!') # 只要出现了break,else代码就不会执行
# while-else使用
# i=1
# while i<=10:
# print(i)
# if i==6:
# break
# i+=1
# pass
# else:
# print('else执行了')