`
caobihole
  • 浏览: 951981 次
文章分类
社区版块
存档分类
最新评论

Python 入门教程 5 ---- Conditionals & Control Flow

 
阅读更多


第一节

1 介绍Python利用有6种比较的方式 == , != , > , >= , < , <=

2 比较后的结果是True或者是False

3 练习

1 把bool_one的值设置为 17 < 118%100

2 把bool_two的值设置为 100 == 33*3 + 1

3把bool_two的值设置为 19 <= 2**4

4 把bool_four的值设置为 -22 >= -18

5 把bool_five的值设置为 99 != 98+1

#Assign True or False as appropriate on the lines below!
bool_one = 17 < 118%100
bool_two = 100 == 33*3+1
bool_three = 19 <= 2**4
bool_four = -22 >= -18
bool_five = 99 != 98+1

第二节

1 介绍了比较的两边不只是数值,也可以是两个表达式

2 练习

1 把bool_one的值设置为 20 + -10*2 > 10%3%2

2 把bool_two的值设置为 (10+17)**2 == 3**6

3把bool_two的值设置为 1**2**3 <= -(-(-1))

4 把bool_four的值设置为 40/20*4 >= -4**2

5 把bool_five的值设置为 100**0.5 != 6+4

# Assign True or False as appropriate on the lines below!
bool_one = 20+-10*2 > 10%3%2
bool_two = (10+17)**2 == 3**6
bool_three = 1**2**3 <= -(-(-1))
bool_four = 40/20*4 >= -4**2
bool_five = 100**0.5 != 6+4

第三节

1 介绍了Python里面还有一种数据类型是booleans,值为True或者是False

2 练习:根据题目的意思来设置右边的表达式

# Create comparative statements as appropriate on the lines below!

# Make me true!
bool_one = 1 <= 2

# Make me false!
bool_two = 1 > 2

# Make me true!
bool_three = 1 != 2

# Make me false!
bool_four = 2 > 2

# Make me true!
bool_five = 4 < 5


第四节

1 介绍了第一种连接符and的使用,只有and的两边都是True那么结果才能为True

2 练习

1 设置变量bool_one的值为False and False

2设置变量bool_two的值为-(-(-(-2))) == -2 and 4 >= 16**0.5

3 设置变量bool_three的值为19%4 != 300/10/10 and False

4设置变量bool_four的值为-(1**2) < 2**0 and 10%10 <= 20-10*2

5设置变量bool_five的值为True and True

bool_one = False and False

bool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5

bool_three = 19%4 != 300/10/10 and False

bool_four = -(1**2) < 2**0 and 10%10 <= 20-10*2

bool_five = True and True


第五节

1 介绍了第二种连接符or的使用,只要or的两边有一个True那么结果才能为True

2 练习

1 设置变量bool_one的值为2**3 == 108%100 or 'Cleese' == 'King Arthur'

2设置变量bool_two的值为True or False

3 设置变量bool_three的值为100**0.5 >= 50 or False

4设置变量bool_four的值为True or True

5设置变量bool_five的值为1**100 == 100**1 or 3*2*1 != 3+2+1

bool_one = 2**3 == 108%100 or 'Cleese' == 'King Arthur'

bool_two = True or False

bool_three = 100**0.5 >= 50 or False

bool_four = True or True

bool_five = 1**100 == 100**1 or 3*2*1 != 3+2+1


第六节

1 介绍第三种连接符not , 如果是not True那么结果为False,not False结果为True

2 练习

1 设置变量bool_one的值为not True

2设置变量bool_two的值为not 3**4 < 4**3

3 设置变量bool_three的值为not 10%3 <= 10%2

4设置变量bool_four的值为not 3**2+4**2 != 5**2

5设置变量bool_five的值为not not False

bool_one = not True

bool_two = not 3**4 < 4**3

bool_three = not 10%3 <= 10%2

bool_four = not 3**2+4**2 != 5**2

bool_five = not not False


第七节

1 介绍了由于表达式很多所以我们经常使用()来把一些表达式括起来,这样比较具有可读性

2 练习

1 设置变量bool_one的值为False or (not True) and True

2设置变量bool_two的值为False and (not True) or True

3 设置变量bool_three的值为True and not (False or False)

4设置变量bool_four的值为not (not True) or False and (not True)

5设置变量bool_five的值为False or not (True and True)

bool_one = False or (not True) and True

bool_two = False and (not True) or True 

bool_three = True and not (False or False)

bool_four = not (not True) or False and (not True)

bool_five = False or not (True and True)


第八节

1 练习:请至少使用and,or,not来完成以下的练习

# Use boolean expressions as appropriate on the lines below!

# Make me false!
bool_one = not ((1 and 2) or 3)

# Make me true!
bool_two = not (not((1 and 2) or 3))

# Make me false!
bool_three = not ((1 and 2) or 3)

# Make me true!
bool_four = not (not((1 and 2) or 3))

# Make me true!
bool_five = not (not((1 and 2) or 3)


第九节

1 介绍了条件语句if

2 if的格式如下, 比如

if 8 < 9:
    print "Eight is less than nine!"

3 另外还有这elif 以及else,格式如下

if 8 < 9:
    print "I get printed!"
elif 8 > 9:
    print "I don't get printed."
else:
    print "I also don't get printed!"

4 练习:设置变量response的值为'Y'
response = 'Y'

answer = "Left"
if answer == "Left":
    print "This is the Verbal Abuse Room, you heap of parrot droppings!"
    
# Will the above print statement print to the console?
# Set response to 'Y' if you think so, and 'N' if you think not.

第十节

1 介绍了if的格式

if EXPRESSION:
    # block line one
    # block line two
    # et cetera


2 练习:在两个函数里面加入两个加入条件语句,能够成功输出

def using_control_once():
    if 1 > 0:
        return "Success #1"

def using_control_again():
    if 1 > 0:
        return "Success #2"

print using_control_once()
print using_control_again()


第十一节

1 介绍了else这个条件语句

2 练习:完成函数里面else条件语句

answer = "'Tis but a scratch!"

def black_knight():
    if answer == "'Tis but a scratch!":
        return True
    else:             
        return False       # Make sure this returns False

def french_soldier():
    if answer == "Go away, or I shall taunt you a second time!":
        return True
    else:             
        return False       # Make sure this returns False


第十二节

1 介绍了另外一种条件语句elif的使用

2 练习:在函数里面第二行补上answer > 5, 第四行补上answer < 5 , 从而完成这个函数

def greater_less_equal_5(answer):
    if answer > 5:
        return 1
    elif answer < 5:         
        return -1
    else:
        return 0
        
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)


第十三节

1 练习:利用之前学的比较以及连接符以及条件语句补全函数。所有的都要出现至少一次

def the_flying_circus():
    # Start coding here!
    if 1 < 2 and not False:
        return True
    elif 1 == 1 or 1 != 2 or 1 < 2 or 1 <= 2 or 1 > 2 or 1 >= 2:
        return True
    else:
        return False



分享到:
评论

相关推荐

    447109564555554Gill-084 Learn English Grammar The 4 Conditionals.srt

    447109564555554Gill-084 Learn English Grammar The 4 Conditionals.srt

    Python例程1.2-入门

    Python例程1.2-入门(conditionals,exceptions,for loop,function, generator, simple loop, while loop, error info)-Python examples 1.2-Quick Start(conditionals,exceptions,for loop,function, ...

    React-Kata-2---Lists-and-Conditionals

    该项目是通过引导的。 您将在下面找到一些有关如何执行常见任务的信息。 您可以在找到本指南的最新版本。 目录 更改页面&lt;title&gt; 安装依赖项 导入组件 代码分割 添加样式表 ...将数据从服务器注入

    assignment-2-lists-and-conditionals

    该项目是通过引导的。 可用脚本 在项目目录中,可以运行: npm start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误。...

    Python Programming - iCode Academy

    The Conditionals and Control Flow Chapter 5: Understanding Lists, Tuples, and Dictionaries Chapter 6: The Loops Chapter 7: Understanding the Functions Chapter 8: Coding a Full Program Chapter 9: ...

    《DirectX 9.0 3D游戏开发编程基础》源码Pascal版

    《DirectX 9.0 3D游戏开发编程基础》源码Pascal版 ...2.Project -&gt; Options -&gt; Directories/Conditionals -&gt; Search Path 3.Add: DirectX_Oct06_D7 & DirectX_Sum03_Common ------------------------

    前端开源库-postcss-conditionals

    前端开源库-postcss-conditionalspostss条件,postss插件,用于在CSS中启用@if语句

    conditionals-python-lab-data-science-intro-000

    Python Lab中的条件 介绍 在我们较早的功能论证实验中,我们找到了与Yelp合作查找和比较餐厅的方法。 在本课中,我们将了解条件语句,然后添加更多复杂的方法。 再次,我们在阿尔伯克基的两家餐厅 让我们再来看一个...

    220018764-Calculation___Conditionals.zip

    220018764-Calculation___Conditionals.zip

    Coding project in python

    Step-by-step instructions teach essential coding basics like loops and conditionals, and outline seven fun and exciting projects, including a script that cracks secret codes, a quiz to challenge ...

    CSS Conditionals & Hacks Cheat Sheet

    CSS 浏览器兼容性教程,需要的下载吧。

    Learning to Program(英文python入门书, pdf)

    Conditionals 102 Functions and Modules 117 File Handling 135 Text Handling 144 Error Handling 153 Namespaces 160 Regular Expressions 168 Classes 190 Event Driven Programs 196 Introduction to ...

    Coding.Projects.in.Python.2017.pdf

    Using fun graphics and easy-to-follow instructions, this straightforward, this visual guide shows young learners how to build their own computer projects using Python, an easy yet powerful free ...

    Learn to Program with Python

    Learn to Program with Python by Irv Kalb English | 7 Sept. 2016 | ISBN: 148421868X | 288 Pages | PDF (True) | 7.33 MB Get started in the world of software development: go from zero knowledge of ...

    EmbeddedWB_D5-D2009_Version_14.67.0.rar

    Directories/Conditionals标签Unit Output Dir=“D:\Program Files\CodeGear\RAD Studio\5.0\lib\EmbeddedWB\Source”(具体路径自行决定); 5.按Compile 再按Install; 6.不要保存变化了的dpk文件及压缩包; 7.在...

    Learn to Program with Python(Apress,2016)

    Get started in the world of software development: go from zero knowledge of programming to comfortably writing small to medium-sized programs in Python. Programming can be intimidating (especially ...

    操作excel库downkr.com_LibXL

    Project -&gt; Options -&gt; Directories/Conditionals -&gt; Include path - add library directory to your project (only for old C++ Builder versions) Project -&gt; Options -&gt; Directories/Conditionals -&gt; ...

    Python Power - The Comprehensive Guide (2008).pdf

    Interpreters Versus Compilers .......................................................................................5 When to Use (or Not Use) an Interpreted Language ...................................

    FastMM 4.991

    Delphi中的fastMM控件,文件解压后在Delphi IDE-&gt;Project-&gt;Options-&gt;Directories/Conditionals-&gt;Search Path中添加指向目录就可以开始使用了。

    -lab-07-conditionals

    -lab-07-条件

Global site tag (gtag.js) - Google Analytics