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

歡迎訪問 生活随笔!

生活随笔

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

生活经验

【Kaggle Learn】Python 1-4

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

【Kaggle Learn】Python https://www.kaggle.com/learn/python

一. Hello, Python

A quick introduction to Python syntax, variable assignment, and numbers

spam_amount = 0
print(spam_amount)# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4if spam_amount > 0:print("But I don't want ANY spam!")viking_song = "Spam " * spam_amount
print(viking_song)"""
The result is:
0
But I don't want ANY spam!
Spam Spam Spam Spam 
"""

①無分號 ;

②printf()

③if a>0 :
printf()
if語句用冒號 :

④注釋單行用#,注釋多好用三個 ’ 單引號或三個 " 雙引號

⑤注意要用英文字符

⑥變量無需定義類型,字符串可直接相乘,如"Spam"*4
另外也可以用type()得出當前變量的類型

①c語言以及matlab的基本操作可以用在python上,還有a//b,a**b,-a
a//b : a/b去小數
a ** b : a^b
-a : a的相反數

②printf()用法

print("Height in meters =", total_height_meters, "?")
#Height in meters = 26.9 ?

③其他函數
min()
max()
abs()

float()
int()

二. Exercise: Syntax, Variables, and Numbers

三. Functions and Getting Help

Calling functions and defining our own, and using Python’s builtin documentation

round() 浮點數四舍五入,可多小數位

定義函數

def least_difference(a, b, c):diff1 = abs(a - b)diff2 = abs(b - c)diff3 = abs(a - c)return min(diff1, diff2, diff3)

①def語句也要用冒號 :

②輸入多行代碼
(1)命令行下
在 : 后面按回車,然 后按Tab,打代碼,按回車,以此類推,最后按兩次回車。

(2)記事本轉.py后, 打開python shell, 寫代碼, 按F5或Run-Run module
此時的def也要有一個Tab縮進(其實空格也行 不好看…)

③docstrings 其實就是注釋
要善于寫注釋

④當def里面沒有return 此時用print(def(1,2,3)) 輸出None

⑤輸出help(print)可知print原型

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
不改即為默認值如:
print(1, 2, 3)
1 2 3print(1, 2, 3, sep=' < ')
1 < 2 < 3也可這樣用printprint(call(mult_by_five, 1),squared_call(mult_by_five, 1), sep='\n', # '\n' is the newline character - it starts a new line
)

調用函數

def greet(who="Colin"):print("Hello,", who)greet()
greet(who="Kaggle")
# (In this case, we don't need to specify the name of the argument, because it's unambiguous.)
greet("world")"""
The result is:
Hello, Colin
Hello, Kaggle
Hello, world
"""

調用方式很多

(1)定義函數時可以用其他函數

def mult_by_five(x):return 5 * x#相當于調用一次mult_by_five(x)
def call(fn, arg):"""Call fn on arg"""return fn(arg)#相當于調用一次mult_by_five( mult_by_five(x) )
def squared_call(fn, arg):"""Call fn on the result of calling fn on arg"""return fn(fn(arg))print(call(mult_by_five, 1),squared_call(mult_by_five, 1), sep='\n', # '\n' is the newline character - it starts a new line
)
"""
The result is:
5
25
"""

(2)調用函數(如max函數)時用其他函數

def mod_5(x):"""Return the remainder of x after dividing by 5"""return x % 5print('Which number is biggest?',max(100, 51, 14),'Which number is the biggest modulo 5?',	#通過改變key,使max函數先對數進行mod5,操作再取最大值max(100, 51, 14, key=mod_5),sep='\n',
)#max原型為 max(arg1, arg2, *args, *[, key=func]) -> value"""
The result is:
Which number is biggest?
100
Which number is the biggest modulo 5?
14
"""

四. Exercise: Functions and Getting Help

round原型round(number, ndigits=None)
①當ndigits為正數, 對小數點后n位四舍五入

>>> round(1.0016,3)
1.002
>>> round(1.0014,3)
1.001

①當ndigits為負數, 對小數點前n位四舍五入

>>> round(2166086,-3)
2166000
>>> round(2166086,-2)
2166100
>>> 

Tip: In the kernel editor, you can highlight several lines and press ctrl+/ to toggle commenting.

time函數用法【略】

The time function returns the number of seconds that have passed since the Epoch (aka [Unix time]).
Epoch:January 1, 1970, 00:00:00 (UTC)

from time import time
t = time()
print(t, "seconds since the Epoch")#1550835855.8225462 seconds since the Epoch
#1550835865.3415313 seconds since the Epoch

②停幾秒再輸出,用sleep()

from time import sleep
duration = 5
print("Getting sleepy. See you in", duration, "seconds")
sleep(duration)
print("I'm back. What did I miss?")#Getting sleepy. See you in 5 seconds
#這里經過了5秒
#I'm back. What did I miss?

③計算調用某個函數用了多久,用time()

def time_call(fn, arg):"""Return the amount of time the given function takes (in seconds) when called with the given argument."""passt0 = time()fn(arg)t1 = time()elapsed = t1 - t0return elapsed
"""
def time_call(fn, arg1,arg2):passt0 = time()fn(arg1,arg2)t1 = time()elapsed = t1 - t0return elapsed
print(time_call(max,1,2))
9.5367431640625e-07
"""

總結

以上是生活随笔為你收集整理的【Kaggle Learn】Python 1-4的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。