day02基础&运算符笔记

今日概要

  1. 循环

  2. 字符串格式化

  3. 运算符

  4. 编码

  5. 博客&git

#根据分数按照等级输出 A B C D
#❌样本       
score = input("请输入分数:")
if val >= "90":
  print("level A")
elif val >= "80" and val <= "90":
	print("level B")
elif val >= "70" and val <= "80"
	print("level C")
else:
  print("其他") 
#1.没有转化为整型  因为字符串不能做大小比较  只能做长度比较或者完全一样
#2.不需要进行二次比对  and  因为Python顺序执行并且分数从高到低 所以不需要并列判断,但是若从地到高或者顺序捣乱  则需要and判断     
        
正解:        
score = input("请输入分数:")
val = int(score)
if val >= 90:
  print("level A")
elif val >= 80:
	print("level B")
elif val >= 70
	print("level C")
else:
  print("其他")          

message="""
欢迎致电10086
1.业务查询
2.流量查询
3.套餐办理
4.其他
"""
print(message)
index = input("请输入你想要办理的业务:")
index = int(index)#转型_________
if index ==1:
  print("业务查询")
  content="""
  1.电话服务请按1
  2.人工服务
  """
  print(content)
  count = inpu1t("请输入你的服务:")
  count = int(count)
  if count == 1:
       print("电话服务")
  else:
       print("退出")
elif index == 2:
   print("流量查询")
elif index == 3:
   print("套餐办理")
else:
  print("退出")
  
  #麻烦所以有了 while

1.循环

  • while基本结构

    #循环打印 
    while True:
        print("life is shot , we should use python")
    
    #循环输出1-10
    count = 1
    while count  <= 10:
      print(count)
      count += 1
    
    
    
    #循环输出1 2 3  4 5  6 7 8 9 10
    #方案一
    count =1
    while count <= 10:
      if count != 7:
        print(count)
      count = count + 1
      
    #方案二
     count = 0
     while count < 10:
      count = count + 1
      if count == 7:
        count = 8
      print(count)
     
    #方案三
    count =1
    while count<= 10:
      if count == 7:
        pass
      else:
        print(count)
      count = count + 1 
        
      
    
    
  • break关键字

    #break 是跳出当前循环,
    # while True:
    #     print("hi")
    #     while True:
    #         print("下午好")
    #         break
    #
    
    while True:
        print("hi")
        while True:
            print("下午好")
            break
        break
    # hi 下午好
        
    # 方案四---------------------------------
    # 循环输出1-10
    # count =1
    # while count <=10:
    #     print(count)
    #     if count == 10:
    #         break
    #     count += 1
    
    
     
     
    
  • continue关键字

    #continue 表示本次循环 continue之后的语句都不再执行 而是重新回到while的条件位置
    
    #方案五——----------------------------
    count=1
    while count<=10:
        print(count)
        count = count + 1
        if count == 7:
            count=count+1
            continue
            
    
  • while else

    • 当while条件不满足时候else里面内容才会执行 或者条件=falsed 时候
    • break除外
    # #while else :当while条件不满足时候else里面内容才会执行 或者条件=falsed 时候
    # count = 1
    # while count <= 10:
    #     count+=1
    #     print(count)
    # else:
    #     print("end")
    #1-10
    #end
    
    #2.
    count=1
    while count <=10:
        print(count)
        if count==10:
            break
        count += 1
    else:
        print("end")
    #1-10 不会输出end  因为break是外界因素迫使跳出while循环 并不是while条件不满足
    
    

