【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的邏輯運(yùn)算返回值(或運(yùn)算結(jié)果)為布爾代數(shù)形式
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"有運(yùn)算優(yōu)先級
not>and>or
【為了防止記錯可以用括號】
True or True and False
"""
The result is:
True
"""
③當(dāng)邏輯關(guān)系比較復(fù)雜時,可以分層寫
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.
【記得加冒號 : 以及加縮進(jìn)】
除了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()
現(xiàn)在有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'#邏輯運(yùn)算outcome = ('failed' if grade < 50 else 'passed')#相當(dāng)于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
類似復(fù)合的數(shù)組,可以有數(shù)字,字符串,甚至函數(shù)等
my_favourite_things = [32, 'raindrops on roses', help]my_favourite_things[0]
#和數(shù)組一樣 從0數(shù)起
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.
#數(shù)組序號可用負(fù)數(shù), -1即為離0最遠(yuǎn)的元素, -2即為次遠(yuǎn)
二維數(shù)組兩種寫法
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中一樣, 求得數(shù)組的某行某列至某行某列
此處的冒號 : 即有 “到” 的意思
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
總結(jié)
以上是生活随笔為你收集整理的【Kaggle Learn】Python 5-8的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Kaggle Learn】Python
- 下一篇: 记录win10快捷键