Python 数据类型转换 All In One
1.Python List Comprehensions All In One2.Python 3 List Type errors All In One3.Python Files All In One4.Python exceptions All In One5.Python decorator method and decorator property All In One6.Python data hiding All In One 7.Python Magic Methods & Operator Overloading All In One8.How to fix the for...in loop errors in Python All In One9.How to check function arguments type in Python All In One10.Python rpi_ws281x library All In One11.Python function argument All In One12.How to use variable in Python String All In One13.How to change the default Python2 to Python3 on Linux All In One14.Python & PEP All In One15.How to use pip3 install the latest version package All In One16.Python for loop with index All In One17.Python try...catch All In One18.Python OOP & Class private method All In One19.Python range function All In One20.pip3 & python3 -m pip All In One21.Python 3 alias All In One22.Python Ternary Operator All In One23.The principle of uploading files with command line tools All In One24.Python __init__() method & __init__.py file All In One25.Python relative import local package module file All In One26.Python check whether a list includes some value All In One27. Python timezone package All In One28.Python script get date and time All In One29.如何把一个 Python 项目包发布到 PyPI 上指南教程 All In One30.Python 3 vs Python 2 All In One31.Python 脚本接收命令行参数的多种方式 All In One
32.Python 数据类型转换 All In One
33.Python errors All In One34.Python 元组解构 All In One35.Python custom modify the __add__ method All In One36.Python 3 function & *args & **kwargs All In One37.Python list methods All In One38.Python 字符串插值 All In One39.Pycharm 如何自定义新建的 Python 文件的注释模版 All in One40.Python & PEP 8 & Style Guide All In One41.小甲鱼 All In One42.PyScript All In One43.QRcode ORC All In One44.Python 3 Data Types All In One45.free Python ebook & videos46.Python API Frameworks All In One47.Python Read JSON File48.2020~2021 职业规划书49.Python Web Framework All In One50.Python errors All In One51.如何使用 Python 编写后端 API 接口52.Python Coding Interview All In One53.Jupyter All In One54.How to use PyPI to publish a Python package All In One55.Python3 & Decorators with arguments & @Decorators with arguments bug56.Python Lambda & Functional Programming57.Python & file operation mode58.Python Turtle59.Python 2 to Python 3 convert60.Spyder & Kite61..pyc & Python62.Python Learning Paths63.Python Web Frameworks64.PEP 8 & Style Guide65.Python Crawler All In One66.Python Quiz & Python Exercise67.Anaconda68.Python module all in one69.PIP & Python packages management All In One70.Versatile Python 3.x71.Python Tutorials72.Flask73.selenium & python74.如何抓取电商的数据 & Python75.Python & dict & switch...case All In One76.Spyder & Python All In One77.How to install python3 on macOS All In One78.NLP & AI79.macOS & Python & Redis All In One80.Python & Spider81.How to run multiple Python versions on Windows?82.MySQL & Python83.Python file 操作 open 官方的文档84.Python关键字查询85.常见算法:python 一个简单的方法来实现Fibonacci序列只用迭代器,没有任何复杂的递归数据结构!86.如何在pycharm中切换python版本(2/3) 的图解教程87.修复 PyCharm 使用中文字符 Python 报错的完美解决方案 All In One88.how to updating Node.js and npm89.PEP 8 -- Style Guide for Python Code All In One90.如何在 Python 中使用 UTF-8 编码 All In One91.sphinx 文档生成器 (基于 python )92.使用 ReStructuredText + Sphinx + Python 开发wiki ebooks!93.如何在Eclipse正确安装配置PyDev插件的官方教程,以及error 问题的解决方法:94.在线的代码托管平台 coding.net ===中国扩展版github95.python ( pycharm EDU)Python 数据类型转换 All In One
Python 3
数据类型转换
函数 | 描述 |
---|---|
int(x [,base]) | 将x转换为一个整数 |
float(x) | 将x转换到一个浮点数 |
complex(real [,imag]) | 创建一个复数 |
str(x) | 将对象 x 转换为字符串 |
repr(x) | 将对象 x 转换为表达式字符串 |
eval(str) | 用来计算在字符串中的有效Python表达式,并返回一个对象 |
tuple(s) | 将序列 s 转换为一个元组 |
list(s) | 将序列 s 转换为一个列表 |
set(s) | 转换为可变集合 |
dict(d) | 创建一个字典。d 必须是一个 (key, value)元组序列。 |
frozenset(s) | 转换为不可变集合 |
chr(x) | 将一个整数转换为一个字符 |
ord(x) | 将一个字符转换为它的整数值 |
hex(x) | 将一个整数转换为一个十六进制字符串 |
oct(x) | 将一个整数转换为一个八进制字符串 |
demos
#!/usr/bin/env python3
# coding: utf8
import RPi.GPIO as GPIO
import time
import sys
arg1 = sys.argv[1]
print("arg1 =", arg1);
# 指定 BCM 模式下的GPIO 针脚编号
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)
# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)
# index
i = 0
# max
# n = 7
# 类型转换,str => int
n = int(arg1)
print("n =", n)
print('开始闪烁⏳')
while (i < n):
print("i =", i)
# 高电平,LED 点亮
GPIO.output(PIN, GPIO.HIGH)
# 休眠 1 秒,防止 LED 长时间点亮烧坏了
time.sleep(1.0)
# 低电平,LED 熄灭
GPIO.output(PIN, GPIO.LOW)
# 休眠 1 秒
time.sleep(1.0)
i = i + 1
# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()
print('结束闪烁 👌🏻')
Linux shell #!
指定脚本解释器的路径
#!/usr/bin/env python3
# ✅ 推荐写法, 动态读取 env 配置的解释器路径,切换系统环境不会报错,可移植性高 🚀
#!/usr/bin/python3
# 👎 不推荐写法,写死了解释器路径,切换系统环境有可能会报错,可移植性低
https://www.runoob.com/python3/python3-tutorial.html
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17273620.html
未经授权禁止转载,违者必究!
合集:
Python Script
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2022-03-30 React Animation All In One
2022-03-30 TypeScript namespace All In One
2022-03-30 macOS Monterey 12.3 All In One
2021-03-30 uni-app & nvue & cli
2021-03-30 如何使用 Apple Watch 上 NFC 功能复制门禁卡 All In One
2020-03-30 @bind decorator
2020-03-30 TypeScript & global.d.ts All In One