python基础第二篇

本文详细介绍了Python的基础操作,包括列表、元组、字符串、字典、集合的使用,以及文件操作和字符编码与转码。重点讨论了各种数据类型的特性与常见操作,如列表的增删改查、字符串的格式化输出、字典的增删改查和文件的读写模式。
摘要由CSDN通过智能技术生成

python基础 二月红

真心觉得python这门语言是业界良心:

  • 列表、元组操作
  • 字符串操作
  • 字典操作
  • 集合操作
  • 文件操作
  • 字符编码与转码

一、列表、元组操作

列表是最常用的数据类型之一,可实现最方便的存储和修改等操作。

定义列表

>>> your_name=["xzx","mm","xmm"]

通过下标访问

>>> your_name[0] 
'xzx'
>>> your_name[2] 
'xmm'
>>> your_name[-1] 
'xmm'
>>> names[-2] #还可以倒着取 
'mm' 

切片:取多个元素

>>> your_name=["xzx","mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name[1:4]#取下标1至4之间的数字,包括下标1,但不包括下标4 
["mm","xmm","xiaoxia"]
>>> your_name[1:-1] 
["mm","xmm","xiaoxia","miaoer"]
>>> your_name[:3]#从头开始可不用写0 
["xzx","mm","xmm"]
>>> your_name[1:]#取到最后就什么都不写
["mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name[::2]#全部取只用写一个:不用写下标,最后的2表示取数下标每次加2
["xzx","xmm","miaoer"]

追加

>>> your_name=["xzx","mm","xmm","xiaoxia","miaoer","xiaxia"]
>>> your_name.append("superxia")
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]

插入

>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> your_name.insert(2,"cutexia")#插入在2的位置
>>> your_name
["xzx","mm","cutexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]

修改

>>> your_name
["xzx","mm","cutexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> your_name[2]="no_facexia"
>>> your_name
["xzx","mm","no_facexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]

删除

>>> your_name
["xzx","mm","no_facexia","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> del your_name[2]
>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]

扩展

>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia"]
>>> add_name=["haha","hehe","heiheihei"]
>>> your_name.extend(add_name)
["xzx","mm","xmm","xiaoxia","miaoer","xiaxia","superxia","haha","hehe","heiheihei"]

复制

>>> your_name
["xzx","mm","xmm","xiaoxia","miaoer",
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值