[Python]DAY2 准备好一切,让我们开始前5节小课

223 阅读2分钟

一、使用阿里云仓库

python毕竟是外国人发明的语言,很多相关的依赖和库都在国外,为了使依赖更快可以下载,需要用到阿里云仓库

1.1设置阿里云仓库:

pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/

1.2验证设置:

pip3 config list

1.3取消设置(一般不用取消)

pip3 config unset global.index-url

二、5节小课

2.1 学习写一个Hello World

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# python的学习从hello开始
# 1. 定义print_name函数
def print_name(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}, 认识你很高兴')  # Press ⌘F8 to toggle the breakpoint.


# 2. 使用方法
# 预计输出
# Hi, Jake, 认识你很高兴
print_name('Jake')

2.2 知道如何定义变量和赋值

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. 定义变量
one = 1
two = 2
ten = 10


# 2. 定义方法
def print_variable():
    print(one)
    print(two)
    print(ten)


# 2. 执行print_variable方法
# 预计输出
# 1
# 2
# 10
print_variable()

2.3 简单计算

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. 定义变量
one = 1
two = 2
ten = 10


# 2. 定义计算calculate方法
def calculate(name):
    print(f'{name} 计算 {one} + {two} =', one + two)  # 计算 1 + 2 = 3
    print(f'{name} 计算 {two} - {one} =', two - one)  # 计算 2 - 1 = 1
    print(f'{name} 计算 {ten} * {two} =', ten * two)  # 计算 10 * 2 = 20
    print(f'{name} 计算 {ten} / {two} =', ten / two)  # 计算 10 / 2 = 5.0


# 3. 执行calculate方法
# 预计输出
# Jake 计算 1 + 2 = 3
# Jake 计算 2 - 1 = 1
# Jake 计算 10 * 2 = 20
# Jake 计算 10 / 2 = 5.0
calculate('Jake')

2.4 逻辑判断

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. 定义变量
one = 1
two = 2
ten = 10


# 2. 定义计算condition方法
def condition(currentVal):
    if currentVal >= one:
        print(f'计算 {currentVal} >= {one}:', currentVal >= one)
    elif currentVal >= two:
        print(f'计算 {currentVal} >= {two}:', currentVal >= two)
    elif currentVal >= ten:
        print(f'计算 {currentVal} >= {ten}:', currentVal >= ten)
    else:
        print(f'计算 当前值小于1,当前值:{currentVal}')


# 3. 执行condition方法
# 计算 当前值小于1,当前值:0
# 计算 1 >= 1: True
# 计算 2 >= 1: True
# 计算 10 >= 1: True
condition(0)
condition(1)
condition(2)
condition(10)

2.5 循环使用

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. 定义变量
one = 1
two = 2
ten = 10
twenty = 20


# 2. 定义loop函数
# 打印 1-max 中的奇数
def for_loop(max):
    for i in range(one, max):
        if i % 2 == 1:
            print(i)
    print('---for_loop end---')


# 输入大于10的数,然后1个个倒数,直到不再大于10
def while_loop(max):
    while ten < max:
        max -= 1
        print(max)
    print('---while_loop end---')


# 九九乘法口诀表
def nested_loop():
    for i in range(one, ten):
        for j in range(one, ten):
            if j >= i:
                print(f'{i} * {j} =', i * j)
    print('')


# 循环断点,大于3的不打印
def break_loop():
    for i in range(one, ten):
        if i > 3:
            break
        print(i)
    print('')


# 循环断点,跳过3其他都打印
def continue_loop():
    for i in range(one, ten):
        if i == 3:
            continue
        else:
            print(i)
    print('')


# 3. 执行calculate方法
# 计算 当前值小于1,当前值:0
# 计算 1 >= 1: True
# 计算 2 >= 1: True
# 计算 10 >= 1: True
for_loop(ten)
while_loop(twenty)
nested_loop()
break_loop()
continue_loop()