浪子无涯

导航

 

#运算符(赋值运算符)

#1、赋值运算符:+、-、*、/、//、=、+=、**、%

 

num=int(input("num="))    #等于

num+=2

print("num+2=",num)    #加  num=num+2

 

num-=2

print("num-2=",num)    #减  num=num-2

 

num*=2

print("num*2=",num)    #乘  num=num*2

 

num/=2

print("num/2=",num)    #除  num=num/2

 

num//=2

print("num//2=",num)   #整除  num=num//2

 

num**=2

print("num**=",num)   #乘方  num=num**2

 

num%=2

print("num%=",num)    #模  num=num%2

 

#2、逻辑运算符: and:且, or:或者, not:非  优先级:not>and>or

#

#条件1 and 条件2   两个条件都成立,结果才是True,其他是False

#a=6>5 and 4>3

print("6>5 and 4>3:",6>5 and 4>3)

print("6>5 and 4<3:",6>5 and 4<3)

 

#条件1 or 条件2    两个条件都不成立,结果是False,其他是True

print("6>5 or 4>3:",6>5 or 4>3)

print("6>5 or 4<3:",6>5 or 4<3)

print("6<5 or 4<3:",6<5 or 4<3)

 

#not 条件

print("not 6>5:",not 6>5)

 

#优先级:not>and>or

print("6>5 or 4<3 and 6<4:",6>5 or 4<3 and 6<4)        #and>or

print("6<5 or 4>3 and not 6>4:",6<5 or 4>3 and not 6>4)

 

#短路原则:and 如果条件1是False,则and结果是False,后面条件2不做计算。

#          or  如果条件1是True,则or结果是True,后面条件2不做计算。

 

#3、while语句

 

a=1

while a<100:

    a+=1

    if a%2==0:#输出1-100中的偶数

        print(a)

#猜年龄

age=30

flag=True

while flag:

    user_age=int(input("user_age is :"))

    if user_age==age:

        print("Yes!")

        flag=False

    elif user_age>age:

        print("It's bigger")

    else:

        print("It's smaller")

print("You got it ! ")

 

#4、break 终止、中断; continue 跳过当次继续执行

a=1

while a<20:

    a+=1

    if a%2==0:#输出1-100中的偶数

        if a==12:

            break #执行到偶数等于10,终止循环

        print(a)

a=1

while a<20:

    a+=1

    if a%2==0:#输出1-100中的偶数

        if a==10:

            continue #执行到偶数等于10跳过,继续执行

        print(a)

#5、while  else 语句,是当执行完while循环才执行else内容,如果没执行完不执行

a=1

while a<20:

    a+=1

    if a%2==0:#输出1-100中的偶数

        if a==12:

            break #执行到偶数等于10,终止循环

        print(a)

else:

    print("else is OK!") #终止while循环,无法输出

 

a=1

while a<20:

    a+=1

    if a%2==0:#输出1-100中的偶数

        if a==10:

            continue #执行到偶数等于10跳过,继续执行

        print(a)

else:

    print("else is OK!")

 

#6、end=""的作用,end=' '意思是末尾不换行,加空格。表示这个语句没结束。

print("abc")#print默认是打印一行,结尾加换行。

print() #等价于 print(end="\n")

print("ddd")

print("abc",end="")#末尾不换行,加空格。表示这个语句没结束。

print("ddd")

#不可见换行符  \n(linuex) \r\n(window里面) \r(mac)

posted on 2021-01-08 17:58  浪子无涯  阅读(104)  评论(0)    收藏  举报