PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!
《Python for Beginners》为LearnStreet上的Python入门课程。本节要主学习容内为条件语句。
Lesson 4 Control Flow and Conditionals
1. 第一个Python数函
1 def check_wounds(): 2 #your code here 3 arms = 0 4 if arms == 1: 5 return "tis but a scratch" 6 elif arms == 0: 7 return "flesh wound" 8 else: 9 return "cross bridge" 10 11 #This is just for you to see what happens when the function is called 12 print check_wounds()
输出结果:
flesh wound
2. if语句
1 def always_true(): 2 if 1 == 1: 3 #insert your code here 4 return "complete" 5 6 #This is just for you to see what happens when the function is called 7 print always_true()
输出结果:
complete
3. 再来一个if语句
1 flag = "unchanged" 2 def always_false(): 3 global flag 4 if 1==2: 5 flag = "changed" 6 7 #This is just for you to see what happens when the function is called 8 always_false() 9 print flag
输出结果:
unchanged
4. else语句
1 def check_condition(): 2 #return this if the condition is True 3 ifRun = "if code block run" 4 5 #return this is if the condition is False 6 elseRun = "else code block run" 7 8 condition = 0 9 10 if condition == True: 11 #your code here 12 return ifRun 13 else: 14 #your code here 15 return elseRun 16 17 #This is just for you to see what happens when the function is called 18 print check_condition()
输出结果:
else code block run
5. elif语句
1 def colorful_conditions(): 2 3 color = "blue" 4 5 if color == "red": 6 return "first block" 7 elif color == "white": 8 return "second block" 9 elif color == "blue": 10 return "third block" 11 else: 12 return "fourth block" 13 14 #This is just for you to see what happens when the function is called 15 print colorful_conditions()
输出结果:
third block
6. if语句训练
问题:
When the string phrase is fewer than 30 characters long, return 1. When it is exactly 30 return 2, and when it is greater than 30 return 3.
代码:
1 def check_length(phrase): 2 # your if condition here 3 if len(phrase) < 30: 4 return 1 5 # your elif condition here 6 elif len(phrase) == 30: 7 return 2 8 # your else condition here 9 else: 10 return 3 11 12 #This is just for you to see what happens when the function is called 13 print check_length("hi, i am a phrase")
输出结果:
1
7. if语句训练2
1 """ 2 This function should check the value of the num variable and 3 return the string representation of the interval it is in Use if, elif, 4 and else statements to check if num falls in the range 1-5, 6-10, 5 11-15, or 16-20. Then return the correct interval as a string, like 6 "1-5", "6-10", "11-15", or "16-20". 7 """ 8 def check_interval(): 9 #Your code here 10 num = 5 11 if num < 6: 12 return "1-5" 13 elif num < 11: 14 return "6-10" 15 elif num < 16: 16 return "11-15" 17 else: 18 return "16-20" 19 20 #This is just for you to see what happens when the function is called 21 print check_interval()
输出结果:
1-5
8. and操作符
1 def should_eat(hungry, awake): 2 #your if statement here 3 if hungry and awake: 4 return "eat" 5 else: 6 return "don't eat" 7 8 #This is just for you to see what happens when the function is called 9 print should_eat(True, True)
输出结果:
eat
9. or操作符
1 def kitchen_or_bed(hungry, thirsty): 2 # your if statement here 3 if hungry or thirsty: 4 return "go to kitchen" 5 else: 6 return "go to bed" 7 8 #This is just for you to see what happens when the function is called 9 print kitchen_or_bed(True, True)
输出结果:
go to kitchen
10. 复习
1 def who_are_you(green, large): 2 # Write if-elif-else statements below 3 if green and large: 4 return "hulk" 5 elif green: 6 return "alien" 7 elif large: 8 return "elephant" 9 else: 10 return "Bruce Banner" 11 12 #This is just for you to see what happens when the function is called 13 print who_are_you(True, True)
输出结果:
hulk
总结:
1)注意缩进的格式
2)#开头为注释一行代码
3)注意数函定义以及条件语句后面的冒号
4)字符串可以直接作为数函的实参
(本文完)
文章结束给大家分享下程序员的一些笑话语录: 警告
有一个小伙子在一个办公大楼的门口抽着烟,一个妇女路过他身边,并对他 说, “你知道不知道这个东西会危害你的健康?我是说, 你有没有注意到香烟 盒上的那个警告(Warning)?”
小伙子说,“没事儿,我是一个程序员”。
那妇女说,“这又怎样?”
程序员说,“我们从来不关心 Warning,只关心 Error”
相关推荐
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 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 Programming for Beginners Easy Steps to Learn the Python Language and Go from Beginner to Expert Today!
**Python编程初学者指南:入门...总之,《Python Programming for Beginners》这本书是Python初学者的绝佳资源,它将带你逐步了解Python的核心概念,并通过实践来巩固这些知识,助你快速成为一名合格的Python程序员。
epub完整版 Python Machine Learning.Practical Guide for Beginners.epub
- **在线资源**:鼓励学习者在空闲时间自学课程笔记,或者参考网络上的Python教程。 - **其他课程链接**:提供了指向“Python: Introduction for Absolute Beginners”课程详情页面的链接。 ### 特别提示 - 课程...
【Python 3 for Absolute Beginners】是一本专为Python编程初学者设计的教程,它涵盖了从基础到进阶的全面知识,旨在帮助新手快速掌握Python 3这一强大的编程语言。在这个教程中,你将学习到: 1. **安装Python 3**...
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
3. **控制结构**:Python的流程控制语句包括条件判断(if-else)、循环(for、while)以及异常处理(try-except)。这些语句在编写逻辑时起到关键作用。 4. **函数与模块**:Python中的函数是一段可重复使用的代码...
### Python 3 for Absolute Beginners — 关键知识点概览 #### 一、书籍基本信息与版权信息 - **书名**:《Python 3 for Absolute Beginners》 - **作者**: Tim Hall 和 J-P Stacey - **出版年份**: 2009 年 - **...
3. **深度学习基础**: - 深度学习的基本原理和神经网络结构介绍。 - 如何使用TensorFlow和Keras构建和训练深度学习模型。 4. **自然语言处理**: - 自然语言处理的基本任务和技术,如文本分类、情感分析等。 -...
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 ...