聪明办法学python Task01 & Task02

聪明办法学python 学习笔记

Chapter 0 安装

  1. 安装清单

  2. 安装Miniconda

  3. 更换镜像源

    • Pip 换源

    • Conda 换源

    加快国内资源下载速度

  4. 镜像站

  • 校园网联合镜像站

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 
  1. 课程环境搭建

    • 创建与激活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 服务
  2. VS Code 推荐插件清单

  • Python

  • Jupyter

  • Office Viewer(Markdown Editor)

  • Black Formatter

  • Code Runner

  • Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code

    Chapter 1 启航

    1. 注释

      分类:

      • 单行注释,使用 # 开头

      • 多行注释,使用 '''""" 包裹起来

      作用:

      • 注释主要是用于对代码进行解释和说明,可以提升代码的可读性

      • 注释并不会被当做代码处理 # magic comment 除外

    1. 错误 Error

    • 语法错误 Syntax Errors,不符合语法规范,代码根本没有开始运行

    • “运行时”错误 Runtime Errors,代码在运行过程中出错,也就是常说的“崩溃”(Crash)

    • 逻辑错误 Logical Errors,代码能够运行,且运行过程中没有出错,但是不是想要的结果

    # 语法错误(在编译时出错,Python 并没有开始运行代码)

    print("哦不!) # Error! 缺少结尾引号

      Cell In[1], line 2
        print("哦不!) # Error! 缺少结尾引号
              ^
    SyntaxError: unterminated string literal (detected at line 2)

    # “运行时”错误(Python 开始运行代码,但是遇到了些问题)

    print(1/0) # Error! 0 被作为除数

    ---------------------------------------------------------------------------
    ZeroDivisionError                         Traceback (most recent call last)
    Cell In [28], line 2
          1 # “运行时”错误(Python 开始运行代码,但是遇到了些问题)
    ----> 2 print(1/0)
    ​
    ZeroDivisionError: division by zero

    # Logical Errors (Compiles and runs, but is wrong!)

    # 逻辑错误(能编译,能运行,但不是想要的结果)

    print("2+2=5") # Error! 算错了!!!

    # 我们想要:4!

    2+2=5
    1. 基础的控制台输入 Basic Console Input

    • input() 可以接收 Console 的输入,并以字符串的形式返回,你可以给定个字符串参数,它会先输出到 Console,再接收输入

    • 返回的格式是字符串

    1. 导入模块

    • Python 中有许多强大的工具箱,我们把它们叫做“库”(Library)

    • 库需要使用 import 来导入,并且使用 xx.yy的方式来调用

    总结

    • 写注释是个好习惯

    • 调整输入输出的参数来控制其呈现效果

    • 大部分错误类型可以归为:语法错误、运行时错误和逻辑错误

    • Python 的库能让很多操作变方便

posted @ 2023-11-21 21:05  大程序猿  阅读(87)  评论(0)    收藏  举报