• python常见语法


    • 变量赋值

      my_var = 10

    1. 基本数据类型

      • 整数(int)、浮点数(float)、字符串(str)、布尔值(bool)、列表(list)、元组(tuple)、集合(set)、字典(dict)。
    2. 字符串

      s = 'This is a string in single quotes.' s = "This is a string in double quotes."
    3. 列表

      my_list = [1, 2, 3, 'Python']
    4. 元组

      my_tuple = (1, 2, 3)
    5. 字典

      my_dict = {'name': 'Python', 'version': 3.8}
    6. 条件语句

      1. if condition:
      2. # Do something
      3. elif another_condition:
      4. # Do something else
      5. else:
      6. # Do a different thing
    7. 循环

      1. #它for循环和while循环。
      2. for item in my_list:
      3. print(item)
      4. while condition:
      5. # Loop body
    8. 函数定义

      1. def my_function(param1, param2):
      2. # Function body return result
    9. 类和对象

      1. class MyClass:
      2. def __init__(self, attribute):
      3. self.attribute = attribute
      4. def my_method(self):
      5. # Method body
    10. 模块和包

        1. # 导入和使用模块。
        2. import module_name from package import module

    11. 异常处理

      1. try:
      2. # Try to do something except Some
      3. Exception as e:
      4. # Handle exception
      5. finally:
      6. # Clean-up code
    12. 列表推导式

      squares = [x**2 for x in range(10)]
    13. 字典推导式

      squares_dict = {x: x**2 for x in range(10)}
    14. 生成器表达式

      squares_gen = (x**2 for x in range(10))
    15. 装饰器

      1. def my_decorator(func):
      2. def wrapper(*args, **kwargs):
      3. # Do something before
      4. result = func(*args, **kwargs)
      5. # Do something after
      6. return result
      7. return wrapper
      8. @my_decorator
      9. def my_function():
      10. # Function body
    16. Lambda函数

      lambda arguments: expression
    17. 三元运算符

      value_if_true if condition else value_if_false
    18. 全局和局部变量

      global my_global_var
    19. 文件操作

      1. with open('file.txt', 'r') as file:
      2. content = file.read()
    20. 异步编程

      1. async def my_async_function():
      2. await some_async_operation()
    21. 类型注解

      def my_function(param1: int, param2: str) -> bool: # Function body
    22. 属性装饰器

      1. @property
      2. def my_property(self):
      3. return self._my_attribute
      4. @my_property.setter
      5. def my_property(self, value):
      6. self._my_attribute = value

    这些是Python编程中经常使用的语法元素。掌握这些基础对于进行有效的Python编程至关重要

  • 相关阅读:
    数据的处理包括哪些内容
    threejs 粒子系统和材质贴图
    Linux 下查看 VNC 连接状态
    STC单片机16——将输入信号2倍频
    Java8新特性: lambda 表达式介绍
    【经验模态分解】2.EMD的3个基本概念
    【C刷题】day6
    我的sql没问题为什么还是这么慢|MySQL加锁规则
    R的作图- -lm拟合结果图解释
    wireshark打开tcpdump抓的包 vwr: Invalid data length runs past the end of the record
  • 原文地址:https://blog.csdn.net/Kingairy/article/details/138138094