聪明办法学Python_task1_11.20-11.21

发布时间 2023-11-21 18:42:58作者: RS_xiaoyu

聪明办法学Python_task1_11.20-11.21

1.task01:Python简介/安装

1.1 Python灵魂三问


为什么学Python?

  • Python是全球最流行的编程语言之一
  • Python是学习人工智能的最佳选择
  • Python比C语言更适合入门编程

什么是Python?

  • 一门在2023年最适合入门人工智能的编程语言

怎么学Python?

1.2 Python环境配置

安装清单

  • miniconda

    • 安装
      • PATH
      • Clear the package cache
    • 配置
      • 进入Anaconda Powershell Prompt

          conda init
          conda config --set show_channel_urls yes
          notepad .condarc #C:\Users\[username]\.condarc
        
      • 粘贴以下内容(来源:清华大学开源镜像站)

        channels:
        - defaults
        show_channel_urls: true
        default_channels:
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
        custom_channels:
        conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
        deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
        
      • 回到Anaconda Powershell Prompt

          conda clean -i
        
  • 使用

    • 创建环境:conda create -n [envname] python=3.10
    • 激活环境:conda activate [envname]
    • 退出环境:conda deactivate
    • 移除环境:conda remove -n [envname] -all
  • vscode

  • git

  • 课程资料下载
    git clone https://github.com/datawhalechina/learn-python-the-smart-way-v2.git --depth=1

2.task02

2.1 注释

  • 后跟单行注释

  • ''' '''或""" """中包含多行代码

看别人代码:为什么不写注释?
看自己代码:为什么要写注释?

2.2 基本控制台输出 print()函数

print(123)      #无引号 输出数字和公式。

print('test')
print("test")   #单/双引号 输出任意字符。

print("""
hello,
world!""")      #三引号 输出有换行的字符。

print(arg1,arg2,...,sep=' ',end='\n')  
#多个变量默认以空格分隔,以换行结尾  

print("this is a \"test\"!")
#转义字符'\' 输出:this is a ”test“!

x,y=1,2
print(f"{x}+{y}={x+y}")
#格式化输出变量 输出:1+2=3

2.3 错误

  • 语法错误/Syntax Error #运行前出错 程序不运行
  • 运行时错误/Runtime Error #运行时出错 程序退出
  • 逻辑错误/Logical Error #运行时不报错 程序逻辑错误

2.4 基本控制台输入 input()函数

  • 以字符串形式返回

  • 多个输入 split()分割
    默认以空格、制表符、换行符分割
    split(',')可指定','分割

    a,b=input().split()#将输入的两个变量分别存储在a,b变量中
    

2.5 导入模块

import [libname]
import [libname] as [name]
from [libname] import [module]