python学习:基础语句

目录

条件语句

循环语句

[for 循环](#for 循环)

[while 循环](#while 循环)

break

continue


条件语句

Python提供了 ifelifelse 来进行逻辑判断。格式如下:

Pythonif 判断条件1:

执行语句1...

elif 判断条件2:

执行语句2...

elif 判断条件3:

执行语句3...

else:

执行语句4...

循环语句

Python 提供了 for 循环和 while 循环。

for 循环

for 循环可以遍历任何序列,比如:字符串、集合。如下所示:

字符串:

>>>

>>> str = 'hello'

>>> for s in str: print(s)

...

h

e

l

l

o

集合:

>>> l = ['high','gg','test']

>>> for x in l: print(x)

...

high

gg

test

>>>

while 循环

满足条件时进行循环,不满足条件时退出循环。如下所示:

>>> num = 1

>>> while num <= 5:

... print(num)

... num += 1

...

1

2

3

4

5

>>>

break

在 for 循环和 while 循环语句中,用来终止整个循环。如下所示:

>>> str = 'Python'

>>> for s in str:

... if s == 'o':

... break

... print(s)

...

P

y

t

h

>>>

continue

用在 for 循环和 while 循环语句中,用来终止本次循环。如下所示:

>>> str = 'Python'

>>> for s in str:

... if s == 'o':

... continue

... print(s)

...

P

y

t

h

n

>>>

相关推荐
虾球xz26 分钟前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
蓝婷儿1 小时前
6个月Python学习计划 Day 17 - 继承、多态与魔术方法
开发语言·python·学习
持续前进的奋斗鸭2 小时前
Postman测试学习(1)
学习·postman
hello kitty w2 小时前
Python学习(7) ----- Python起源
linux·python·学习
一叶知秋秋2 小时前
python学习day39
人工智能·深度学习·学习
永日456703 小时前
学习日记-day24-6.8
开发语言·学习·php
安和昂3 小时前
【iOS】 Block再学习
学习·ios·cocoa
pop_xiaoli3 小时前
OC学习—命名规范
学习·ios
jackson凌3 小时前
【Java学习笔记】String类(重点)
java·笔记·学习
行云流水剑3 小时前
【学习记录】在 Ubuntu 中将新硬盘挂载到 /home 目录的完整指南
服务器·学习·ubuntu