# ### 代码块 : 用冒号作为开始,用缩进来划分相同的作用域
"""作用域: 作用范围"""
# 基本使用
"""代码块是一个整体,在条件满足时,会一并执行,否则一并不执行"""
if 10 == 10:
print(1)
print(2)
print(3)
# 注意点
"""在使用代码时,要么全部使用缩进 ,要么全部使用4个空格,不能混合使用"""
'''
if 10 == 10:
print(5)
print(6)
print(7) # IndentationError
'''
'''
# 扩展: (php js java ...)
if (10 == 10){
print(0)
print(1)
print(2)
}
'''
# ### 流程控制
"""
流程 : 代码执行的顺序
控制 : 对代码执行顺序的把控
三大结构:
(1) 顺序结构:
代码从上到下依次执行
(2) 分支结构:
四大小类
(3) 循环结构:
1.for循环
2.while循环
分支结构:
(1) 单项分支
(2) 双项分支
(3) 多项分支
(4) 巢状分支
"""
# 单项分支
"""
if 条件表达式:
code1
code2
当条件表达式成立的时候,执行代码块其中的内容,否则不执行
"""
xk = "帅哥"
if xk == "帅哥":
print("哇,好帅呀")
# 双项分支 (二选一)
"""
if 条件表达式:
code1
code2
else:
code3
code4
当条件表达式成立的时候,执行if这个分支的代码块
当条件表达式不成立的时候,执行else这个分支的代码块
if分支代码块也叫作真区间
else分支代码块也叫作假区间
"""
xk = "靓女"
if xk == "靓仔":
print("买包包给他")
else:
print("买西装给他")
# ### 登录 账号/密码
"""账户: admin 密码:111 => 登录成功 , 否则提示登录失败 """
# input => 获取用户的输入内容 ,类型是字符串
# name = input("请输入您的姓名:")
# print(name,type(name))
"""
username = input("请输入您的用户名")
password = input("请输入您的密码")
if username == "admin" and password == "123123":
print("登录成功")
else:
print("登录失败")
"""
# ### 多项分支 (多选一)
"""
语法结构:
if 条件表达式1:
code1
code2
elif 条件表达式2:
code3
code4
elif 条件表达式3:
code5
code6
else:
code ...
如果条件表达式1 成立 , 执行对应代码块 , 如果不成立,继续向下判断
看一下条件表达式2 是否成立 ,成立的话执行对应代码块, 如果不成立,继续向下判断
看一下条件表达式3 是否成立 ,成立的话执行对应代码块, 如果不成立,继续向下判断
当所有的条件都不满足的时候,直接执行else这个分支
elif 可以出现0次或者多次
else 可以出现0次或者1次
"""
youqian = False
youfang = False
youche = False
yanzhi = True
renpin = False
if youqian == True:
print("选择嫁给他")
elif youche == True:
print("选择嫁给他")
elif yanzhi == True:
print("选择嫁给他")
elif renpin == True:
print("选择嫁给他")
else:
print("你是个好人")
print("<=====>")
# ### 巢状分支
"""单项分支,双项分支,多项分支的互相嵌套的写法"""
youqian = False
youfang = False
youche = False
yanzhi = False
renpin = False
if youqian == True:
if youfang == True:
if youche == True:
if yanzhi == True:
if renpin == True:
print("嫁给他,接盘侠")
else:
print("你不适合我")
else:
print("去韩国整容再来吧")
else:
print("好好赚钱")
'''
#出题 height
#女生找对象
# 男生在1米~1.5米之间 小强 你在哪里?
# 男生在1.5~1.7米之间 没有安全感~
# 男生 1.7~ 1.8米之间 帅哥 留个电话
# 男生 1.8~2米之间 帅哥 你建议多一个女朋友吗
height = float(input("请输入您的身高:"))
print(height,type(height))
# python特有
"""
if 1 <= height < 1.5:
print("小强 你在哪里")
elif 1.5 <= height < 1.7:
print("没有安全感")
elif 1.7 <= height < 1.8:
print("帅哥 留个电话")
elif 1.8 <= height < 2:
print("帅哥 你建议多一个女朋友吗")
else:
print("没有合适的选项")
"""
#通用写法
if height >= 1 and height < 1.5:
print("小强 你在哪里")
elif height >= 1.5 and height < 1.7:
print("没有安全感")
elif height >= 1.7 and height < 1.8:
print("帅哥 留个电话")
elif height >= 1.8 and height < 2:
print("帅哥 你建议多一个女朋友吗")
else:
print("没有合适的选项")
'''
print("<=====>")
# ### 循环结构
"""特点:减少冗余的代码,提升开发的效率,便于后期的维护"""
"""
(1) 初始化一个值
(2) 写上循环条件
(3) 写上自增自减的值
while 条件表达式:
code
"""
# 打印1 ~ 100的数字
# (1) 初始化一个值
i = 1
# (2) 写上循环的条件
while i <= 100:
# (4) 写上循环的逻辑
print(i)
# (3) 写上自增自减的值
i+=1 # i = i+1
"""
# 代码解析:
# 第一次循环
i = 1 , i <= 100 =>True,执行循环 print(i) => 1
i += 1 => 1 + 1 => 2
# 第二次循环
i = 2 , i <= 100 =>True,执行循环 print(i) => 2
i += 1 => 2 + 1 => 3
# 第二次循环
i = 3 , i <= 100 =>True,执行循环 print(i) => 3
i += 1 => 3 + 1 => 4
依次类推...
当i = 101 时 i <= 100 =>False , 终止循环 , 程序结束.
"""
# 1 ~ 100 计算累加和
# 方法一
i = 1
total = 0
while i <= 100:
total += i # total = total + i
i+=1
print(total,"方法1") # 5050
"""
第一次循环
i = 1 i <= 100 成立 , 执行循环 total += i => total = total + i => 0 + 1
i += 1 => 2
第二次循环
i = 2 i <= 100 成立 , 执行循环 total += i => total = total + i => 0 + 1 + 2
i += 1 => 3
第三次循环
i = 3 i <= 100 成立 , 执行循环 total += i => total = total + i => 0 + 1 + 2 + 3
i += 1 => 4
什么时候结束?
i = 101 i <= 100 不成立 , 不执行循环,循环终止
total => 0 + 1 + 2 + 3 + ... + 100 = 5050
"""
# 死循环
"""
while True:
print(123)
"""
# 死循环的写法完成1~100的累加和
# 方法二
i = 1
total = 0
sign = True
while sign:
total += i # 1 + 2 + ... +100
i+=1
if i == 101:
# 手动设置False ,跳出循环
sign = False
print(total,"方法2")
print("<=====>")
# ### 单向循环练习
# (1)打印一行十个小星星
"""
help(print)
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
"""
# print默认是打印一行 ,结尾加换行。end=''意思是末尾不换行 。
i = 1
while i <= 10:
print("*",end="")
i+=1
print()
# (2)用变量拼接字符串的形式,打印一行十个小星星
# + 号可以用来拼接字符串
'''strvar = "可爱的" + "小宝贝"
strvar = "可爱的"
strvar += "小宝宝" # strvar = strvar + "小宝宝" => 可爱的小宝宝
print(strvar)
'''
i = 0
strvar = ""
while i < 10:
strvar += "*"
i+=1
print(strvar)
"""
代码解析
strvar = "*"
strvar = "*" + "*" ="**"
strvar = "**" + "*" ="***"
strvar = "***" + "*" ="****"
...
strvar = "*********" + "*" = "**********"
"""
# (3)打印一行十个小星星 奇数个打印★ 偶数个打印☆
# ★☆★☆★☆★☆★☆
i = 0
while i < 10:
if i % 2 == 0:
print("★",end="")
else:
print("☆", end="")
i+=1
"""
### 规律: 任意数和n取余,余数范围是多少? 0~ (n-1)
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
"""
print()
# (4)用 一个循环 打印十行十列小星星
i = 0
while i < 100:
# 打印星星
print("★",end="")
# 什么时候换行 9,19,29... 99 是换行点
if i % 10 == 9:
# 添加换行
print()
i+=1
"""
0123456789
**********
10111213141516171819
* * * * * * * * * *
20 ~ 29
* * * * * * * * * *
30 ~ 39
* * * * * * * * * *
...
90 ~ 99
* * * * * * * * * *
9,19,29... 99 是换行点
i % 10 == 9
"""
print()
# (5)一个循环 打印十行十列隔列变色小星星(一个循环) """ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ """ i = 0 while i < 100: # 打印黑白相间的小星星 if i % 2 == 0: print("★",end="") else: print("☆", end="") # 打印换行 if i % 10 == 9: # 添加换行 print() i+=1 # (6)一个循环 打印十行十列隔行变色小星星(一个循环) """ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ """ """ ###规律:任意数和n进行地板除,会出现n个相同的数字! 10个相同的0 0 // 10 => 0 1 // 10 => 0 2 // 10 => 0 3 // 10 => 0 4 // 10 => 0 ... 9 // 10 => 0 10个相同的1 10 // 10 => 1 11 // 10 => 1 12 // 10 => 1 ... 19 // 10 => 1 0 // 2 => 0 1 // 2 => 0 2 // 2 => 1 3 // 2 => 1 0 ~ 9 // 10 => 10个相同的0 10 ~ 19 // 10 => 10个相同的1 20 ~ 29 // 10 => 10个相同的2 ... 90 ~ 99 // 10 => 10个相同的9 相同数字的范围是0 ~ 9 """ i = 0 while i < 100: # 打印黑白相间的小星星 if i // 10 % 2 == 0: print("★",end="") else: print("☆", end="") # 打印换行 if i % 10 == 9: # 添加换行 print() i+=1 # ### 双向循环练习 # (1)打印十行十列小星星 (用两个循环) j = 0 while j < 10: # 打印一行十个星星,控制列 i = 0 while i < 10: print("*",end="") i+=1 # 控制换行 print() j+=1 # (2)打印十行十列隔列换色小星星 j = 0 while j < 10: i= 0 while i < 10: if i % 2 == 0: print("★", end="") else: print("☆", end="") i+=1 print() j+=1 # (3)打印十行十列隔行换色小星 j = 0 while j < 10: # 控制打印一行十个黑白相间的星星 i = 0 while i < 10: if j % 2 == 0: print("★", end="") else: print("☆", end="") i += 1 # 打印换行 print() j += 1 print() # (4)99乘法表 # 方向一 i = 1 while i <= 9: j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="") j+=1 print() i+=1 print() # 方向二 i = 9 while i >= 1: # 打印表达式 j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="") j+=1 # 打印换行 print() i-=1 # 方向三 i = 1 while i <= 9: k = 9 - i while k > 0: print(" ",end="") k-=1 j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="" ) j+=1 print() i+=1 print() # 方向四 i = 9 while i >= 1: # 打印空格 k = 9 - i while k > 0: print(" ", end="") k -= 1 # 打印表达式 j = 1 while j <= i: print("%d*%d=%2d " % (i, j, i * j), end="") j += 1 # 打印换行 print() i -= 1 # (5)100 ~ 999 找吉利数字 111 222 123 321 888 ... """ 678 // 可以取到一个数的高位 % 可以取到一个数的低位 # 个位 678 % 10 => 8 # 十位 678 // 10 % 10 = 7 # 百位 678 // 100 => 6 """ i = 100 while i <= 999: # 个位 gewei = i % 10 # 十位 shiwei = i // 10 % 10 # 百位 baiwei = i // 100 # 111 222 333 if shiwei == gewei and shiwei == baiwei: print(i) # 123 456 elif shiwei == gewei - 1 and shiwei == baiwei + 1: print(i) # 321 654 elif shiwei == gewei + 1 and shiwei == baiwei - 1: print(i) i+=1 # 方法二 print("<====>") i = 100 while i < 1000: # 先变成字符串 strvar = str(i) # 用字符串取下标的形式得到个十百,强转成整型 gewei = int(strvar[-1]) shiwei = int(strvar[1]) baiwei = int(strvar[0]) # print(baiwei,shiwei,gewei) # 111 222 333 if shiwei == gewei and shiwei == baiwei: print(i) # 123 456 elif shiwei == gewei - 1 and shiwei == baiwei + 1: print(i) # 321 elif shiwei == gewei + 1 and shiwei == baiwei - 1: print(i) i += 1
# break 终止当前循环(只能用在循环中) # 1~10 遇到5 终止 i = 1 while i <= 10: if i == 5: break print(i) i+=1 for i in range(1,10): if i == 5: break print(i) i = 1 while i <= 3: j = 1 while j <=3 : if j == 2: break print(i,j) j+=1 i+=1 # (3)continue 跳过当前循环(只能用在循环当中) # 1~10 遇到5跳过 # while 循环 i = 1 while i <= 10: if i == 5: # 需要手动加一, 否则死循环 i += 1 continue print(i) i+=1 # for循环 for i in range(1,11): if i == 5: continue print(i)
# (5)一个循环 打印十行十列隔列变色小星星(一个循环) """ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ """ i = 0 while i < 100: # 打印黑白相间的小星星 if i % 2 == 0: print("★",end="") else: print("☆", end="") # 打印换行 if i % 10 == 9: # 添加换行 print() i+=1 # (6)一个循环 打印十行十列隔行变色小星星(一个循环) """ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ """ """ ###规律:任意数和n进行地板除,会出现n个相同的数字! 10个相同的0 0 // 10 => 0 1 // 10 => 0 2 // 10 => 0 3 // 10 => 0 4 // 10 => 0 ... 9 // 10 => 0 10个相同的1 10 // 10 => 1 11 // 10 => 1 12 // 10 => 1 ... 19 // 10 => 1 0 // 2 => 0 1 // 2 => 0 2 // 2 => 1 3 // 2 => 1 0 ~ 9 // 10 => 10个相同的0 10 ~ 19 // 10 => 10个相同的1 20 ~ 29 // 10 => 10个相同的2 ... 90 ~ 99 // 10 => 10个相同的9 相同数字的范围是0 ~ 9 """ i = 0 while i < 100: # 打印黑白相间的小星星 if i // 10 % 2 == 0: print("★",end="") else: print("☆", end="") # 打印换行 if i % 10 == 9: # 添加换行 print() i+=1 # ### 双向循环练习 # (1)打印十行十列小星星 (用两个循环) j = 0 while j < 10: # 打印一行十个星星,控制列 i = 0 while i < 10: print("*",end="") i+=1 # 控制换行 print() j+=1 # (2)打印十行十列隔列换色小星星 j = 0 while j < 10: i= 0 while i < 10: if i % 2 == 0: print("★", end="") else: print("☆", end="") i+=1 print() j+=1 # (3)打印十行十列隔行换色小星 j = 0 while j < 10: # 控制打印一行十个黑白相间的星星 i = 0 while i < 10: if j % 2 == 0: print("★", end="") else: print("☆", end="") i += 1 # 打印换行 print() j += 1 print() # (4)99乘法表 # 方向一 i = 1 while i <= 9: j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="") j+=1 print() i+=1 print() # 方向二 i = 9 while i >= 1: # 打印表达式 j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="") j+=1 # 打印换行 print() i-=1 # 方向三 i = 1 while i <= 9: k = 9 - i while k > 0: print(" ",end="") k-=1 j = 1 while j <= i: print("%d*%d=%2d " %(i,j,i*j),end="" ) j+=1 print() i+=1 print() # 方向四 i = 9 while i >= 1: # 打印空格 k = 9 - i while k > 0: print(" ", end="") k -= 1 # 打印表达式 j = 1 while j <= i: print("%d*%d=%2d " % (i, j, i * j), end="") j += 1 # 打印换行 print() i -= 1 # (5)100 ~ 999 找吉利数字 111 222 123 321 888 ... """ 678 // 可以取到一个数的高位 % 可以取到一个数的低位 # 个位 678 % 10 => 8 # 十位 678 // 10 % 10 = 7 # 百位 678 // 100 => 6 """ i = 100 while i <= 999: # 个位 gewei = i % 10 # 十位 shiwei = i // 10 % 10 # 百位 baiwei = i // 100 # 111 222 333 if shiwei == gewei and shiwei == baiwei: print(i) # 123 456 elif shiwei == gewei - 1 and shiwei == baiwei + 1: print(i) # 321 654 elif shiwei == gewei + 1 and shiwei == baiwei - 1: print(i) i+=1 # 方法二 print("<====>") i = 100 while i < 1000: # 先变成字符串 strvar = str(i) # 用字符串取下标的形式得到个十百,强转成整型 gewei = int(strvar[-1]) shiwei = int(strvar[1]) baiwei = int(strvar[0]) # print(baiwei,shiwei,gewei) # 111 222 333 if shiwei == gewei and shiwei == baiwei: print(i) # 123 456 elif shiwei == gewei - 1 and shiwei == baiwei + 1: print(i) # 321 elif shiwei == gewei + 1 and shiwei == baiwei - 1: print(i) i += 1
出处:https://www.cnblogs.com/liucx/
# ### for循环 # 遍历 循环 迭代 把里面的元素一个一个拿出来 lst = [1,2,3,4] # lst[0] # lst[1] # lst[2] # lst[3] i = 0 while i < len(lst): print(lst[i]) i+=1 # while 循环在遍历数据时,有局限性,for循环应用而生 """ setvar = {"a","b","c",1,2,3} while i < len(setvar): print(setvar[i]) error i+=1 """ # for 循环语法 """ for 变量 in 可迭代性数据: code .... 可迭代性对象(数据): 1.容器类型(str,list,tuple,set,dict) 2.range对象 3.迭代器 """ # 遍历字符串 container = "你的酒窝不醉人,但是我却神魂颠倒" for i in container: print(i) # 遍历列表 container = ["小李子","小邓子","小卓子","小钉子"] for i in container: print(i) # 遍历元组 container = ("小李子","小邓子","小卓子","小钉子") for i in container: print(i) # 遍历集合 container = {"小李子","小邓子","小卓子","小钉子"} for i in container: print(i) # 遍历字典 (遍历字典的时候,默认只遍历键,没有值) container = {"lz":"李子","dz":"邓子","zz":"卓子"} for i in container: print(i) # 可以遍历不等长的二级容器 container = [("小李子","小邓子"),{"小卓子"},["小钉子"]] for i in container: for j in i: print(j) # 变量的解包 a,b = 1,2 a,b = [3,4] a,b = {7,8} # 把字典中的键依次解包,赋值给对应的变量 a,b,c = {"a":10,"b":11,"c":12} print(a,b,c) # 遍历等长的二级容器 container = [("王思聪","王健林","王美丽"), ("马云",'马化腾',"马丽"),("王宝强","马蓉","宋小宝")] for a,b,c in container: print(a,b,c) # ### range 用法 """ # range("开始值","结束值","步长") 结束值最大取不到,取到结束值之前的那个数据 for 一般配合 range使用 """ # 只有一个值的情况 for i in range(10): print(i) # 两个值的情况 for i in range(1,10): print(i) # 1 ~ 9 # 三个值的情况 for i in range (1,10,3): #(正序) print(i) # 1 4 7 print("<====>") for i in range(10,0,-2):# 倒序 print(i) #10 8 6 4 2 # ### pass break continue # (1) pass 过 """ if 10 == 10: pass while True: pass """# break 终止当前循环(只能用在循环中) # 1~10 遇到5 终止 i = 1 while i <= 10: if i == 5: break print(i) i+=1 for i in range(1,10): if i == 5: break print(i) i = 1 while i <= 3: j = 1 while j <=3 : if j == 2: break print(i,j) j+=1 i+=1 # (3)continue 跳过当前循环(只能用在循环当中) # 1~10 遇到5跳过 # while 循环 i = 1 while i <= 10: if i == 5: # 需要手动加一, 否则死循环 i += 1 continue print(i) i+=1 # for循环 for i in range(1,11): if i == 5: continue print(i)
文章来源于网络,如有侵权请联系站长QQ61910465删除本文版权归趣快排营销www.SEOguruBLOG.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ㊣61910465