学习python十一天

今天主要从以下网站进行学习
https://www.kaggle.com/code/wangyumei/exercise-syntax-variables-and-numbers/edit
https://www.kaggle.com/code/colinmorris/functions-and-getting-help/tutorial
发现print函数的定义还挺有趣的,之前完全没意识到

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

有被安利到help(function)函数以及docstring撰写的必要性,给个例子真的非常有利于代码的理解,以后写代码要有这个意识:

def least_difference(a, b, c):
    """Return the smallest difference between any two numbers
    among a, b and c.
    
    >>> least_difference(1, 5, -5)
    4
    """
    diff1 = abs(a - b)
    diff2 = abs(b - c)
    diff3 = abs(a - c)
    return min(diff1, diff2, diff3)

Functions Applied to Functions

def mult_by_five(x):
    return x * 5


def call(fn, arg):
    """ Call fn on arg """
    return fn(arg)


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'
)

By default, max returns the largest of its arguments. But if we pass in a function using the optional key argument, it returns the argument x that maximizes key(x) (aka the ‘argmax’).

def mod_5(x):
    return x % 5


print(
    'Which number is biggest?',
    max(100, 51, 14),
    'Which number is the biggest modulo 5?',
    max(100, 51, 14, key=mod_5),
    sep='\n',
)

Round

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

As you’ve seen, ndigits=-1 rounds to the nearest 10, ndigits=-2 rounds to the nearest 100 and so on. Where might this be useful? Suppose we’re dealing with large numbers:

The area of Finland is 338,424 km²
The area of Greenland is 2,166,086 km²

We probably don’t care whether it’s really 338,424, or 338,425, or 338,177. All those digits of accuracy are just distracting. We can chop them off by calling round() with ndigits=-3:

The area of Finland is 338,000 km²
The area of Greenland is 2,166,000 km²

完成https://www.kaggle.com/code/wangyumei/exercise-functions-and-getting-help/edit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python八十天环游世界》是一本以Python编程语言为主题的书籍,它通过一系列有趣的项目和挑战,帮助读者掌握Python的各种应用和技巧。这本书的目标是帮助读者在八十天内通过实践来提高他们的Python编程能力,并且了解Python在不同领域的应用。 这本书通常会涵盖以下内容: 1. Python基础知识:包括变量、数据类型、条件语句、循环语句等基本概念和语法。 2. 函数和模块:介绍如何定义和使用函数,以及如何使用Python的模块来组织代码。 3. 文件操作:学习如何读写文件,以及如何处理CSV、JSON等常见文件格式。 4. 网络编程:了解如何使用Python进行网络通信,包括HTTP请求、Socket编程等。 5. 数据库操作:介绍如何使用Python连接和操作数据库,如MySQL、SQLite等。 6. Web开发:学习使用Python的Web框架(如Django、Flask)来构建网站和Web应用程序。 7. 数据分析和可视化:了解如何使用Python进行数据分析和可视化,如Pandas、Matplotlib等库的使用。 8. 机器学习和人工智能:介绍如何使用Python进行机器学习和人工智能的开发,如Scikit-learn、TensorFlow等。 通过完成这本书中的项目和挑战,读者可以逐步提高他们的Python编程能力,并且了解Python在不同领域的应用。这本书适合已经具备一定Python基础的读者,希望通过实践来进一步提升自己的编程技能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值