python学习笔记

VSCode安装

  1. 在官网选择对应的安装包下载(Windows x64)。
  2. 运行后,按照引导安装。
  3. 安装成功后,在左侧“extensions”搜索Chinese语言包,点击install,按照提示重启VSCode。
  4. 在左侧“扩展”搜索python,安装python扩展。
  5. 在资源管理器页面创建新文件,创建好文件编写好第一个代码后,在页面右下角单元格左侧有一段文字,点击可以切换python运行内核。

Miniconda安装及使用

  1. 安装: 务必勾选“Add Miniconda3 to my PATH environment variable”。
  2. 创建环境:
     

    shell

    conda create -n env_name python=3.10

  3. 进入环境:
     

    shell

    conda activate env_name

  4. 退出环境:
     

    shell复制

    conda deactivate

Jupyter Notebook 使用

安装:

 

shell

pip install jupyter

打开:

 

shell复制

jupyter notebook

python基础学习
基本数据类型

整型、浮点型、列表、布尔类型、元组、集合、字典类型

数据类型转换
  1. int()

    • 将其他类型转换为整数。
    • 常用于将浮点数、字符串等转换为整数。
     

    python

    int("123") # 123 int(123.456) # 123

  2. float()

    • 将其他类型转换为浮点数。
    • 常用于将整数、字符串等转换为浮点数。
     

    python

    float("123.456") # 123.456 float(123) # 123.0

  3. str()

    • 将其他类型转换为字符串。
    • 常用于将数字、列表、元组等转换为字符串。
     

    python

    str(123) # "123" str(123.456) # "123.456"

  4. list()

    • 将其他类型转换为列表。
    • 常用于将字符串、元组等转换为列表。
     

    python

    list("hello") # ['h', 'e', 'l', 'l', 'o'] list((1, 2, 3)) # [1, 2, 3]

  5. tuple()

    • 将其他类型转换为元组。
    • 常用于将列表、字符串等转换为元组。
     

    python

    tuple("hello") # ('h', 'e', 'l', 'l', 'o') tuple([1, 2, 3]) # (1, 2, 3)

  6. dict()

    • 将其他类型转换为字典。
    • 常用于将列表或元组的键值对转换为字典。
     

    python

    dict([('name', 'Alice'), ('age', 25)]) # {'name': 'Alice', 'age': 25}

  7. set()

    • 将其他类型转换为集合。
    • 常用于将列表、元组等转换为集合。
     

    python

    set([1, 2, 3]) # {1, 2, 3} set((1, 2, 3)) # {1, 2, 3}

  8. bool()

    • 将其他类型转换为布尔值。
    • 常用于将数字、字符串等转换为布尔值。
     

    python复制

    bool(0) # False bool(1) # True bool("") # False bool("hello") # True 

 

for 循环

for 循环用于遍历序列(如列表、元组、字典、集合、字符串)或迭代其他可迭代对象。

基本语法:

 

python

for item in iterable: # 执行代码块

示例:

 

python

fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)

循环控制语句:

  • break:跳出循环
  • continue:跳过当前循环的剩余部分,进入下一次循环

示例:

 

python

for i in range(5): if i == 3: break print(i)

while 循环

while 循环根据条件重复执行代码块,直到条件不再为真。

基本语法:

 

python

while condition: # 执行代码块

示例:

 

python

count = 1 while count < 3: print(count) count += 1

循环控制语句:

  • break:跳出循环
  • continue:跳过当前循环的剩余部分,进入下一次循环

示例:

 

python

count = 0 while True: count += 1 if count > 3: break print(count)

 字符串格式化

传统格式化(% 操作符)
 

python

name = "Alice" age = 30 print("Hello, %s. You are %d years old." % (name, age))

 

python

name = "Alice" age = 30 print("Hello, {}. You are {} years old.".format(name, age))

格式化字典

 

python

person = {"name": "Alice", "age": 30} print("Hello, {name}. You are {age} years old.".format(**person))

格式化列表

 

python

people = ["Alice", 30] print("Hello, {0}. You are {1} years old.".format(*people))

f-string(格式化字符串字面量)

Python 3.6 引入了 f-string,提供了更简洁和直观的格式化方式。

 

python

name = "Alice" age = 30 print(f"Hello, {name}. You are {age} years old.")

表达式内嵌
 

python

print(f"Hello, {name.upper()}. You are {age * 2} years old in dog years.")

访问字典和列表
 

python

person = {"name": "Alice", "age": 30} print(f"Hello, {person['name']}. You are {person['age']} years old.")

类似于 str.format(),但使用 format_map() 可以格式化字典映射。

 

python

from string import Template t = Template('Hello, ${name}. You are ${age} years old.') print(t.safe_substitute(name="Alice", age=30))

 格式化数字
浮点数格式化
 

python

num = 123.4567 print(f"{num:.2f}") # 输出: 123.46

整数格式化
 

python

num = 123456 print(f"{num:08d}") # 输出: 000123456

格式化输出的控制
填充和对齐
 

python

print(f"{'hello':<10}") # 左对齐 print(f"{'hello':^10}") # 居中对齐 print(f"{'hello':>10}") # 右对齐

宽度和精度
 

python

print(f"{123456789:10}") # 宽度为10 print(f"{123.456789:.2f}") # 浮点数,保留两位小数

格式化输出的类型
布尔值
 

python

is_true = True print(f"{is_true}")

列表和元组
 

python

print(f"{[1, 2, 3]}") print(f"{(1, 2, 3)}")

字典
 

python

print(f"{dict(a=1, b=2)}")

格式化输出的实用技巧
条件表达式
 

python

is_active = True print(f"User is {'active' if is_active else 'inactive'}.")

调用函数
 

python

def greet(name): return f"Hello, {name}!" print(greet("Alice"))

格式化输出的组合
 

python复制

num1 = 10 num2 = 20 print(f"The sum of {num1} and {num2} is {num1 + num2}.")

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值