【Kaggle Learn】Python 1-4

本教程为Python初学者提供了一个快速入门,涵盖了语法、变量、数字处理、函数定义及调用等核心概念。从Hello World到自定义函数,深入浅出地讲解了Python的基本操作和常用函数,如round()、min()、max()等,并介绍了如何获取帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【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 + 4

if 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 3

print(1, 2, 3, sep=' < ')
1 < 2 < 3

也可这样用print

print(
    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 % 5

print(
    '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.
    """
    pass
	t0 = time()
	fn(arg)
	t1 = time()
	elapsed = t1 - t0
	return elapsed
"""
def time_call(fn, arg1,arg2):
    pass
    t0 = time()
    fn(arg1,arg2)
    t1 = time()
    elapsed = t1 - t0
    return elapsed
print(time_call(max,1,2))
9.5367431640625e-07
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值