花些时间把python入门<二>input、print、缩进

本文介绍了Python的基础语法,包括print()函数的使用,支持单引号和双引号输出字符串,以及如何通过逗号隔开输出多个字符串。同时,讲解了input()函数获取用户输入并将其存储为字符型变量,若需要数字型变量需进行类型转换。此外,文章强调了Python中缩进的重要性,缩进代表代码的所属关系,通常使用四个空格作为标准,并提醒开发者注意复制粘贴时要检查缩进的一致性。

1.输出函数 print()
括号里可以加单引号,双引号print(’ '),print(" “),还可以一次输出多个字符串,方法:在两个引号之间加逗号隔开即可,如print(” “,” ")
2.输入函数input()
用来获得用户的输入信息,并放储存到一个变量里,括号里可以加引号,作为一种输入的提示,一般用单引号。值得注意的是input()
函数得到的变量默认是字符型的,如果想获得数字型的变量,需要使用数据类型强制转换,将字符转换成数字。
3.关于缩进
python的注释采用#,缩进代表的是所属关系,一旦在语句后面加入冒号“:”,那在下一行般均采用缩进,缩进的个数软件没有强制要求,但大多数都默认采用四个空格进行缩进。需要注意的是,复制粘贴因为缩进不能随意使用,因此每次粘贴必须要检查缩进的个
数,确保相同区域的代码缩进保持一致。
方便的是,在文本编辑器里可以设置tab自动转换为四个空格。

#!/usr/bin/env python3 >>> """ ... NumPy 环境修复工具 ... 执行步骤: ... 1. 检查当前环境问题 ... 2. 清理并重装 NumPy ... 3. 验证修复结果 ... """ '\nNumPy 环境修复工具\n执行步骤:\n1. 检查当前环境问题\n2. 清理并重装 NumPy\n3. 验证修复结果\n' >>> import sys >>> import subprocess >>> import platform >>> >>> def run_cmd(command): ... """执行 shell 命令并返回输出""" ... result = subprocess.run( ... command, ... shell=True, ... stdout=subprocess.PIPE, ... stderr=subprocess.PIPE, ... text=True ... ) ... return result.stdout, result.stderr ... File "<python-input-6>", line 3 result = subprocess.run( IndentationError: unexpected indent >>> def check_numpy(): ... """诊断 NumPy 状态""" ... print("="*50) ... print("STEP 1: 环境诊断") ... print("="*50) ... File "<python-input-7>", line 3 print("="*50) IndentationError: unexpected indent >>> # 检查 Python 版本 >>> py_version = f"{sys.version_info.major}.{sys.version_info.minor}" File "<python-input-9>", line 1 py_version = f"{sys.version_info.major}.{sys.version_info.minor}" IndentationError: unexpected indent >>> print(f"[INFO] Python 版本: {platform.python_version()}") File "<python-input-10>", line 1 print(f"[INFO] Python 版本: {platform.python_version()}") IndentationError: unexpected indent >>> >>> # 检查操作系统 >>> os_info = f"{platform.system()} {platform.release()} ({platform.machine()})" File "<python-input-13>", line 1 os_info = f"{platform.system()} {platform.release()} ({platform.machine()})" IndentationError: unexpected indent >>> print(f"[INFO] 操作系统: {os_info}") File "<python-input-14>", line 1 print(f"[INFO] 操作系统: {os_info}") IndentationError: unexpected indent >>> >>> # 尝试导入 NumPy >>> try: File "<python-input-17>", line 1 try: IndentationError: unexpected indent >>> import numpy as np File "<python-input-18>", line 1 import numpy as np IndentationError: unexpected indent >>> print(f"[SUCCESS] NumPy 版本: {np.__version__}") File "<python-input-19>", line 1 print(f"[SUCCESS] NumPy 版本: {np.__version__}") IndentationError: unexpected indent >>> print("[TEST] 核心组件载: ", end="") File "<python-input-20>", line 1 print("[TEST] 核心组件载: ", end="") IndentationError: unexpected indent >>> np.array([1, 2, 3]) # 触发 multiarray 载 File "<python-input-21>", line 1 np.array([1, 2, 3]) # 触发 multiarray 载 IndentationError: unexpected indent >>> print("OK") File "<python-input-22>", line 1 print("OK") IndentationError: unexpected indent >>> return True File "<python-input-23>", line 1 return True IndentationError: unexpected indent >>> except ImportError: File "<python-input-24>", line 1 except ImportError: IndentationError: unexpected indent >>> print("[ERROR] NumPy 未安装") File "<python-input-25>", line 1 print("[ERROR] NumPy 未安装") IndentationError: unexpected indent >>> return False File "<python-input-26>", line 1 return False IndentationError: unexpected indent >>> except Exception as e: File "<python-input-27>", line 1 except Exception as e: IndentationError: unexpected indent >>> print(f"[FAIL] NumPy 载失败: {str(e)}") File "<python-input-28>", line 1 print(f"[FAIL] NumPy 载失败: {str(e)}") IndentationError: unexpected indent >>> return False File "<python-input-29>", line 1 return False IndentationError: unexpected indent >>> >>> def reinstall_numpy(): ... """重装 NumPy""" ... print("\n" + "="*50) ... print("STEP 2: 重新安装 NumPy") ... print("="*50) ... File "<python-input-31>", line 3 print("\n" + "="*50) IndentationError: unexpected indent >>> # 清理现有安装 >>> print("[ACTION] 卸载 NumPy...") File "<python-input-33>", line 1 print("[ACTION] 卸载 NumPy...") IndentationError: unexpected indent >>> stdout, stderr = run_cmd("pip uninstall -y numpy") File "<python-input-34>", line 1 stdout, stderr = run_cmd("pip uninstall -y numpy") IndentationError: unexpected indent >>> if "Successfully uninstalled numpy" in stdout: File "<python-input-35>", line 1 if "Successfully uninstalled numpy" in stdout: IndentationError: unexpected indent >>> print("[SUCCESS] NumPy 已卸载") File "<python-input-36>", line 1 print("[SUCCESS] NumPy 已卸载") IndentationError: unexpected indent >>> >>> # 安装兼容版本 >>> print("[ACTION] 安装最新稳定版...") File "<python-input-39>", line 1 print("[ACTION] 安装最新稳定版...") IndentationError: unexpected indent >>> run_cmd("pip install --no-cache-dir numpy") File "<python-input-40>", line 1 run_cmd("pip install --no-cache-dir numpy") IndentationError: unexpected indent >>> >>> # 验证安装 >>> stdout, _ = run_cmd("pip show numpy") File "<python-input-43>", line 1 stdout, _ = run_cmd("pip show numpy") IndentationError: unexpected indent >>> if "Version:" in stdout: File "<python-input-44>", line 1 if "Version:" in stdout: IndentationError: unexpected indent >>> print("[SUCCESS] NumPy 重装完成") File "<python-input-45>", line 1 print("[SUCCESS] NumPy 重装完成") IndentationError: unexpected indent >>> else: File "<python-input-46>", line 1 else: IndentationError: unexpected indent >>> print("[ERROR] 安装失败,请检查网络或权限") File "<python-input-47>", line 1 print("[ERROR] 安装失败,请检查网络或权限") IndentationError: unexpected indent >>> >>> def main(): ... """主修复流程""" ... if not check_numpy(): ... reinstall_numpy() ... print("\n" + "="*50) ... print("STEP 3: 最终验证") ... print("="*50) ... if not check_numpy(): ... \ print("\n[CRITICAL] 修复失败!建议:") ... \ print("1. 检查系统依赖: sudo apt-get install libatlas3-base libopenblas-base (Linux)") ... \ print("2. 使用虚拟环境: python -m venv .venv && source .venv/bin/activate") ... \ print("3. 更换 Python 版本 (推荐 3.8+)") ... \ sys.exit(1) ... \ File "<python-input-49>", line 3 if not check_numpy(): IndentationError: unexpected indent >>> print("\n[SUCCESS] 环境验证通过,请重启服务") File "<python-input-50>", line 1 print("\n[SUCCESS] 环境验证通过,请重启服务") IndentationError: unexpected indent >>> >>> if __name__ == "__main__": ... main() ... Traceback (most recent call last): File "<python-input-52>", line 2, in <module> main() ^^^^ NameError: name 'main'
09-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是不想再当小白的黑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值