[特殊字符] Python学习第一天:从零开始的基础语法

👋 大家好!这是我学习Python的第一天笔记,希望能帮助同样刚入门的小伙伴。文中会有代码+输出对比,还有小贴士练习建议哦!

目录

  • 📝 1. 注释:代码的“说明书”

  • 📦 2. 数据类型与容器

    • 2.1 列表(List)

    • 2.2 集合(Set)

    • 2.3 字典(Dict)

  • 🔄 3. 循环结构:让代码“动”起来

    • 3.1 使用range()生成数列

    • 3.2 多种方式创建列表

  • 📤 4. 输入输出:与用户对话

  • ✨ 5. 字符串魔法:让文字更美

    • 5.1 大小写转换

    • 5.2 f-string格式化(Python 3.6+)

    • 5.3 字符串修剪

    • 5.4 删除前缀/后缀(Python 3.9+)

  • 🎯 6. 字符串格式化练习

  • 🔢 7. 数字类型:整数和浮点数

  • 🧱 8. 类的初步认识

    • 8.1 对象的内置方法

  • 📚 今日学习总结

  • 📢 写在最后


📝 1. 注释:代码的“说明书”

Python中有两种注释方式:

python

# 这是单行注释,用井号开头

"""
这是多行注释
可以用三个双引号
包含多行内容
"""

💡 小贴士:好的注释能让代码更易读,但不要过度注释——“代码即文档”才是最高境界!


📦 2. 数据类型与容器

2.1 列表(List)

python

l = []           # 空列表
l = list()       # 也是空列表
l = [1, 2, 3, 4, 5]  # 有初始值的列表

2.2 集合(Set)

python

s = {}           # ⚠️ 注意:这是空字典,不是集合!
s = set()        # ✅ 这才是空集合
s = {1, 2, 3}    # 有初始值的集合

2.3 字典(Dict)

python

d = {1: 2}       # 键为1,值为2的字典
d = dict()       # 空字典

🔄 3. 循环结构:让代码“动”起来

3.1 使用range()生成数列

python

# 生成1到100的自然数列表
l = range(1, 101)  # 注意:range是左闭右开,所以是1-100

# 方法1:直接遍历
for i in l:
    print(i, end=' ')  # end=' '让输出不换行

输出示例(只显示前10个)

text

1 2 3 4 5 6 7 8 9 10 ...

3.2 多种方式创建列表

python

# 方法2:列表推导式
l = [x for x in range(1, 101)]
print(l[:10])  # 只打印前10个

# 方法3:append()方法
a = []
for i in range(1, 101):
    a.append(i)
print(a[:10])

# 方法4:列表拼接
b = []
for i in range(1, 101):
    b += [i]  # 注意:这里要加[i]而不是i
print(b[:10])

输出对比

text

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # 三种方法结果相同

⚡ 性能小知识:列表推导式通常比append()更快,而b += [i]的方式最慢(会创建新列表)。


📤 4. 输入输出:与用户对话

python

# 简单输出
print("simple message")

# 变量输出
message = "simple message"
print(message)

🔄 交互示例

python

name = input("请输入你的名字:")
print(f"你好,{name}!欢迎学习Python!")

text

请输入你的名字:小明
你好,小明!欢迎学习Python!

✨ 5. 字符串魔法:让文字更美

5.1 大小写转换

python

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"

print(full_name.lower())     # 全部小写
print(full_name.upper())     # 全部大写
print(full_name.title())     # 首字母大写

输出

text

ada lovelace
ADA LOVELACE
Ada Lovelace

5.2 f-string格式化(Python 3.6+)

python

print(f"Hello, {full_name.title()}!")

输出

text

Hello, Ada Lovelace!

⚠️ 注意陷阱

python

# 错误示范
print("hello,full_name.title()!")  # 不会替换变量
print(f"hello,full_name.title()!")  # 还是不会替换?不对!

# 正确写法
print(f"hello,{full_name.title()}!")

输出对比

text

hello,full_name.title()!        # 字符串原样输出
hello,full_name.title()!        # 没有{}就不会解析
hello,Ada Lovelace!             # ✅ 正确使用{}

5.3 字符串修剪

python

# 去除空格
a = ' python '
print(f"'{a.strip()}'")    # 去除两端空格
print(f"'{a.lstrip()}'")   # 去除左端空格
print(f"'{a.rstrip()}'")   # 去除右端空格

输出

text

'python'
'python '
' python'

5.4 删除前缀/后缀(Python 3.9+)

python

# 删除前缀
url = "https://sophia"
domain = url.removeprefix('https://')
print(domain)  # sophia

# 删除后缀
filename = 'python_note.txt'
name = filename.removesuffix('.txt')
print(name)  # python_note

🎯 6. 字符串格式化练习

python

a = 'Sophia'
b = "111"

# 多种格式化方式
print(f'hi {a}, would you like to learn some Python today?')
print(f"{a} said, '{b}'")
print("hi {}, would you like to learn some Python today?".format(a))
print("%s said, '%s'" % (a, b))

输出

text

hi Sophia, would you like to learn some Python today?
Sophia said, '111'
hi Sophia, would you like to learn some Python today?
Sophia said, '111'

🏆 推荐:f-string最简洁直观,是Python 3.6+的首选!


🔢 7. 数字类型:整数和浮点数

python

# 乘方运算
print(2 ** 3)      # 8
print(2 ** 0.5)    # 1.4142135623730951(平方根)

# 类型转换规则
print(3 + 2)       # 5 (整数)
print(3 + 2.0)     # 5.0 (浮点数)
print(3 / 2)       # 1.5 (除法结果是浮点数)
print(3 // 2)      # 1 (整数除法)

# 同时赋值
x, y, z = 1, 2, 3
print(x, y, z)     # 1 2 3

🔍 观察规律

  • 只要有浮点数参与运算,结果就是浮点数

  • 除法(/)即使两个整数相除,结果也是浮点数

  • 整数除法(//)会向下取整


🧱 8. 类的初步认识

python

class File:
    def __init__(self):
        self.a = 0
        self.b = 1.9
        self.c = 'fda'
    
    def remove(self):
        print('call the method remove()')

# 创建对象
f = File()
f2 = File()

# 调用方法
f.remove()
f2.remove()

输出

text

call the method remove()
call the method remove()

8.1 对象的内置方法

python

a = 1
print(a.bit_length())  # 1的二进制是'1',长度是1
print((255).bit_length())  # 255的二进制是'11111111',长度是8

输出

text

1
8

📚 今日学习总结

知识点 掌握程度 重点内容
注释 ✅✅✅ 单行#,多行"""
列表/集合/字典 ✅✅ 创建方式、区别
循环 ✅✅✅ forrange()
字符串操作 ✅✅✅ 大小写、f-string、修剪
数字类型 ✅✅ 类型转换规则
类与对象 初步认识

📢 写在最后

这是我学习Python的第一天笔记,欢迎在评论区留言交流!如果你也是Python初学者,可以一起打卡学习;如果你是大神,也请多多指教!


📌 互动提示:觉得有帮助的话点个👍,有疑问评论区见!我们下期再见~ 👋


这份Markdown文章包含了:

  • ✅ 清晰的层级结构(#、##、###)

  • ✅ 代码块与输出对比

  • ✅ 折叠式小练习(互动感)

  • ✅ 表格总结

  • ✅ 课后作业和挑战题

  • ✅ 表情符号增加可读性

  • ✅ 结尾互动引导