2.占位符(字符串格式化)

  • 占位符

    1. %s

      #var="value"
      #template="%s"%(var,)模板 括号里面最后一定要加逗号
      name="gao"
      sex="F"
      age=18
      template="名字:%s;性别:%s;年龄:%s"%(name,sex,age,)
      print(template)
      
      ##名字:gao;性别:F;年龄:18
      
      #直接赋值
      # template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
      # print(template)
      
    2. %d

      #var="value"
      #template="%s"%(var,)模板 括号里面最后一定要加逗号
      name="gao"
      sex="F"
      age=18
      template="名字:%s;性别:%s;年龄:%d"%(name,sex,age,)
      print(template)
      
      ##名字:gao;性别:F;年龄:18
      
    3. %%

      当模板的内容涉及到%时候  需要多写一个% 不然报错
      name="gao"
      template="名字:%s的电量是100%%"%(name,)
      print(template)
      #名字:gao的电量是100% 
      
      
    4. 练习

      name=input("请输入名字:")
      age=input("请输入年龄:")
      sex=input("请输入性别:")
      #模板新样式
      message="""
      -------info of Gao-------
         姓名:%s    #占位符
         年龄:%s    #占位符
         性别:%s
      ---------end-------------
         """
      print(message%(name,age,sex,))
      
      #请输入名字:Gao
      #请输入年龄:18
      #请输入性别:F
      
      -------info of Gao-------
           姓名:Gao
           年龄:18
           性别:F
      ---------end-------------
      

3.数据类型转换

  • 字符串转数字

    content="123"
    num=int(content)
    
    
    
  • 数字转字符串

    content=666
    num=str(content)
    
    
  • 布尔转换

    • 整数转bool 0==false 其余都为 true
    • 字符串bool —> " " == false 其余都为true
    #字符串转布尔值
    v1 = ""
    v2 = bool(v1)
    print(v2)
    #false
    
    # 数字转布尔值
    # v1 = 0
    # v2 = bool(v1)
    # print(v2)
    #false
    
    # 布尔值转换其他 
    # v1 = True
    # v2 = str(v1) 
    # print(v2)
    #"1"
    
    

4.运算符

  1. 算术运算符

    • 取模%
    打印1~100基数
    #模2余1的为基数
    #以1 3 5 7 9结尾的为奇数
    # count =1
    # while count<100:
    #     print(count)
    #     count+=2
    #
    
    count =1
    while count<100:
        if count %2 == 1:
            print(count)
        count+=1
    
    
    
    • ** 幂次方

      2**8
      
    • // 取整

      9//2
      
  2. 比较运算符

  3. 赋值运算符

    c += 1  c **= a 等价 c = c**a
    
  4. 逻辑运算符:

    • and: 取决于遇到的值是否为false

      v1 = 1 and 9
      print(v1)
      # 9
      
      v2 = 1 and 0
      # 0
      
      v3 = 0 and 7
      #0
      
      v4 = 0 and ""
      #0
      #如果第一个值转换成布尔值是true,则value= 后面的值
      #如果第一个值是false,则value= 第一个值
      #如果有多个and条件,则从左到右依次进行上述流程
      
    • or : 取决于遇到的值是否为真

      面试:
      value = 0 or 9
      print(value) ## 9
      
      value = 1 or 9
      print(value) ## 1
      
      value = 0 or ""
      ## " " 打印空
      
      value = 0 or 9 or 8
      ## 9
      
      ## 第一个值转换成布尔值 如果是真,则value=第一值
      ## 如果是假 则value=第二个值
      ##如果有多个or条件  则从左到右依次执行
      
      
      
    • not

    • and 、or

      #在没括号的时候优先级先看and 再看or 
      v1 = 1 and 9 or 0 and 6
      v1= 9 or 0
      #9
      
    • 优先级

      ()>not>and >or

  5. 补充

    • in

      value="我是中国人"

      #判断”中国“是否在value所指代的字符串中
      v1="中国” in value
      
      #列示
      content = input("请输入“)
      if ”退钱“ in content:
      print("退钱")
      
  6. 比较运算符优先级> 逻辑运算符

5.编码

1.编码扩展
  • Unicode:内存运算时候

    • Ecs2:两个字节表示
    • Ecs4:四个字节表示
  • utf-8:

    网络传输 硬盘存储

    中文三个字节表示

  • utf-16:

  • ASCII

  • GBK:亚洲通用 中文两个字节表示

  • GB2312:中国大陆编码

2.单位
8bit = 1byte 1024byte = 1KB 1024KB = 1MB 1024MB = 1GB 1024GB = 1TB 1024TB = 1PB 1024TB = 1EB 1024EB = 1ZB 1024ZB = 1YB 1024YB = 1NB 1024NB = 1DB 常用到TB就够了

6.git