Python的基本语句,,if条件语句(1)i


if条件语句

(1)if 条件的判断 : (冒号)

if 1==1 :

print (‘真的啥也不会‘)

else:

print (‘以后就会了‘)

### 代码块的介绍:在同一列下的为代码块,如上,if下的print就是一个代码块。

###一般为空4格为标识,或直接按TAB键。

(2)if的嵌套:

if n==1 :

  print(‘哈哈‘)

  if n==2 :

    print(‘呵呵‘)

  else:

    print(‘hehe‘)

else:

  print(‘haha‘)

##注意代码块。。。。。

(3)if elif 的用法:

if n==1 :

  print (‘你好‘)

elif n==2 :

  print (‘年后‘)

elif n==3:

  print (‘真香‘)

else:

  print (‘sb‘)

如果想要直接else 则要:

if n==1 :

  pass 关键字 :不执行当前 直接跳过。

else :

  print(‘sb‘)

while的使用方法:

(1)while count ==1:

  print("哈哈")

使用时同样需要注意代码块。

(2)while else 的使用

while count <10:

  print("haha")

count =count +1

else:

  print("hehe")
使用时同样注意代码块

(3)continue break

while count<10:

  if count==7:

    count=count+1

    print(‘haha‘)

    continue

  else:  

    print(‘haha‘)

    count=count+1

其中 ,continue的用法就是当满足if时,遇到continue后返回if,继续执行。 即当=7时,continue后到else输出haha

break时, break的作用是跳出所有循环.

对前面的补充:

(1)当现实字符串时 只能是"" ‘‘ """ """ 这几种方式

  

Python的基本语句

评论关闭