day6

默写

# 什么是可变数据类型什么事不可变数据类型:
#   不可变的数据类型: (可hash) 当数据值改变时 id()一定跟着改变,   不可变数据类型.
#       例如: 字符串 整数 浮点数 布尔值
#   可变数据类型: (不可hash) 当数值改变时id()可以不改变> 可变数据类型
#       例如: 列表 字典
# 字典 可以存取多个值,并且每个值都有唯一对应的key
#   key:必须是不可变类型
#   value:可以是任意数据类型
# 逻辑运算 not and or
# not 真即假假即真
# and 全真为真一假为假
# or 一真为真,全假为假
# 短路运算
# 在计算 a and b 时,如果 a 等价于 False,整个结果必定为 False,因此返回 a;
# 如果 a 是 True,则整个计算结果必定取决与 b,因此返回 b。
# 在计算 a or b 时,如果 a 等价于 True,整个计算结果必定为 True,因此返回 a;
# 如果 a 是 False,则整个计算结果必定取决于 b,因此返回 b。
# 所以Python解释器在做布尔运算时,只要能提前确定计算结果,它就不会往后算了,直接返回结果。

# 身份运算  is is not
# 成员运算  in not in

# 什么是流程控制
# 流程控制即控制流程  程序的执行流程分为三种结构: 顺序  分支 循环
# 分支结构
# 什么是分支结构:
#   根据条件判断的真假去执行不同分值对应的子代码
# 为什么要用分支结构:
#   使用分支结构让计算机具有像人一样的判断能力:
# 如何使用分支语句
#   if
# 语法一
if True:
    print('智慧树上智慧果')
    print('智慧树下你和我')
#   ps : python 用相同的缩进来标识一组代码块,同组代码块从上到下依次运行.
#       (代码块缓存机制:有兴趣自己了解)
#        条件可以是任意语句但是结果必须返回True or False
# bool : 显性bool True  False
#      : 隐性bool 所有数据类型
# 语法二.
hige = int(input('请输入你的身高').strip())
if  hige<160:
    print('天王盖地虎,提莫一米五')
elif hige >180:
    print('宝塔镇河妖,段友吊缠腰')
else:
    print('完美身高')
View Code

字符串

# 数字类型
# 整形
# 浮点型
#复数类型


string = 'tag local tools'
# 字符串常用方法
# 1
# 索引取值
print(string[0])
print(string[1])
print(string[-1])
# 2
# 字符串切片
print(string[::-1])
print(string[:])
# 3 取长度字符的个数
# len ()
print(len(string))
# 4 去除两端的特定字符
# strip()
print(string.strip('tals'))
# lstrip() 去除左边
# rstrip() 去除右边
# 默认删除空格
# 5 split()  rsplit()切分 将字符串切分为列表(以指定的符号切分)
print(string.split(' ',1))   # 以什么切分 切分几次  从左切
print(string.rsplit(' ',1))
# 6 循环
for i in string:
    print(i)
# 7 lower() 字符串全部大写 supper() 字符串全部小写
print(string.lower())
print(string.upper())

# 8 startswith() and end endswith()
# 判断 stratswith :是否以'特定符号'开头  endswith :是否以特定符号结尾
print(string.startswith('tag'))
print(string.endswith('ago'))

# 9 join 将列表以特定字符连接组成字符
#   字符串类型方法  所以 str.join()
# res = '_'.join([1,2,3,4,5])
# print(res)
# 报错
# ps : 列表内的元素必须全部是字符串 否则报错
ret = '_'.join(['1','2','2','3'])
print(ret)
# replace 替换('old', 'new',次数)
print(string.replace('a','0',1))
# 1
# 10 is 系列
string.isdigit()  # 判断 数字
string.isalnum()  # 判断 字母数字
string.isalpha()  # 判断字母
View Code

循环结构

# 流程控制之循环结构
# 什么是循环结构
# 循环结构是重复执行某段代码块的语句结构
# 为什么要有循环结构
# 模拟人类不断重复做某事是的能力
# 如何使用结构循环

