Python 的判断类型type : 获取值的类型
res = 123
print(res , type(res))isinstance: 获取值的类型
用法一
isinstance(要判断的值,要判断的类型) 返回True 或者 False
用法二
isinstance(要判断的值,(可能的类型1,可能的类型2,可能的类型3 ...))
如果有一个类型满足,返回的就是True 否则 返回False
使用范围 => 数据类型
int 整型 float 浮点型 bool 布尔型 complex 复数 str 字符串 list 列表 tuple 元组 set 集合 dict 字典
# 用法一
res = isinstance(5,int)
res = isinstance("abcdef",str)
res = isinstance([1,2,3,4],tuple) # False
print(res)
# 用法二
res = isinstance(21 , (str,list,tuple,int))
print(res)
Python 的代码块
代码块: 以冒号作为开始,用缩进来划分作用域
作用域: 作用的区域,作用的范围
注意点: 要么全都是一个tab缩进,要么全都是4个空格,不能混合使用
if 11 == 11: # True 执行
print(1)
print(2)
print(3)
print(4)

if 11 == 12: # False 不执行
print(111)
print(222)
print(333)
print(444)
Python 的流程控制
流程: 代码执行的过程
控制: 对代码执行过程的一种把控
三大结构顺序结构 : 默认代码依次从上到下执行
分支结构: 4种
循环结构: for while
1. 分支结构
分支结构单项分支
双项分支
多项分支
巢状分支
1. 单项分支
代码语法:
if 条件表达式:
code1
code2
当条件表达式成立,返回True,执行对应的代码块,反之不执行
luna = "女生"
if luna == "女生":
print("化妆1个小时")
print("收拾衣服1个小时")
print("逛街4个小时")
print("吃饭2个小时")
2. 双项分支
代码语法:
if 条件表达式:
code1
code2
else:
code3
code4
如果条件表达式成立,执行if对应的代码块
如果条件表达式不成立,执行else对应的代码块
if 代码块叫真区间
else 代码块叫假区间
Tom = "抠脚大汉"
if Tom == "女生":
print("跟她打招呼")
print("约她出去玩")
print("请她吃东西")
print("以后常联系")
else:
print("给你一榔头")

rich = True
if rich == True:
print("上午刚认识,下午就结婚")
else:
print("你是个好人")
input 函数的功能: 等待用户输入字符串,接受的数据是字符串
name = input("今天是个好日子")
print(name , type(name))
练习
网站登录功能:提示用户输入对应的用户名和密码
如果用户名是admin,密码是12345提示用户,登录成功
否则提示,登录失败
username = input("请输入您的用户名")
password = input("请输入您的密码")
if username == "admin" and password == "12345":
print("登录成功~")
else:
print("登陆失败~")
3. 多项分支
多项分支就是从多个条件表达式中选一个
代码语法:
if 条件表达式1:
code1
code2
elif 条件表达式2:
code3
code4
elif 条件表达式3:
code5
code6
else:
code ...
如果条件表达式1成立,返回True,执行对应的代码块,反之则向下执行
如果条件表达式2成立,返回True,执行对应的代码块,反之则向下执行
如果条件表达式3成立,返回True,执行对应的代码块,反之则向下执行
......
直到最后,任何条件都不满足,执行else这个分支的代码块
elif 可以出现0次或者多次
else 可以出现0次或者一次
rich = True
home = True
benz = True
cool = True
strong = True

