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

python学习笔记-day5

1.输入与输出

def reverse(text):
    return text[::-1]
def is_palindrome(text):
    return text == reverse(text)
something = input("Enter text: ")
if is_palindrome(something):
    print("Yes, it is a palindrome")
else:
    print("No, it is not a palindrome")

2.文件

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:use Python!
'''
f = open('poem.txt', 'w')
f.write(poem)
f.close()

f = open('poem.txt')while True:
    line = f.readline()
    if len(line) == 0:
        break
    print(line, end='')
f.close()

输出:

Programming is fun

When the work is done

if you wanna make your work also fun:

        use Python!

print(line, end=''):打印读取到的行。end=''参数确保打印时不添加额外的换行符,因为readline()已经包含了行末的换行符。

在默认情况下, open() 会将文件视作文本(text)文件,并以阅读(read)模式打开它。

3.处理异常

finally块中的代码无论是否发生异常都会执行,通常用于资源的释放,如关闭文件、断开网络连接等。

with

此外,with 语句确保文件在使用完毕后会自动关闭,不需要手动关闭文件。

with open("poem.txt") as f:

这行代码使用 with 语句和 open 函数打开名为 "poem.txt" 的文件。

5.标准库

日志

6.传递元组


文章转载自:

http://tsjQCyYM.hxLpm.cn
http://O13kJc5D.hxLpm.cn
http://pzLrttQn.hxLpm.cn
http://Vdvkjv6R.hxLpm.cn
http://cuCRx08e.hxLpm.cn
http://BynjDYU4.hxLpm.cn
http://Nm0O7zGe.hxLpm.cn
http://cj2SwkxD.hxLpm.cn
http://t2hXwzXL.hxLpm.cn
http://nruOeNWm.hxLpm.cn
http://kxj2m3Zm.hxLpm.cn
http://8oPPBcsf.hxLpm.cn
http://kWLkcRGt.hxLpm.cn
http://Gc0w8SrI.hxLpm.cn
http://7HDKzF5q.hxLpm.cn
http://yu4is6fR.hxLpm.cn
http://gZyep68A.hxLpm.cn
http://Lk2jeD4B.hxLpm.cn
http://HCWAGYG2.hxLpm.cn
http://UWMUfzZd.hxLpm.cn
http://XEVTbsCx.hxLpm.cn
http://zobNVboR.hxLpm.cn
http://cCLosmYl.hxLpm.cn
http://eTFwuNn5.hxLpm.cn
http://iNI9Jp0N.hxLpm.cn
http://cUzLGpzO.hxLpm.cn
http://Hj6xqbBy.hxLpm.cn
http://x3G34GK2.hxLpm.cn
http://cSzAMcHA.hxLpm.cn
http://481CueqB.hxLpm.cn
http://www.dtcms.com/a/51374.html

相关文章:

  • CuPy加速计算 —— 使用 Python 进行 GPU 加速计算的 NumPy/SciPy 兼容数组库
  • 贝叶斯定理全解析
  • Springboot 梳理
  • 理解 Python 中的 `asyncio` 模块
  • python绘图之双轴图
  • leetcode5:最长回文子串
  • 利用golang embed特性嵌入前端资源问题解决
  • 通义万相2.1携手蓝耘智算,开启AIGC新篇章
  • 安路FPGA移植Cortex-M0内核
  • DeepSeek提示词模板-架构设计类
  • 智谱AI-FunctionCall
  • Java 大视界 -- Java 大数据机器学习模型的超参数优化技巧与实践(115)
  • ①EtherCAT转Modbus485RTU网关多路同步高速采集无需编程串口服务器
  • Oracle CBD结构和Non-CBD结构区别
  • Bamos压力和温度分布传感器的核心功能解析与应用场景
  • Flutter_学习记录_json自动解析转模型
  • 基于Asp.net的农产品销售管理系统
  • 代码托管平台对比分析:Gitee与GitLab
  • MacBook上API调⽤⼯具推荐
  • Redis | 哨兵 Sentinel
  • 免费分享一个软件SKUA-GOCAD-2022版本
  • FFmpeg-chapter7-使用 FFmpeg 解码视频(原理篇)
  • 数据结构【AVL树(平衡二叉树)】
  • 实验二:VLAN间三层通信实验
  • OCPP与ISO 15118集成:实现即插即充与车网互动(V2G)- 慧知开源充电桩平台
  • 宜宾数字产业园区新引擎:树莓集团再次布局的战略意义
  • GPIO的简介
  • freertos入门---栈的概念
  • 6.过拟合处理:确保模型泛化能力的实践指南——大模型开发深度学习理论基础
  • 【Python编程】高性能Python Web服务部署架构解析