p2s 学习chapter_1与chapter_2

发布时间 2023-11-21 20:35:33作者: LPF0502

datawhale p2s 学习chapter_1与chapter_2

chapter_1

conda 安装配置

  1. 修改powershell执行策略(仅win10+非家庭版本需要)

    进入windows powershell后输入:

    Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

    A

    并在Anaconda Powershell Prompt中输入:

    conda init

  2. 更换镜像源

    • pip换源
      在Anaconda Powershell Prompt中

      pip config set global.index-url <url>

    • conda换源
      在复制镜像站源文本后输入执行以下内容:

      conda config --set show_channel_urls yes

      notepad .condarc

      conda clean -i

      或者仅输入以下内容

      conda config --add channels <url>

  3. conda虚拟环境构建

    • 创建环境

      conda create -n <env_name> python=x.xx(x.xx为python版本号)

    • 激活环境

      conda activate <env_name>

    • 退出环境

      conda deactivate

    • 克隆环境

      conda create -n <env_name_new> --clone <env_name_old>

    • 删除环境

      conda remove -n <env_name> --all

    • 在环境中安装额外的包

      conda install -n <your_env_name> [package]

    • 在环境中删除某个包

      conda remove --name <your_env_name> [package]

pip 安装配置

  1. pip安装说明

    在terminal中输入 python get-pip.py 即安装完成。

  2. pip使用说明

    • 安装第三方库或包

      pip install <Library/package>

    • 卸载第三方库或包

      pip uninstall <Library/package>

    • 验证写在结果

      pip list查看list中是否有已卸载的第三方库,如没有,即卸载成功

VSCode插件清单

  • Python
  • Jupyter
  • Office Viewer(Markdown Editor)
  • Black Formatter
  • Code Runner
  • Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code

云端环境

chapter_2

  1. Hello,World

    print("Hello,World") 
    
  2. 注释

    • 单行注释,使用 # 开头

    • 多行注释,使用 ''' 或 """ 包裹起来(单引号(')与双引号(")在 Python 中并无太大区别)

      #单行注释
      
      '''
      多1234567890
      行1234567890
      注1234567890
      释1234567890
      '''
      
      """
      多1234567890
      行1234567890
      注1234567890
      释1234567890
      """
      
    • 作用:解释和说明, # magic comment 除外。

  3. 输出

    • print()作用是将填入的内容显示在 Console 中,默认每次输入后会换行

    • end参数控制结尾

      print("Data", end="*")
      print("whale")
      
    • sep参数控制分隔

      print("Data", "whale", sep="*")
      
    • 输出中可以有运算

      print("p2s"*2,"data"*3, sep="/"*4)
      print("Data"+"whale"+"P2S")
      
    • f-strings:

      print(f"一个简单的数学问题:\"{x} + {y} = ?\",答案是 {x+y}!")
      
    • 多行输出

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

    • 语法错误 Syntax Errors,编译时出错

      print("哦不!) # Error! 缺少结尾引号
      
    • “运行时”错误 Runtime Errors == Crash,运行过程中出错

      print(1/0) # Error! 0 被作为除数
      
    • 逻辑错误 Logical Errors,不是想要的结果

      print("2+2=5")
      
  5. 输入与导入

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

      x = input("输入一个数字")
      x = int(x)
      x = int(input("输入一个数字:"))
      print(x, "的一半等于", x/2)
      
    • 一行多个输入值,可以在结尾加上 split(),默认分隔参数是空格,可以更改,如:split(",")

      a, b = input().split("*")
      print(f"a = {a}, b = {b}")
      
    • import 来导入库

      import math
      print(math.factorial(20))
      
  6. 浮点数误差:二进制运算导致,不可避免,只可减少。