Python day02 课堂笔记

      今天是第二天学习Python课程,主要从格式化输出,逻辑运算,编码,数据类型 这几个方面来学习。

1.格式化输出:

  % : 占位符

  %s:字符串

  %d:数字

  注意:

  在格式化的输出中,如果要输出%(因为%作为占位符),要写%% 。才能在打印中显示%(显示后面的%,前面的%作为转义的作用)

 

2.逻辑运算:

  and , or , not . 【优先级:()> not > and >or 】,同一优先级从左至右依次运算。

  1)and :两边都是真才是真

  2)or   :一个为真就是真

  3)not :非

  

  1】x or y  x为非零(真),则返回x

    int -> bool :

       非零转化为布尔:True

       零转化为布尔   : False

      bool -> int:

       True : 1

       False : 0

  2】x and y   x为非零(真),则返回y

 

【例题】eg.

  •   1.  1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8  False
  •   2.  1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6   False
  •   3.   1 or 2   1
  •   4.   3 or 2   3
  •   5.   0 or 2   2
  •   6.   1 and 2   2
  •   7.   0 and 2 0
  •   8.   2 or 100 or 3 or 4  2
  •   9.  6 or 2 > 1    6
  •    10.  3 or 2 > 1  3
  •    11.  0 or 5 < 4  False
  •    12.  5 < 4 or 3    3
  •    13.  2 > 1 or 6    Ture
  •    14.  3 and 2 >1  Ture
  •    15.  0 and 3 >1  0
  •      16.  2 > 1 and 3 3
  •    17.    3 > 1 and 0    0
  •    18.  3 > 1 and 2 or 2 < 3 and 3 < 4 and 4 or 3 > 2  2

 

3.编码:

  1. ASCII  :  只能显示英文,特殊字符和数字

  2.Unicode :  万国码,最开始16位,显示不够,后来32位(占4个字节),缺点是占用的资源多。

  3.UTF-8   :   最少用一个字节,8位表示一个英文。欧码 16位 ,亚洲24位

  4.JBk     :   中国国产,只能用于中文和ASCII码中的文字

 注意:

  GBK 和 UTF-8 不能直接转化,都需要通过Unicode来进行转码。

 

4.数据类型:

  1. int  :  1,2,3…….,用于计算

  2.bool  :   True ,False ,用于判断

  3.str  :   y用引号引起来的数据,存储少量数据,进行操作

  4.list  :   [1,2,3,’张三’,’1234′] , 用来存储大量数据

  5.元组    :   (1,2,3……,’张三’),又称只读列表

  6.字典 :    dict , { ‘name’ : ‘云姐’ , ‘age’ : 18 } ,{ ‘云姐’ :[ …任何形式… ] } , 键值对形式存在,大量的关系型数据存储在字典里

  7.集合 ;    { 1, 2, 3 ,,,,,, }

 

5.课堂练习:

  1)写代码:计算 1 – 2 + 3 ……. + 99 中除了88之外所有数的总和?

 

i = 1
sum = 0
while i <= 99:
    if i == 88:
        i += 1
        continue
    elif i % 2 == 0 :
        sum = sum - i
    elif i % 2 != 0:
        sum = sum + i
    i += 1
print(sum)

 

 

 

   2)写代码:计算 1 – 2 + 3 ……. – 99 中除了88之外所有数的总和?

 

i = 1
sum = 0
while i < 100 :
    if i == 88:
        i += 1
        continue
    elif i == 99:
        sum = sum - i
    elif i % 2 != 0:
        sum = sum + i
    else:
        sum = sum - i
    i += 1
print( sum )

 

   3)用户登陆(三次输错机会)且每次输错误时显示剩余错误次数 (提示:使用字符串格式化)

username = "deng"
password = "4312"
i = 3
while i > 0 :
    name = input("username:")
    word = input("password:")
    if name == "deng" and word == "4312":
        print("输入正确...")
        break
    else:
        i -= 1
        print("输入错误,您还有"+str(i)+"次机会")
print("正在退出...")

 

    原文作者:如果可以,我选择忘记
    原文地址: https://www.cnblogs.com/if-it-is-possible/p/11395685.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