这两天笔者几篇文章介绍了改输出循环的文章. 关联文章的地址
《Python for Beginners》为LearnStreet上的Python入门课程。本节重要学习内容为循环语句。
一、基本法语
1. while循环
上面两种写法是等价的:
while flag:
while flag == True:
2. for循环
for x in range(0,3):
range(0,3) 行执的有效值范围为[0,1,2], 并且同range(3)是等价的。
二、程编训练
1. Incrementing and Decrementing
1 def run(first, second): 2 #your code here 3 first +=1 4 second -=1 5 return (first, second) 6 7 #This is just for you to see what happens when the function is called 8 print run(1, 20)
输出结果:
(2, 19)
#注意本数函有两个返回值。
2. while循环训练
数函能功:输出1-10
1 def run(): 2 count = 1 3 #your code here 4 while count <=10: 5 print count 6 count +=1 7 return count 8 9 #This is just for you to see what happens when the function is called 10 print run()
输出结果:
1
2
3
4
5
6
7
8
9
10
11
思考:为什么会输出11?因为数函以print run()式方停止调用,将输出数函的返回值(值为11),若无返回值则输出None。
3. for循环训练1
数函能功:算计1-10共10个数的和。
1 def run(): 2 sum = 0 3 for i in range(11): 4 #your code here 5 sum +=i 6 return sum 7 8 #This is just for you to see what happens when the function is called 9 print run()
输出结果:
55
4. for循环训练2
1 def run(): 2 #your loop here 3 for i in range(10): 4 print "#" * i 5 6 #This is just for you to see what happens when the function is called 7 run()
输出结果:
#
##
###
####
#####
######
#######
########
#########
注意:输出结果的第一为行空行
5. for循环训练3
实现能功:输出全部小于20的奇数,并返回这些数的和。
代码:
1 def run(): 2 #your code here 3 sum = 0 4 for i in range(10): 5 print i*2+1 6 sum += i*2+1 7 return sum 8 9 #This is just for you to see what happens when the function is called 10 print run()
输出结果:
1
3
5
7
9
11
13
15
17
19
100
6. Iterables
1 def run(): 2 """ 3 print each letter in the string using the for loop syntax and then return the last character sequence 4 """ 5 str = "LearnStreet!" 6 for i in range(len(str)): 7 print str[i] 8 return str[i] 9 10 #This is just for you to see what happens when the function is called 11 run()
输出结果:
L
e
a
r
n
S
t
r
e
e
t
!
注意:如果数函以print run()式方调用,则会输出返回值(值为'!'),若没有return str[i]语句,则返回值输出为None。
7. 习复(条件语句和循环语句)
1 def checkLen(): 2 str = "Ninja Coder!" 3 flag = True 4 while flag: 5 count = 0 6 for i in range(len(str)): 7 count +=1 8 if count == len(str): 9 flag = False 10 print "done" 11 else: 12 print "not yet done" 13 14 #This is just for you to see what happens when the function is called 15 checkLen()
输出结果:
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
not yet done
done
总结:
1. 一个数函可以有多个返回值。
2. Python许允一个字符串与整数n相乘,结果等价于n个字符串联串一同。
3. 注意数函调用时run()与print run()的别区。
(本文完)
文章结束给大家分享下程序员的一些笑话语录: 一位程序员去海边游泳,由于水性不佳,游不回岸了,于是他挥着手臂,大声求.救:“F1,F1!”
相关推荐
Python Programming for Beginners 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
When you purchase Python: Programming Language for Beginners - Learn In A Day, you'll find out the importance of this language, and how to begin improving your skills! These fun and easy tips ...
Introduction to python for Beginners.pptx
Python: Practical Python Programming For Beginners and Experts English | 9 July 2016 | ISBN: 1535192976 | 170 Pages Start work on your website, app, or search engine today with this practical Python ...
Python 3 for Absolute Beginners.pdf
Python in 8 Hours, Python for Beginners, Learn Python fast! A Smart way to learn Python, Python programming, Python in easy steps: A Beginner's Guide, Fast & Easy! by Ray Yao English | Feb. 28, 2016 ...
python for beginners
Python Programming for Beginners Easy Steps to Learn the Python Language and Go from Beginner to Expert Today!
Python for beginners
epub完整版 Python Machine Learning.Practical Guide for Beginners.epub
Python3 入门书籍,Python3 比python27多了很多不同的特点
Packt.Python.Beginners.Guide.to.Artificial.Intelligence
Easy Python Programming for Beginners Your Step-By-Step Guide to Learning Python Programming
Python: Learn Python in 24 Hours: Complete Beginners Guide English | 19 July 2016 | ASIN: B01IRGB6MY | 110 Pages If you are one of them who easily get scared of Python’s long, complicated code, then...
Packt.Python.Artificial.Intelligence.Projects.for.Beginners.2018
Django For Beginners.part4. Django For Beginners.part1-3已上传.
Apache Spark 2.0 for Beginners English | ISBN: 1785885006 | 2016 | Key Features This book offers an easy introduction to the Spark framework published on the latest version of Apache Spark 2 Perform ...
分享一本英文python书籍,是azw3格式的是kindle商店正版破解的,epub版是无损破解。
Python入门课程针对完全没有编程经验的人。