if rich == True:
print("我要嫁给你1")
elif home == True:
print("我要嫁给你2")
elif benz == True:
print("我要嫁给你3")
elif cool == True:
print("我要嫁给你4")
elif strong == True:
print("我要嫁给你5")
else:
print("你是个好人")
4. 巢状分支
巢状分支: 就是单项分支,双项分支,多项分支的互相嵌套
rich = True
home = True
benz = True
cool = True
strong = True
if rich == True:
if home == True:
if benz == True:
if cool == True:
if strong == True:
print("今天我要嫁给你,谁说都不好使")
else:
print("恭喜你,成为我的备胎")
else:
print("你可以去韩国整容,整成金正恩再来")
else:
print("你是个好人,不送了,老弟")
2. 循环结构
while 循环
特点: 减少冗余的代码,提示代码的效率
代码语法:
while 条件表达式:
code1...
code2...初始化一个变量
写上循环的判断条件
自增自减的变量值
# 打印 1 ~ 100
i = 1
while i <= 100:
# 在这个位置写要执行的逻辑
print(i)
i += 1 # i = i + 1
"""
代码解析:
初始化一个变量 i = 1
第一次循环
i = 1 , i <= 100 , 成立返回True, 执行循环
print(1)
i += 1 => 2
第二次循环
i = 2 , i <= 100 , 成立返回True, 执行循环
print(2)
i += 1 => 3
第三次循环
i = 3 , i <= 100 , 成立返回True, 执行循环
print(3)
i += 1 => 4
......
当i = 101时,判断i <= 100 不成立,返回False,不执行循环
循环结束...
"""
练习
计算1~100的累加和
方法一
i = 1
total = 0
while i <= 100:
# 把所有的i值全部累加到total这个变量中
total += i # total = total + i
i += 1 # i = i + 1
print(total)
"""
代码解析:
第一次循环
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 = 100 , i <= 100 , 成立执行循环
total = 0 + 1 + 2 + 3 + 4 + ... + 100 = 5050
当i = 101时,不满足循环条件,直接跳出,循环结束
"""
方法二
i = 1
total = 0
sign = True
while sign:
total += i # total = total + i
i += 1
# 手动加上终止循环的条件
if i == 101:
sign = False
print(total)
# 拓展 => 死循环
# while True:
# print(111)
3. 单项循环的练习
1.打印一行十个小星星
"""
help(print)
print("",end="") print("",end="") print("",end="") print("",end="") print("",end="") print("",end="") print("",end="") print("",end="") print("",end="") print("",end="")
"""
# **********
# 1.初始化变量
i = 0
# 2.循环判断的条件
while i < 10:
# 4.写上自己的代码逻辑
print("*" , end="") # 打印小星星
# 3.自增加1
i += 1
print("<====================>")
2.通过打印变量实现一行十个小星星
# **********
# 字符串的拼接
var1 = "你在哪"
var2 = "我在迪士尼"
# 表达方式1
res = var1 + var2
print(res)
# 表达方式2
var1 += var2
print(var1)
# 循环10次,拼接10个小星星
i = 0
strvar = ""
while i < 10:
strvar += "*" # strvar = '' + "*" / "*" + "*" / "**" + "*" / "*********" + "*" /
i += 1
print(strvar)
print("<====================>")
3.打印一行十个小星星,奇数个为★ , 偶数个位☆
★☆★☆★☆★☆★☆
"""
任意数和n取余,范围是: 0 ~ (n-1)
n = 2
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
0 ~ 1
n = 5
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
0 ~ 4
"""
i = 0
while i < 10:
if i % 2 == 0:
print("★" , end="")
else:
print("☆" , end="")
i += 1
print("<====================>")
4.一个循环打印十行十列的小星星
"""0123456789********** 0~910 11 12 13 14 15 16 17 18 19* * * * * * * * * * 10~19**********20~29**********30~39**********40~49**********50~59**********60~69**********70~79**********80~89**********90~99"""
i = 0
while i < 100:
# 打印星星
print("*" , end="")
# 打印换行
if i % 10 == 9:
print()
i += 1
print("<====================>")
5.一个循环打印十行十列隔列换色的小星星
""" ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ ★☆★☆★☆★☆★☆ """
i = 0
while i < 100:
# 打印星星
# print("*" , end="")
if i % 2 == 0:
print("★" , end="")
else:
print("☆" , end="")
# 打印换行
if i % 10 == 9:
print()
i += 1
print("<====================>")
6.一个循环打印十行十列隔行换色的小星星
""" ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ """
"""
任意数和n进行地板除,会出现n个相同的数字
n = 2
0 // 2 = 0
1 // 2 = 0
2 // 2 = 1
3 // 2 = 1
4 // 2 = 2
5 // 2 = 2
n = 6
0 // 6 = 0
1 // 6 = 0
2 // 6 = 0
3 // 6 = 0
4 // 6 = 0
5 // 6 = 0
6 // 6 = 1
7 // 6 = 1
8 // 6 = 1
9 // 6 = 1
10 // 6 = 1
11 // 6 = 1
12 // 6 = 2
......
0 ~ 9 // 10会出现10个0
10 ~ 19 // 10会出现10个1
20 ~ 29 // 10会出现10个2
......
90 ~ 99 // 10会出现10个9
由上述推论可知,地板除的值的范围是: 0 ~ 9
"""
i = 1
while i < 100:
# 打印星星
if i // 10 % 2 == 0:
print("★" , end="")
else:
print("☆" , end="")
# 打印换行
if i % 10 == 9:
print()
i += 1