python学习day2

1、格式化输出

  %s,%d 表示占位

  %%目的是转义,表示为百分比的意义

name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%d 身高 %s 学习进度为3%%s" %(name,int(age),height) #注意%d表示按整型数据输出,所以需要将输入的字符串转为整形
print(msg)
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入工作:')
hobbie = input('你的爱好:')

msg = '''------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------''' %(name,name,int(age),job,hobbie)
print(msg)

2、while else

  当while的循环没有被break打断时走else,当被打断时不走else

#循环执行完毕,执行else结果
count = 0
while count <= 5 :
    count += 1
    print("Loop",count)
else:
    print("无法执行循环")
print("-----out of while loop ------")


# 当count==3时,跳出循环,不会执行else结果
count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

else:
    print("无法执行循环")
print("-----out of while loop ------")

 3、运算符

  • 算数运算:+ 、- 、* 、/ 、%(取余)、 **(幂次方)、 //(整除)
  • 比较运算:==、!=(等同于<>)、>、<、>=、<=、x is y、x is not y、x in y、x not in y
  • 赋值运算:=、+=、-=、*=、/=、%=、 **=、 //=
  • 逻辑运算:and、or、not
    #and or not
    #优先级,()> not > and > or
    print(3>4 or 4<3 and 1==1)    #结果是 F
    print(1 < 2 and 3 < 4 or 1>2)   # 结果是T
    
    #int  ----> bool   非零转换成bool True   0 转换成bool 是False
    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,x为False则返回y
    print(1 or 2)  # 1
    print(0 or 2)  # 2
    
    # x and y ;与or的情况相反,x 为True,则返回y,x为False则返回x
    print(1 and 2)  # 2
    print(0 and 2)  #0
    print(3 > 1 or 2 and 2)  #True

     

 

posted @ 2020-12-01 18:16  暴躁的产品  阅读(41)  评论(0)    收藏  举报