日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【Kaggle Learn】Python 5-8

發(fā)布時間:2023/11/28 生活经验 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Kaggle Learn】Python 5-8 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

五. 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的邏輯運算返回值(或運算結(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ōu)先級
not>and>or
【為了防止記錯可以用括號】

True or True and False
"""
The result is:
True
"""

③當邏輯關(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.
【記得加冒號 : 以及加縮進】
除了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'#邏輯運算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

類似復(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ù)組序號可用負數(shù), -1即為離0最遠的元素, -2即為次遠

二維數(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。