100个Python代码(一)

本文详细介绍了Python编程语言的基础概念,包括变量赋值、数学运算、字符串处理、列表与元组操作、条件语句、字典与集合、函数定义、类与对象、文件I/O以及错误和异常处理。
摘要由CSDN通过智能技术生成
  1. 打印输出:

     

    pythonCopy code

    print("Hello, world!")

  2. 变量赋值:

     

    pythonCopy code

    x = 10

  3. 基本数学运算:

     

    pythonCopy code

    addition = 5 + 5 subtraction = 10 - 5 multiplication = 4 * 3 division = 8 / 2 modulus = 7 % 3 exponentiation = 2 ** 3

  4. 字符串拼接:

     

    pythonCopy code

    greeting = "Hello" + " " + "World!"

  5. 列表创建:

     

    pythonCopy code

    my_list = [1, 2, 3, 4, 5]

  6. 列表添加元素:

     

    pythonCopy code

    my_list.append(6)

  7. 列表访问元素:

     

    pythonCopy code

    first_element = my_list[0]

  8. 列表切片:

     

    pythonCopy code

    sublist = my_list[1:3]

  9. 循环遍历列表:

     

    pythonCopy code

    for item in my_list: print(item)

  10. 条件语句:

     

    pythonCopy code

    if x > 5: print("x is greater than 5") elif x == 5: print("x is 5") else: print("x is less than 5")

  11. 字典创建和访问:

     

    pythonCopy code

    my_dict = {"name": "John", "age": 30} name = my_dict["name"]

  12. 字典添加或修改元素:

     

    pythonCopy code

    my_dict["city"] = "New York"

  13. 字典中的循环:

     

    pythonCopy code

    for key, value in my_dict.items(): print(f"{key}: {value}")

  14. 使用range函数:

     

    pythonCopy code

    for i in range(5): print(i)

  15. 列表推导式:

     

    pythonCopy code

    squares = [x**2 for x in range(10)]

  16. 函数定义和调用:

     

    pythonCopy code

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

  17. 使用*args和kwargs**:

     

    pythonCopy code

    def function_with_args(*args, **kwargs): print(args) print(kwargs) function_with_args(1, 2, 3, key1="value1", key2="value2")

  18. 类定义和实例化:

     

    pythonCopy code

    class MyClass: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!" my_object = MyClass("Bob") print(my_object.greet())

  19. 文件读写:

     

    pythonCopy code

    # 写入文件 with open("myfile.txt", "w") as file: file.write("Hello, file world!") # 读取文件 with open("myfile.txt", "r") as file: content = file.read() print(content)

  20. 错误和异常处理:

     

    pythonCopy code

    try: result = 10 / 0 except ZeroDivisionError: print("Divided by zero!") finally: print("This code block is always executed")

  21. 使用enumerate:

     

    pythonCopy code

    for index, value in enumerate(["a", "b", "c"]): print(index, value)

  22. 集合创建和操作:

     

    pythonCopy code

    my_set = {1, 2, 3} my_set.add(4) my_set.remove(2)

  23. 集合运算:

     

    pythonCopy code

    set_a = {1, 2, 3} set_b = {

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序老猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值