python 基础复习 02
Posted pioneerlee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 基础复习 02相关的知识,希望对你有一定的参考价值。
#逻辑运算
# and or not
#优先级, () > not > and > or 如果同一优先级 从左到右依次计算
# print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
#先看 and 三个and 依次是 T or T or F
#然后再看 or T 和 T 是 真 T or F 最后是真
# print(3 > 4 or 4 < 3 and 2 == 2)
# print(1 < 2 and 3 < 4 or 1 > 2)
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
#F or F and T or F
#F or F or F
"""
x or y x为非零 则返回x 若x为0 则返回y
int 转换成 bool 非零转换成bool 为 True 0 转换成bool 是False
"""
# print(1 or 2) 1
# print(3 or 2) 3
# print(0 or 2) 2
# print(100 or 3) 100
# print(0 or 23) 23
# print(bool(2)) # True
# print(bool(-2)) # True
# print(bool(0)) # False
#bool ---> int
# print(int(True)) # 1
# print(int(False)) # 0
"""x or y x 为 True 则返回x"""
# print(2 or 100 or 3 or 4) # 2
"""x and y x True , 则返回 y """
# print(1 and 2 )
# print(0 or 4 and 3 or 2) # 3
# print(1 > 2 and 3 or 4 and 3 < 2) # False
# print(2 or 1 < 3 and 2) # 2
""" x or y x 为 True 则返回 x """
""" x and y x 为 True 则返回 y """
# print(6 or 2 > 1) # 6 先看 > <
# print(3 or 2 > 1) # 3
# print(0 or 5 < 4) # False
# print(5 < 4 or 3) # 3
# print(2 > 1 or 6) # True
# print(3 and 2 > 1) # True
# print(0 and 3 > 1) # 0
# print(2 > 1 and 3) # 3
1 # 小练习 用户登陆 有三次输入机会 2 username = "蜘蛛" 3 password = "123" 4 i = 3 5 while i > 0: 6 user = input("请输入您的账号:") 7 i -= 1 8 if user == username: 9 passwd = input("请输入您的密码:") 10 if passwd == password: 11 print("验证成功,正在登陆...") 12 print("""恭喜你登陆成功! 13 用户名:%s 14 密码:%s 15 """%(user,passwd)) 16 break 17 else: 18 if i == 0: 19 print("您的机会已经没有了!gameover 下次见。") 20 answer = input("是否想再试试?Yes") 21 if answer == "Yes": 22 i = 3 23 print("密码错误,请重新输入:") 24 print("你还有"+str(i)+"次机会") 25 else: 26 print("请输入正确的用户名!") 27 if i == 0: 28 print("您的机会已经没有了!") 29 answer = input("是否还想再试试?请输入:Yes") 30 if answer == "Yes": 31 i = 3 32 print("您还有"+str(i)+"次机会") 33 else: 34 print("您该歇歇了...")
"""
数据类型:
int
方法:
bit_length
"""
# i = 4
# print(i.bit_length()) # 3
"""
bit_length
1 0000 0001 1
2 0000 0010 2
3 0000 0011 2
4 0000 0100 3
"""
"""
bool True False
int ---> str
i = 1
s = str(i)
print(s)
str ---> int (只能是数字 不能是字母)
s = "234"
a1 = int(s)
print(a1)
int ---> bool
i = 3
b = bool(i)
print(b) # True
--->(转化)
bool ---> int
True 1
False 0
str ---> bool
s = ""(空字符串)---->(False)
非空字符串都是True
s = " "
d = bool(s)
print(d) # True
a = ""
c = bool(a)
print(d) # False
"""
#字符串的索引与切片
s = "QAZWSXEDCrfvedc"
# s1 = s[1]
# print(s1) # A
# s2 = s[0]
# print(s2) # Q
# s3 = s[8]
# print(s3)
# s4 = s[0:]
# print(s4)
# s5 = s[0:4] # 顾头不顾尾
# print(s5)
# s6 = s[0:5]
# print(s6)
# s7 = s[-1]
# print(s7)
# s8 = s[-2]
# print(s8)
# s9 = s[0:-1] # 也是顾头不顾尾
# print(s9)
#
# q = s[:]
# print(q)
# s = "QAZWSXEDCrfvedc"
# a1 = s[0:7:3]
# print(a1)
# a2 = s[5:0:-1]
# print(a2) # 倒着来 XSWZA
# a3 = s[5::-1]
# print(a3) # XSWZAQ
# a4 = s[-1::-1]
# print(a4) # 字符串所有元素 倒着排列
"""
字符串的操作
"""
# s = "qazwsx"
# s1 = s.capitalize() # 首字母大写
# print(s1)
#
# s2 = s.upper() # 都大写
# print(s2)
# s3 = s2.lower() # 都小写
# print(s3)
1 #输入验证码,不区分大小写 2 3 s_str = "asdQAZ" 4 you_input = input("请输入’asdQAZ‘,不区分大小写") 5 if s_str.upper() == you_input.upper(): 6 print("输入成功") 7 else: 8 print("请重新输入")
# 大小写翻转
# s = "qazAZ"
# s3 = s.swapcase()
# print(s3)
#每个隔开(特殊字符或者数字)的单词首字母大写
# s = ‘lizy waws‘
# a1 = s.title()
# print(a1)
# w = "fade hello,edc/rew8tre_bvc"
# a2 = w.title()
# print(a2)
# center 居中 20 是总长度
# s = "hellopython" # ‘ hellopython ‘
# a1 = s.center(20, "#") # 总长度 20 用#号来填充
# print(a1)
# s = "lizyPYTHON"
# a1 = s.startswith("lizy") #以...开头
# print(a1) # True
#find 通过元素找索引,找不到返回-1
#index 通过元素找索引,找不到报错
# s = "lizyPYTHon"
# a1 = s.find("z") # 2
# a2 = s.index("S") # 报错
# print(a1)
# print(a2)
# strip左右一起删除 rstrip :从右开始删除 lstrip:从左开始删除
# s = " lizy "
# a1 = s.strip() # 去空格
# print(a1)
# s = " lizy "
# a2 = s.rstrip() # 去右边空格
# print(a2)
# s = "lizy ll l"
# a3 = s.count("l") # count 查看元素的个数
# print(a3)
# s = ":lizy:wufa:chenggong"
# a1 = s.split(":") # split 分隔符 ** str ---> list 字符串转换成列表
# print(a1) # 返回 [‘‘, ‘lizy‘, ‘wufa‘, ‘chenggong‘]
# format的三种玩儿法 格式化输出
# 第一种
# s = "我叫{}, 今年{}, 爱好{}, 再说一下我叫{}".format("蜘蛛", 23, "旅行", "蜘蛛")
# print(s)
#第二种 大括号里写索引 一定得按顺序写
# s = "我叫{0}, 今年{1}, 爱好{2}, 再说一下我叫{0}".format("蜘蛛", 23, "旅行")
# print(s)
#第三种 按照键值对的形式来写 顺序无所谓
# s = "我叫{name}, 今年{age}, 爱好{hobby}, 再说一下我叫{name}".format(name="蜘蛛", hobby="旅行", age=23)
# print(s)
#如果要替换一个的话 replace("原内容","替换后的内容",1) 加数字
#replace 替换 默认全部替换 replace("原内容","替换后的内容")
# s = "伍佰谓和句孙女士的举动看到你我孙女士"
# a1 = s.replace("孙女士", "王女士")
# print(a1)
以上是关于python 基础复习 02的主要内容,如果未能解决你的问题,请参考以下文章