1120-Datawhale组队学习
Datawhale组队学习
Task01:课程简介,安装installation
——————————————————————
-----------------------------课程简介---------------------------
Python介绍:
python是目前流行度较高的编程语言
为什么要学python
1,跑出自己的ai程序?
2,想学人工智能?
3,值得用python入门编程?
4,做可视化分析?
什么是python
一句话
很适合入门人工智能的编程语言
特点:
1,是一门胶水语言,可以和其他语言一起用
2,生态很好
怎么学python
1,多动手,多写代码,写有意义的代码
2,多去看别人的优代码
installatioin的安装
安装清单
所有安装均仅需默认配置即可
-
Visual Studio Code: https://code.visualstudio.com/
Miniconda安装配置
Miniconda 下载地址:https://docs.conda.io/en/latest/miniconda.html
最新版 Miniconda For Windows 下载链接:
https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe
安装配置推荐
修改 Powershell 执行策略(可选)
仅 出现”此系统禁止运行脚本“ 的情况下需要执行本操作
在开始
图标右键单击,选择 Windows PowerShell(管理员)
先输入下面的内容,并回车:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
出现如下内容后,输入:A
,回车:
最后在 Anaconda Powershell Prompt 中输入:
conda init
更换镜像源
-
Pip 换源
-
Conda 换源
加快国内资源下载速度
校园网联合镜像站
https://help.mirrors.cernet.edu.cn/
阿里巴巴开源镜像站
https://developer.aliyun.com/mirror/
请避免使用代理,不合理的代理设置会导致下载失败
Conda 更换镜像源
清华大学开源软件镜像站:https://help.mirrors.cernet.edu.cn/anaconda/
南方科技大学开源软件镜像站:https://help.mirrors.cernet.edu.cn/anaconda-extra/
在 Anaconda Powershell Prompt 中输入:
conda config --set show_channel_urls yes
在镜像站复制文本后,在 Anaconda Powershell Prompt 中输入:
notepad .condarc # 注意有个小点 "." 在 "condarc" 的前面
删除原有的所有内容,粘贴刚刚复制的文本,保存文件后关闭
最后在 Anaconda Powershell Prompt 中输入:
conda clean -i # 清除源缓存,以启用镜像源
PyPI 更换镜像源
校园网联合镜像站:https://help.mirrors.cernet.edu.cn/pypi/
复制文本后,在 Anaconda Powershell Prompt 中粘贴运行即可:
# 设置 PyPI 镜像源
pip config set global.index-url https://mirrors.cernet.edu.cn/pypi/web/simple
课程环境搭建
创建与激活 Conda 环境
创建 Conda 环境
conda create -n p2s python=3.10 # conda 环境创建
其中 -n 代表创建的环境名称,这里是 p2s,并指定* Python 版本为 3.10*
激活刚刚创建的 Conda 环境:
conda activate p2s # 激活 p2s 环境,不同环境的 Python 包版本不同!
如果需要删除某个 Conda 环境:
conda deactivate # 退出该环境
conda remove -n p2s --all # 删除整个环境
Git 下载课程资料
Github Repo: https://github.com/datawhalechina/learn-python-the-smart-way-v2
Gitee Repo: https://gitee.com/anine09/learn-python-the-smart-way-v2
在 Anaconda Powershell Prompt 中,使用 cd
命令,进入到指定文件夹,例如:
cd C:\Coding\ # 注意 cd 与指定文件夹路径之间有个空格
继续输入:
git clone https://github.com/datawhalechina/learn-python-the-smart-way-v2 --depth=1 # --depth=1 的作用是只下载最新版本的代码文件
Pip 安装与展示
Pip 安装课程所需第三方库
pip install jupyter # julia python R
在指定文件夹输入:
jupyter lab # 会自动跳转到浏览器
结束学习时使用:
Ctrl + C # 关闭 Jupyter Notebook 服务
VSCode 推荐插件清单
-
Python
-
Jupyter
-
Office Viewer(Markdown Editor)
-
Black Formatter
-
Code Runner
-
Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code
云端环境的使用
-
百度飞桨 AI Studio https://aistudio.baidu.com/aistudio/index
-
Mo(智海平台)https://momodel.cn/workspace
-
阿里天池 PAI DSW https://tianchi.aliyun.com/notebook-ai
-
Kaggle https://www.kaggle.com/code
-
Google Colab https://colab.research.google.com/
-
Sagemaker Studio Lab https://studiolab.sagemaker.aws/
Task02:启航Getting Started
第一行代码“Hello,World”
Language C
#include<stdio.h>
int main(){
printf("Hello, World");
return 0;
}
python
print("Hello,World")
hello world 的由来
Language B Version:
B
main( ) {
extern a, b, c;
putchar(a); putchar(b); putchar(c); putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';
注释 Comment
分类:
-
单行注释,使用
#
开头 -
多行注释,使用
'''
或"""
包裹起来
eg 1: print("hello") #打印hello
eg 2:
print("我爱")
print("云顶书院")
'''
打印
我爱
云顶书院
'''
多行注释
使用 '''
或 """
包裹起来(头和尾都是 3 个),单引号(')与双引号(")在 Python 中并无太大区别
print("人生苦短,我用 Python")
'''
Python is powerful... and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open.
'''
基础的控制台输出 Basic Console Output
print("datawhale")
print()
的作用是将填入的内容显示在 Console 中,默认每次输入后会换行(等价于按了一次回车,或者 \n
)
控制结尾的参数是 end
print("Data")
print("whale")
Data whale
print("Data", end="*")
print("whale")
Data*whale
在这里并不换行
//
控制分隔的参数是 sep
print输出时可以输出多个内容,默认用空格分开
print("Data","whale")
Data whale
print("Data", "whale", sep="*")
Data*whale
在python中可以进行字符串的加法和乘法
加法
a='A'
b='B'
c=a+b
加法表示字符串的拼接
//
乘法
a="A"*4
等价于
a="AAAA"
乘法表示一定数量相同字符串的堆叠
f-string的用法
x = 1
y = 2
print(f"一个简单的数学问题:\"{x} + {y} = ?\",答案是 {x+y}!") # f-strings
一个简单的数学问题:"1 + 2 = ?",答案是 3!
要想一次输出多行
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.
用此可以秒杀马里奥
https://www.luogu.com.cn/problem/P1000
错误 Error
-
语法错误 Syntax Errors,不符合语法规范,代码根本没有开始运行
-
“运行时”错误 Runtime Errors,代码在运行过程中出错,也就是常说的“崩溃”(Crash)
-
逻辑错误 Logical Errors,代码能够运行,且运行过程中没有出错,但是不是想要的结果
# 语法错误(在编译时出错,Python 并没有开始运行代码)
print("哦不!) # Error! 缺少结尾引号
Syntax Errors
//
print(1/0) # Error! 0 被作为除数
ZeroDivisionError
//
# 逻辑错误(能编译,能运行,但不是想要的结果)
print("2+2=5") # Error! 算错了!!!
# 我们想要:4!
Logical Errors
基础的控制台输入 Basic Console Input
name=input("please enter you name:")
给出提示语句提示用户输入姓名
注意!返回的格式是字符串
如果输入的是数字类型的字符串,想要使用这些值,需要转型,
x = int(input("输入一个数字:"))# f(g(x))
一行输入多个值
1 -> a,2 -> b
可以在结尾加上 split()
,默认分隔参数是空格,可以更改,如:split(",")
In [38]:
a, b = input().split("*")
print(f"a = {a}, b = {b}")
1*2 a = 1, b = 2
导入模块
Python 中有许多强大的工具箱,我们把它们叫做“库”(Library),库需要使用 import
来导入,并且使用 xx.yy
的方式来调用
以调用内置数学库为例
阶乘
# 阶乘 factorial
print(math.factorial(20))
这样输入会报错
NameError
要先导入
ValueError Traceback (most recent call last)
Cell In [43], line 2
1 import math # 使用库前要先导入!
----> 2 print(math.factorial(20000))
ValueError: Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
这里有限制只能算到4300的阶乘
//
常数
# Euler 常数
print(math.e)
2.718281828459045
//
计算最大公因数
# gcd 最大公约数
math.gcd(12, 36)
12
最后
-
写注释是个好习惯
-
调整输入输出的参数来控制其呈现效果
-
大部分错误类型可以归为:语法错误、运行时错误和逻辑错误
-
Python 的库能让很多操作变方便
posted on 2023-11-21 20:22 Shun了个shun 阅读(65) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下