【Kaggle Learn】Python 5-8
五. Booleans and Conditionals
Using booleans for branching logic
x = True
print(x)
print(type(x))'''
True
<class 'bool'>
'''
①Booleans
Python has a type bool which can take on one of two values: True and False.
②Comparison Operations
a == b, and, or, not等等
Python的逻辑运算返回值(或运算结果)为布尔代数形式
3.0 == 3
True'3' == 3
False
①Python provides operators to combine boolean values using the standard concepts of “and”, “or”, and “not”.
def can_run_for_president(age, is_natural_born_citizen):"""Can someone of the given age and citizenship status run for president in the US?"""# The US Constitution says you must be a natural born citizen *and* at least 35 years oldreturn is_natural_born_citizen and (age >= 35)print(can_run_for_president(19, True))
print(can_run_for_president(55, False))
print(can_run_for_president(55, True))
"""
The result is:
False
False
True
"""
②"and", “or”, and "not"有运算优先级
not>and>or
【为了防止记错可以用括号】
True or True and False
"""
The result is:
True
"""
③当逻辑关系比较复杂时,可以分层写
prepared_for_weather = have_umbrella or ((rain_level < 5) and have_hood) or (not (rain_level > 0 and is_workday))prepared_for_weather = (have_umbrella or ((rain_level < 5) and have_hood) or (not (rain_level > 0 and is_workday))
)
Conditionals(条件语句)
if, elif(新), and else.
【记得加冒号 : 以及加缩进】
除了else, 其他有判断条件
Note especially the use of colons ( : ) and whitespace to denote separate blocks of code. This is similar to what happens when we define a function - the function header ends with :, and the following line is indented with 4 spaces. All subsequent indented lines belong to the body of the function, until we encounter an unindented line, ending the function definition.
def inspect(x):if x == 0:print(x, "is zero")elif x > 0:print(x, "is positive")elif x < 0:print(x, "is negative")else:print(x, "is unlike anything I've ever seen...")inspect(0)
inspect(-15)
"""
The result is:
0 is zero
-15 is negative
"""
之前有int(), float()
现在有Boolean conversion bool()
print(bool(1)) # all numbers are treated as true, except 0
print(bool(0))
print(bool("asf")) # all strings are treated as true, except the empty string ""
print(bool(""))
# Generally empty sequences (strings, lists, and other types we've yet to see like lists and tuples)
# are "falsey" and the rest are "truthy"
"""
True
False
True
False
"""
if else语句两种写法
def quiz_message(grade):if grade < 50:outcome = 'failed'else:outcome = 'passed'print('You', outcome, 'the quiz with a grade of', grade)def quiz_message(grade):outcome = 'failed' if grade < 50 else 'passed'#逻辑运算outcome = ('failed' if grade < 50 else 'passed')#相当于c语言中的outcome = grade < 50 ? 'failed' : 'passed'print('You', outcome, 'the quiz with a grade of', grade)
六. Exercise: Booleans and Conditionals
略
七. Lists
Lists and the things you can do with them. Includes indexing, slicing and mutating
类似复合的数组,可以有数字,字符串,甚至函数等
my_favourite_things = [32, 'raindrops on roses', help]my_favourite_things[0]
#和数组一样 从0数起
32my_favourite_things[2]
#Type help() for interactive help, or help(object) for help about object.my_favourite_things[-1]
#Type help() for interactive help, or help(object) for help about object.
#数组序号可用负数, -1即为离0最远的元素, -2即为次远
二维数组两种写法
hands = [['J', 'Q', 'K'],['2', '2', '2'],['6', 'A', 'K'], # (Comma after the last element is optional)
]
# (I could also have written this on one line, but it can get hard to read)
hands = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]
Slicing 跟matlab中一样, 求得数组的某行某列至某行某列
此处的冒号 : 即有 “到” 的意思
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']planets[0:3]
['Mercury', 'Venus', 'Earth']planets[:3]
['Mercury', 'Venus', 'Earth']
#planets[0:3] is our way of asking for the elements of planets starting from index 0
#and continuing up to but not including index 3.可以理解为左闭右开[m,n)planets[3:]
['Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']# The last 3 planets
planets[-3:]
['Saturn', 'Uranus', 'Neptune']
八. Exercise: Lists
【Kaggle Learn】Python 5-8相关推荐
- 【Kaggle Learn】Python 1-4
[Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...
- 关于python的比赛_【蓝桥杯】——python集团的比赛技巧,Python,组
[蓝桥杯]-- Python组比赛技巧 蓝桥杯是大学生IT学科赛事,由工业和信息化部人才交流中心主办,所以对于大学生还说还是非常值得去参加的,2020年第十一届蓝桥杯新增了大学Python组,不分组别 ...
- 【面试题】python怎么合并两个字典
Python中将两个字典进行合并操作,是一个比较常见的问题.本文将介绍几种实现两个字典合并的方案,并对其进行比较. 对于这个问题,比较直观的想法是将两个字典做相加操作,赋值给结果字典,其代码为: [方 ...
- python就业方向及工资-【行情分享】python就业方向与薪资大揭秘
原标题:[行情分享]python就业方向与薪资大揭秘 学python,我们要首先问自己,是为了转行?提升自己?还是什么,有了明确的目标,才会沉下心来学习.我学习python的目标是想要转行,可以跟大家 ...
- python获取已打开网页的html,【已解决】Python的BeautifulSoup去实现提取带tag的HTML网页主体内容...
折腾: [未解决]Python的html网页主体内容提取 期间,去试试BeautifulSoup提取HTML网页主体内容 先去随便找个合适的网页 -> 简单看了看网页内容结构: 发现是: 网页主 ...
- python里面两个大于号_【课堂笔记】Python常用的数值类型有哪些?
学习了视频课程<财务Python基础>,小编特为大家归纳了Python常用的数值类型和运算符,大家一起来查缺补漏吧~~ 数值类型 整型(int):整型对应我们现实世界的整数,比如1,2,1 ...
- python中构造方法的名字,【填空题】Python提供了名称为 的构造方法,实现让类的对象完成初始化。...
[填空题]Python提供了名称为 的构造方法,实现让类的对象完成初始化. 更多相关问题 如图是2012年元宵节灯展中一款五角星灯连续旋转闪烁所成的三个图形,照此规律闪烁,下一个呈现出来的图形是( 在 ...
- 《计算机科学导论》百度云,【麻省理工学院】Python编程和计算机科学导论公开课(中英字幕)...
声明&链接 『[麻省理工学院]Python编程和计算机科学导论公开课(中英字幕)』资源内容来源于 52搜盘. 请认真阅读以下说明,您只有在了解并同意该说明后,才可继续访问本站. 1. 请认准罗 ...
- python中sqrt(4)*sqrt(9)_【单选题】Python表达式sqrt(4)*sqrt(9)的值为
[单选题]Python表达式sqrt(4)*sqrt(9)的值为 更多相关问题 构成营业利润的要素主要包括(). A.营业收入 B.营业成本 C.营业税金及附加 D.所得税费用 E.管理费用 已知二次 ...
最新文章
- 【Ubuntu】将Ubuntu的源改为国内源
- 麻省理工正式宣布人工智能独立设系!人工智能与电子工程、计算机科学系将三分天下...
- 植物的意识,是我们的错觉吗?
- Deseq2的理论基础
- 九 Android基本知识介绍
- 人工智能第二课:认知服务和机器人框架探秘
- 不使用sizeof,获取变量所占用的字节数
- Codeforces1142D
- 书评:Mockito Essentials
- zabbix前端php界面,Zabbix Web UI
- 单机安装ZooKeeper
- 【操作系统】Semaphore处理吸烟者问题
- 用matlab做数据处理的几个小坑
- Ignition Vision基本操作
- CAD批量提取数值lisp插件_CAD批量获取文本坐标及内容
- 实现渐变彩色消隐旋转立方体
- 非线性方程-概念应用及解法
- Android BottomNavigationView的使用
- 同步linux软件源,linux 双向同步软件 unison的安装和配置!
- nginx自定义404错误页面