day 2:格式化输出,基本运算符

今日主要内容
1. 循环. (while循环)
  结构:while 条件:
      代码块(循环体)
  执行流程:
  1. 判断条件是否为真. 如果真. 执行代码块
  2. 再次判断条件是否为真......
  3. 当条件为假.执行else 跳出循环. 循环结束

# 计数
count = 1
while count <= 8:
    print("你是alex么?")
    print("你才是太白")
    count = count + 1
# 无限循环,让用户尽情的喷. 输入q退出
while True:
    s = input("请开始喷:")
    if s == 'q':
        break   # 停止当前循环
    # 过滤掉马化腾
    if  "马化腾" in s: # 在xxx中出现了xx
        print("你输入的内容和草泥马有一拼. 不能输出")
        continue    # 停止当前本次循环. 继续执行下一次循环
    print("喷的内容是:"+s)
# 1+2+3+4+5+6+7+8....+100 = ?
count = 1
# 准备一个变量
sum = 0
while count <= 100:
    # 累加到sum中
    sum = sum + count   # 把sum中的值(之前运算的结果)和当前数的数相加
    count = count + 1
print(sum)
# 输出1-100所有的奇数.
count = 1
while count <= 100:

    if count % 2 != 0:    #判断是否是奇数
        print(count)

    count = count + 1

2. 格式化输出

print("%s今年%s岁,爱好是%s,性别是:%s"  %  (name,age,hobby,gender))

  %s: 字符串的占位符, 可以放置任何内容(包含数字)
  %d: 数字的占位符,对应的数据必须是int类型,否则程序会报错.使用时,需要进行类型转换.

int(str)    #字符串转换成int
str(int)    #int转换成字符串
            #想转换成什么类型就用什么类型把目标包括起来

  注意: 如果你不想转换,也可以全部用%s;

      如果字符串中已经使用了占位符,则后面的百分数必须加2个%,例如%%2;如果没有继续使用%2

name = "alex"
print("%s已经喜欢了沙河%%2的女生" % name)  # 如果字符串中有了占位符. 那么后面的所有的%都是占位. 需要转义
print("wuse喜欢了昌平%5的女生") # 这句话中没有占位符. %还是%

3. 运算符

  逻辑运算:          (运算顺序:  括号=>比较大小=>not =>and => or)
  and 并且的意思. 左右两端的值必须都是真. 运算结果才是真
  or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假
  not 非的意思. 原来是假. 现在是真. 非真即假, 非假既真
  break 结束循环. 停止当前本层循环
  continue 结束当前本次循环. 继续执行下一次循环

计算机常用的数据
print(2**0) # 1
print(2**1) # 2
print(2**2)
print(2**3)
print(2**4)
print(2**5)
print(2**6)
print(2**7)
print(2**8)
print(2**9)
print(2**10)
赋值运算.
a = 10
a += 20     # a = a + 20
print(a)
先算括号, 然后算not, 然后算and , 最后算or
print(3>4 or 4<3  and  1==1) # False
print(1 < 2  and  3 < 4 or 1>2  ) # T
print(2 > 1  and  3 < 4 or 4 > 5 and  2 < 1)    # T
print(1 > 2  and  3 < 4 or 4 > 5 and  2 > 1  or 9 < 8) # F
print(1 > 1  and  3 < 4 or 4 > 5 and  2 > 1  and  9 > 8 or 7 < 6)
print(not  2 > 1  and 3 < 4  or 4 > 5  and 2 > 1  and 9 > 8  or 7 < 6) # F
#x or y 如果x==0 那么就是y, 否则是x
print(1 or 2)   # 1
print(2 or 3)   # 2
print(0 or 3)   # 3
print(0 or 4)   # 4
#x and y, x为真,就是y;x为假,是x
print(1 and 2)  # 2
print(2 and 0)  # 0
print(0 and 3)  # 0
print(0 and 4)  # 0
print(2 > 3 and 3)      # false相当于0
print(2 < 1 and 4 > 6 or 3 and 4 > 5 or 6)

4.基本运算符

  1.算数运算

  +   加   10+20=30

  -     减    20-10= 10

  *    乘   10*10=100

  /    除法     3/2=1.3

  %  取余   3/%2=1

  //   商取整   3//2=1

  2.比较运算

  =   等于

  !=   不等于      <>    也是不等于

  >    大于

  <    小于

  >=   大于等于

  <=  小于等于

  3.赋值运算

  a=10   #将10赋值个a

  4.逻辑运算

  not:   真假反过来

  and : 同真为真,有假为假

  or  :   有真为真,全假为假

  5.举例

  1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  ==> True
  not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ==>Flase

5.补充
  1.while...else
count  = 1
while count  <= 10:
    print( count)
    count = count + 1
    if count == 5:
        break   # 彻底停止循环. 不会执行后面的else,pass不表示任何内容,只是占位
else:   # while条件不成立的时候执行
    print("这里是else")

  2. in和not in

可以判断xxx字符串是否出现在xxx字符串中

content=input("请输入你的评论:")
if "苍老师" in content or "麻辣" in content:
    print("你输入的内容不合法")
else:
    print("评论成功")
 


预习:
1.编码
 2. 数据类型
 3. str 索引切片. 常用方法

猜你喜欢

转载自www.cnblogs.com/mwj-blog1/p/9259174.html