当前位置: 首页 > news >正文

二、Python提供了丰富的内置工具,无需额外安装即可使用

Python提供了丰富的内置工具,无需额外安装即可使用,极大地提高了编程效率。以下是常用内置工具的汇总介绍和完整示例:

解释器(Interpreter)
  • 介绍:Python解释器是运行Python代码的核心工具,常见的有CPython、Jython和IronPython等。
  • 示例
    # 在命令行中输入python进入交互式环境
    python
    >>> print("Hello, World!")
    Hello, World!
    
文本编辑器(Text Editors)
  • 介绍:用于编写Python代码的文本编辑器,如Notepad、TextEdit、Sublime Text、Atom等。
  • 示例
    • 在Sublime Text中编写并保存以下代码为hello.py
      print("Hello, World!")
      
    • 在命令行中运行:
      python hello.py
      
集成开发环境(IDEs)
  • 介绍:提供全面开发功能的IDE,如PyCharm、Visual Studio Code(配合Python插件)。
  • 示例
    • 在PyCharm中创建新项目,编写Python代码,并使用其调试功能。
内置函数(Built-in Functions)
  • 数据类型转换
    • int()float()str()bool()list()tuple()set()dict()
    • 示例
      num_str = "10"
      num_int = int(num_str)
      print(num_int)  # 输出:10num_float = 3.14
      num_str = str(num_float)
      print(num_str)  # 输出:"3.14"
      
  • 数学运算
    • abs()round()pow()max()min()sum()divmod()
    • 示例
      num = -5
      abs_num = abs(num)
      print(abs_num)  # 输出:5nums = [1, 2, 3, 4]
      total = sum(nums)
      print(total)  # 输出:10
      
  • 序列操作
    • len()sorted()reversed()enumerate()zip()
    • 示例
      fruits = ["apple", "banana", "cherry"]
      for i, fruit in enumerate(fruits):print(i, fruit)
      # 输出:
      # 0 apple
      # 1 banana
      # 2 cherrynames = ["Alice", "Bob"]
      scores = [95, 85]
      combined = list(zip(names, scores))
      print(combined)  # 输出:[("Alice", 95), ("Bob", 85)]
      
  • 输入输出
    • print()input()
    • 示例
      name = input("请输入您的姓名:")
      print("您好," + name + "!")
      
  • 其他常用函数
    • type()range()map()filter()any()all()
    • 示例
      num = 42
      num_type = type(num)
      print(num_type)  # 输出:<class 'int'>numbers = [1, 2, 3, 4]
      squares = list(map(lambda x: x**2, numbers))
      print(squares)  # 输出:[1, 4, 9, 16]
      
内置模块
  • shelve
    • 介绍:提供简单的键值对存储机制,用于持久化Python对象。
    • 示例
      import shelve# 存储数据
      d = shelve.open("data.shelve")
      d["name"] = "Alice"
      d["age"] = 30
      d.close()# 读取数据
      d = shelve.open("data.shelve")
      print(d["name"])  # 输出:Alice
      print(d["age"])  # 输出:30
      d.close()
      
  • xml
    • 介绍:用于处理XML文档,包括解析、创建和操作XML元素树。
    • 示例
      import xml.etree.ElementTree as ET# 解析XML文件
      tree = ET.parse("example.xml")
      root = tree.getroot()
      for child in root:print(child.tag, child.attrib)
      
  • configparser
    • 介绍:用于读写INI格式的配置文件。
    • 示例
      import configparser# 创建配置文件
      config = configparser.ConfigParser()
      config["DEFAULT"] = {"ServerAliveInterval": "45"}
      config["COMPUTER1"] = {"Host": "alpha", "IP": "192.168.1.1"}
      with open("config.ini", "w") as configfile:config.write(configfile)# 读取配置文件
      config = configparser.ConfigParser()
      config.read("config.ini")
      print(config["COMPUTER1"]["Host"])  # 输出:alpha
      
  • hashlib
    • 介绍:提供各种哈希函数,如MD5、SHA1、SHA256等,用于计算字符串的哈希值。
    • 示例
      import hashlibmessage = "Hello, World!"
      hash_object = hashlib.sha256(message.encode())
      hex_dig = hash_object.hexdigest()
      print(hex_dig)
      
  • hmac
    • 介绍:用于生成消息认证码(HMAC),结合密钥和哈希函数,验证数据的完整性和来源。
    • 示例
      import hmac
      import hashlibkey = b"secret key"
      message = b"Hello, HMAC!"
      hmac_digest = hmac.new(key, message, hashlib.sha256).digest()
      print(hmac_digest)
      

这些内置工具涵盖了Python编程的各个方面,从基本的代码编写到数据处理和系统交互,都能提供强大的支持。熟练掌握它们,可以显著提升编程效率和代码质量。

http://www.xdnf.cn/news/733429.html

相关文章:

  • 6个月Python学习计划 Day 9 - 函数进阶用法
  • 【ROS2实体机械臂驱动】rokae xCoreSDK Python测试使用
  • 单卡4090部署Qwen3-32B-AWQ(4bit量化)-vllm
  • 网易 - 灵犀办公文档
  • const ‘不可变’到底是值不变还是地址不变
  • Python使用
  • C 语言中 * count++ 引发的错误与正确指针操作解析
  • Lyra学习笔记2 GFA_AddComponents与ULyraPlayerSpawningManagerComponent
  • 玛哈特校平机实战指南:选型、应用痛点
  • 【Unity】模型渐变技术 BlendShapes变形
  • 深入理解复数加法与乘法:MATLAB演示
  • 云原生微服务架构演进之路:理念、挑战与实践
  • Hive的数据倾斜是什么?
  • 杨传辉:构建 Data × AI 能力,打造 AI 时代的一体化数据底座|OceanBase 开发者大会实录
  • Armv7l或树莓派32位RPI 4B编译faiss
  • @Pushgateway自定义脚本推送数据
  • C++继承权限与访问控制详解
  • 解决win自动重启(自用,留链接)
  • Express教程【002】:Express监听GET和POST请求
  • 基于CAPL的DDS子消息解析- Data
  • golang 基于redis实现集群中的主实例选举
  • Nginx网站服务:从入门到LNMP架构实战
  • 生动形象理解CNN
  • 文件雕刻——一种碎片文件的恢复方法
  • 为什么建立 TCP 连接时,初始序列号不固定?
  • 日志技术-LogBack、Logback快速入门、Logback配置文件、Logback日志级别
  • Kubernetes 入门:安装 kubectl 并掌握基础命令
  • RK3568 OH5.1 编译运行程序hellworld
  • (22)大文件流式处理
  • 五星级酒店技能比赛主持稿串词