# 语法一(while)
# while '条件':
#     print('代码块1')
#     print('代码块2')
# 运行步骤
#   1 如果条件为真那么依次执行:代码1 代码2
#   2 执行完毕后再次判断条件,条件为则再次执行代码1 2 再次判断 执行 直到条件为False为止.循环终止

# 例1 (初版)
# user_name = 'lee'
# user_password = '123456'
# inp_name = input('name>>:').strip()
# inp_pwd = input('password>>:').strip()
# if inp_name == user_name and inp_pwd == user_password:
#     print('ligon succsessful')
#     exit()
# else:
#     print('name or password erro')
# inp_name = input('name>>:').strip()
# inp_pwd = input('password>>:').strip()
# if inp_name == user_name and inp_pwd == user_password:
#     print('ligon succsessful')
#     exit()
# else:
#     print('name or password erro')
# inp_name = input('name>>:').strip()
# inp_pwd = input('password>>:').strip()
# if inp_name == user_name and inp_pwd == user_password:
#     print('ligon succsessful')
#     exit()
# else:
#     print('name or password erro')
# (改进版) (三次出错机会)
# user_name = 'lee'
# user_password = '123456'
#
# count = 0
# while count < 3:
#     inp_name = input('name>>:').strip()
#     inp_pwd = input('password>>:').strip()
#     if inp_name == user_name and inp_pwd == user_password:
#         print('ligon succsessful')
#     else:
#         print('name or password erro')
#     count +=1
# 语法二(while + break)
# while True :
#     print('代码块1')
#     print('代码块2')
#     break
#     print('代码块3')
# break :退出当前循环
#初稿(输入成功后退出)
# user_name = 'lee'
# user_password = '123456'
#
# count = 0
# while count < 3:
#     inp_name = input('name>>:').strip()
#     inp_pwd = input('password>>:').strip()
#     if inp_name == user_name and inp_pwd == user_password:
#         print('ligon succsessful')
#         break
#     else:
#         print('name or password erro')
#     count +=1
# 语法三 (while + cuntinue)
# cuntinue :跳过本次循环继续执行下次循环
#   本次循环中continue之后的代码不在执行
#
# count = 1
# while count <= 100:
#     count += 1   #   注意注意
#     if count % 2 == 1:
#         continue
#     else:
#         print(count)
#    count += 1  (出错 cuont+=1 一定要放在前面
# 语法四 while循环嵌套 + tag使用
# while 循环嵌套 : while : 结构中再次引用 while: 循环 就形成了嵌套
# tag :标记
# 目的在某一层次一次性退出所有循环.   实现方法 : 所有while 的条件都使用同一个变量 tag :设tag初始值为True 当tag 变为False时
# 程序退出所有循环
# # 再改 (登录成功后不退出可以输入命令)
# user_name ='lee'
# user_password = '123456'
# count = 0
# while count<3:
#     count +=1
#     inp_name = input('name:_').strip()
#     inp_password = input('password:-').strip()
#     if inp_name == user_name and user_password == inp_password:
#         print('login successful ')
#         while 1:
#             cmd = input('("q"==exit)>>:')
#             if cmd =='q':
#                 break
#             print(cmd)
#         break
#     else:
#         print('password erro')
# # 最终版 (改进 运用 while + tag)
# # while
# username = "lee"
# password = "123456"
# count = 0
#
# tag = True
# while tag:
#         count += 1
#     inp_name = input("请输入用户名:")
#     inp_pwd = input("请输入密码:")
#     if inp_name == username and inp_pwd == password:
#         print("登陆成功")
#         while tag:
#             cmd = input('>>: ')
#             if cmd == 'quit':
#                 tag = False  # tag变为False, 所有while循环的条件都变为False
#                 break
#             print('run <%s>' % cmd)
#         break  # 用于结束本层循环,即第一层循环
#     else:
#         print("输入的用户名或密码错误!")

# 语法五 while + else
# count = 0
# while count<3:
#     count +=1
#     print('代码1')
#     print('代码2')
#    # break
# else:
#     print('运行')
# 当 while循环没有中途跳出那么执行 else: 反之不执行
View Code
原文地址:https://www.cnblogs.com/lee1225/p/12446000.html