python控制流语句if、while、for、break、contnue的学习———ubuntu下python的学习(2)
2017-07-03 09:26
791 查看
继续上次的内容,上次对python进行了简单的介绍,今天看了python的控制流内容:if、for、while、break、continue语句。
下面来介绍这些语句:
if程序:#!/usr/bin/python
#Filename:if1.py
number=23
guess=int(raw_input('Enter an integer:'))
if guess==number:
print"Congratulations,you guessed it."
print"but you do not win any prizes!"
elif guess<number:
print'No,it is a little higher than that'
else:
print'No,it is a little lower than that'
print'Done'
这个if程序是一个猜数游戏,用了if结构。if、elif、else,在ubuntu下要注意缩进,这里涉及到语法错误。这里使用了raw_input函数,int将输入的数确认为整型。
for程序:#!/usr/bin/python
#Filename:for1.py
for i in range(1,5):
print i
else:
print'The for loop is over'
这是一个for循环语句,采用了python内置的range函数,这里要注意:range函数从左到右列举,默认步长为1。
while程序:
#/usr/bin/python
#FIlename while1.py
number=23
running=True
while running:
guess=int(raw_input('Enter an integer:'))
if guess==number:
print"Congratulation,you guessed it."
running=False
elif guess<number:
print"No,guess is a little lower than number "
else:
print"No,guess is a little higher than number"
else:
print "the while loop is over"while循环结构,还是一个猜数游戏,不过可以让用户一直猜下去。这就是while函数的作用。
break函数:#!/usr/bin/python
#Filename:break.py
while True:
s=raw_input('Enter something:')
if s=='quit':
break
print'length of the string is',len(s)
这是一个计算字母个数的程序,增添了输入quit退出程序的功能,这就是break的作用。
continue语句:#!/usr/bin/python
#FIlename:continue.py
while True:
s=raw_input('Enter something:')
if s=='quit':
break
if len(s)<3:
continue
print"tne length of something:",len(s)
还是计算字母个数的程序,增添了在个数小于3时,继续输入的功能,通过continue来实现。
控制流内容
明天开始学习函数内容。
下面来介绍这些语句:
if程序:#!/usr/bin/python
#Filename:if1.py
number=23
guess=int(raw_input('Enter an integer:'))
if guess==number:
print"Congratulations,you guessed it."
print"but you do not win any prizes!"
elif guess<number:
print'No,it is a little higher than that'
else:
print'No,it is a little lower than that'
print'Done'
这个if程序是一个猜数游戏,用了if结构。if、elif、else,在ubuntu下要注意缩进,这里涉及到语法错误。这里使用了raw_input函数,int将输入的数确认为整型。
for程序:#!/usr/bin/python
#Filename:for1.py
for i in range(1,5):
print i
else:
print'The for loop is over'
这是一个for循环语句,采用了python内置的range函数,这里要注意:range函数从左到右列举,默认步长为1。
while程序:
#/usr/bin/python
#FIlename while1.py
number=23
running=True
while running:
guess=int(raw_input('Enter an integer:'))
if guess==number:
print"Congratulation,you guessed it."
running=False
elif guess<number:
print"No,guess is a little lower than number "
else:
print"No,guess is a little higher than number"
else:
print "the while loop is over"while循环结构,还是一个猜数游戏,不过可以让用户一直猜下去。这就是while函数的作用。
break函数:#!/usr/bin/python
#Filename:break.py
while True:
s=raw_input('Enter something:')
if s=='quit':
break
print'length of the string is',len(s)
这是一个计算字母个数的程序,增添了输入quit退出程序的功能,这就是break的作用。
continue语句:#!/usr/bin/python
#FIlename:continue.py
while True:
s=raw_input('Enter something:')
if s=='quit':
break
if len(s)<3:
continue
print"tne length of something:",len(s)
还是计算字母个数的程序,增添了在个数小于3时,继续输入的功能,通过continue来实现。
控制流内容
明天开始学习函数内容。
相关文章推荐
- Python学习--控制语句:if, while, for, break, continue
- GoodZhang在学Python(三)--控制流if while for break continue
- A Byte of Python 笔记(4)控制流:if、for、while、break、continue
- Python控制流语句(if,while,for)
- python控制流语句-while,for,if
- Python语法基础_控制流语句_if、while、for
- 如何熟练掌握python的if、while和for以及与它们相关的break和continue语句 通过什么样的形式, 做题吗?
- Python if for else elif while break loop pass 控制流工具
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- Python学习笔记(九):if、while、for语句
- Python的控制语句,(if,while,for)之if
- python学习小结2:if和while控制语句
- 【python】if&&for&&while语句
- python入门之语句(if语句、while语句、for语句)
- Python学习之if语句和for语句
- Python学习:两种循环语句for和while
- python学习之语句:循环语句(for,while)
- 【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)
- python_3(循环语句、if、switch、for、break、冒泡算法)
- linux shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select 学习笔记