组队学习-学习笔记P1

发布时间 2023-11-21 22:58:56作者: 人间烟火天上星

组队学习-学习笔记P1

Task01 课程简介、安装Installation

创建 Conda 环境

Anaconda Powershell Prompt 中运行:

conda create -n p2s python=3.10 # conda 环境创建

其中 -n 代表创建的环境名称,这里是 p2s,并指定 Python 版本为 3.10

这里的Python版本需要根据你自己下载的Python版本而定,

例如我下载的是3.12版本,则代码需要改为 conda create -n p2s python=3.12,否则将会报错

Task02 启航 Getting Started

注释 Comment

单行注释

使用 # 开头,# 后面的内容不会被当做代码,只能写在一行中

例:

print("Datawhale") # for the learner,和学习者一起成长

输出为:Datawhale

多行注释

使用 '''""" 包裹起来(头和尾都是 3 个),单引号(')与双引号(")在 Python 中并无太大区别

例:

print("人生苦短,我用 Python")
'''
Python is powerful... and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open.
'''

输出为:人生苦短,我用 Python

输出

Python 中的输出函数为 print 函数

  1. print() 的作用是将填入的内容显示在 Console 中,默认每次输入后会换行(等价于按了一次回车,或者 \n

  2. print() 一次也可以输出多个内容,默认以空格分隔

    控制分隔的参数是 sep

    print("Data","whale") # f(x, y)

    输出为:Data whale

    print("Data", "whale", sep="*")

    输出为:Data*whale

  3. print也可以一次性输出多行

    print("""
    Python is powerful... and fast;
    plays well with others;
    runs everywhere;
    is friendly & easy to learn;
    is Open.
    """)

    输出为:

    Python is powerful... and fast;
    plays well with others;
    runs everywhere;
    is friendly & easy to learn;
    is Open.

导入模块

Python 中有许多强大的工具箱,我们把它们叫做“库”(Library),很多强大的工具库需要使用 import 来导入,并且使用 xx.yy的方式来调用

以 Python 内置数学库 math 为例:

import math # 使用库前要先导入!

#阶乘 factorial

print(math.factorial(20))

#Euler 常数

print(math.e)sh

输出为:2.718281828459045

#gcd 最大公约数

math.gcd(12, 36)

输出为:

12