首页 > 编程语言 >Python入门系列(五)一篇搞懂python语句

Python入门系列(五)一篇搞懂python语句

时间:2022-08-21 18:44:56浏览次数:74  
标签:语句 关键字 Python else python while print 搞懂 than

If语句

elif关键字是pythons表示“如果前面的条件不为真,那么试试这个条件”。

The else keyword catches anything which isn't caught by the preceding conditions.

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

如果只有一条语句要执行,则可以将其与If语句放在同一行。

if a > b: print("a is greater than b")

如果只有一条语句要执行,一条用于If,另一条用于else,则可以将所有语句放在同一行中

a = 2
b = 330
print("A") if a > b else print("B")

and关键字是一个逻辑运算符,用于组合条件语句

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

or关键字是一个逻辑运算符,用于组合条件语句

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

循环语言

while语句

使用while循环,只要条件为true,我们就可以执行一组语句。

i = 1
while i < 6:
  print(i)
  i += 1

使用break语句,即使while条件为true,我们也可以停止循环

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

使用continue语句,我们可以停止当前迭代,然后继续下一个迭代

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

使用else语句,当条件不再为真时,我们可以运行一段代码

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

for语句

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

for循环中的else关键字指定循环完成时要执行的代码块

for x in range(6):
  print(x)
else:
  print("Finally finished!")
for x in range(6):
  if x == 3: break
  print(x)
else:
  print("Finally finished!")

#If the loop breaks, the else block is not executed.

本文由mdnice多平台发布

标签:语句,关键字,Python,else,python,while,print,搞懂,than
From: https://www.cnblogs.com/bugs-in-life/p/16610529.html

相关文章

  • python print 输出格式化的几种方式
    #对浮点数,保留小数点后几位print('{:0.3f}'.format(50.5/220.5))#print格式化字符串num=int(input('请输入一个十进制的整数:'))#将str转为int类型print(num......
  • 学习python第十周学习总结
    数据存储演变史数据存储的演变其实是对数据的存储格式和数据存储的位置进行演变的过程:1.文本文件基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无......
  • python使用装饰器时带括号与不带括号的区别 | 装饰
    带括号如下代码func_dic={}defmake(name):defdemo(func):func_dic[name]=funcreturndemo@make("1")#这里就相当与@demodefmake1():......
  • python3 使用paho-mqtt
    python版本:python3.8mqtt库:paho-mqtt1.6.1 一,消息发布创建pub.py,写入以下代码importtimefrompaho.mqttimportclientasmqtt_client#broker服务器broker......
  • python-f字符串(f-string)99乘法表三种方式
    #%s打印99乘法表foriinrange(1,10):forjinrange(1,i+1):print("%s*%s=%s"%(j,i,j*i),end="")print("\n")#format打印九九乘法表foriinrange(1,......
  • Python教程 - 画折线图
    matplotlibmatplotlib是Python的绘图库,它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。matplotlib可以用来绘制各种静态,动态,交互式的图表。matplotlib......
  • Python教程 - 保存分数结果至文件
    保存文件的方法fns='test_out.txt'withopen(fns,'w+')asfs:print('helloworldbypython',file=fs)将上节课的分析的分数结果保存至文件importtkinter......
  • 一文搞懂 Ftrace 的实现原理
    arm64栈帧结构arm64有31个通用寄存器r0-r30,用法分别如下:寄存器意义SPStackPointer:栈指针r30LinkRegister:在调用函数时候,保存下一条要执行指令的......
  • python 使用正则表达式截取字符串
    假设字符串“a={};”要截取包含花括号在内的内容importrepattern=r="=(.+?);"match_bet_list=eval(re.findall(pattern,match_bet_list,re.M)[0])re.M表示在字符......
  • python-%格式化输出
    输出输出使用的是print()函数,作用,将程序中的数据或结果打印到控制台(屏幕)print('helloworld')name='小明'print(name)age=18print(name,age)#可以使用逗号输出......