python分支结构、循环结构学习小结

什么是表达式?

表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列

表达式优先级,例如乘法大于加法,在程序中,同一级别的优先级从左到有开始

 

1 or 2

1

1 and 3

3

列表是序列、元组是序列


not:>and>or

>>> not a or b+2==c

False

>>> not a or( b+2==c)

False

>>> (not a) or ((b+2)==c)

False

>>> not (a or b)+2==c    //not 不正确变为 True

False

 

 

元素优先级

 

在文件中编译python代码

 

常用开发工具,pycharm,vscode,subline

IDE  集成开发环境

 

流程控制语句

条件控制     循环控制     分支

If else     for     while      switch

 

注释的方法#单行注释

这里为你要注释的代码块

…  

快速注释 ctrl + /

多行注释 alt + shift + a


判断结构示例

Mood = False

If mood:

              Print()

Else

              Print()

循环        while

condition = True

     while condition:

              printf(“I AM WHILE”)

         结果会一直打印,强制结束用ctrl + z(PS:vscode);

 

while 语句示例

 conter =1

     while conter<=10:

         conter+=1

     print(“conter”)    //从2打印到10

         else:

         print(“end”)

 

“while” 达到目的执行else语句,while在递归中用的多

 

     for语句

              for用与遍历/循环 序列 或者集合,字典

 

     a=[‘a’,’b’,’c’,’d’]

         for x in a:

         print(x)

 

    

a=[[‘a’,’b’,’c’,’d’],(1,2,3)]

         for x in a:

              for y in x:

         print(y,end=” ”)

              else:

                   print(“fruit is gone”)

 


打印所有列表和子列表的所有的结果

     a=[1,2,3]

         for in x a:

              if x==2:

                   break 

     print(x)

 

break处可以与continue之间可以互换,但是结果不同

break打印1     //中止循环,break直接跳出该个循环

continue 打印1,3        //跳过2继续,跳过该循环,进行下一个循环

 

a=[1,2,3,4……….]

for x in range(0,10):

     print(x)

实现打印0~9  10个元素

for x in range(0,10,2):

     print(x,end=” | ”)

打印 0 | 2 | 4 | 8   递增

for x in range(10,0,-2):

递减

 

 

 

猜你喜欢

转载自blog.csdn.net/caidewei121/article/details/81433579