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. 条件语句

    复制代码
    if condition: 
        # Do something 
    elif another_condition: 
        # Do something else 
    else: 
        # Do a different thing
  7. 循环

    复制代码
    #它for循环和while循环。
    
    for item in my_list: 
        print(item) 
    
    while condition: 
        # Loop body
  8. 函数定义

    复制代码
    def my_function(param1, param2): 
        # Function body return result
  9. 类和对象

    复制代码
    class MyClass: 
        def __init__(self, attribute): 
            self.attribute = attribute 
    
        def my_method(self): 
            # Method body
  10. 模块和包

    复制代码
      # 导入和使用模块。
      import module_name from package import module
  11. 异常处理

    复制代码
    try: 
        # Try to do something except Some
    Exception as e: 
        # Handle exception 
    finally: 
        # 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. 装饰器

    复制代码
    def my_decorator(func): 
        def wrapper(*args, **kwargs): 
            # Do something before 
            result = func(*args, **kwargs) 
            # Do something after 
            return result 
        return wrapper 
    
    @my_decorator 
    def my_function(): 
        # Function body
  16. Lambda函数

    复制代码
    lambda arguments: expression
  17. 三元运算符

    复制代码
    value_if_true if condition else value_if_false
  18. 全局和局部变量

    复制代码
    global my_global_var
  19. 文件操作

    复制代码
    with open('file.txt', 'r') as file: 
        content = file.read()
  20. 异步编程

    复制代码
    async def my_async_function(): 
        await some_async_operation()
  21. 类型注解

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

    复制代码
    @property 
    def my_property(self): 
        return self._my_attribute 
    
    @my_property.setter 
    def my_property(self, value): 
        self._my_attribute = value

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

相关推荐
李妍.2 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy2 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
2601_965958462 小时前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python
AI_小站2 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
微小冷3 小时前
在不同空间中展示数据(欧式、球面、庞加莱圆盘)
python·双曲空间·微分几何·geomstats·庞加莱圆盘·球面
felixking3 小时前
C++20 协程
开发语言·c++·协程
weixin_431600443 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
讲温控就好了4 小时前
从医疗影像到能源存储——芯片冷却温控技术的跨行业赋能
人工智能·python·能源
DeepVisionary4 小时前
从 Qwen3.8-27B 把原生多模态、原生 FP8、桌面级 Agent 三件事一次集齐,看开源大模型的“工业化拐点“
python·自动化
Zane1994124 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言