python基础1

python 一切皆对象

转义字符

\n 换行 newland

print('helloworld\nokk')

helloworld

okk

\r 回车 return 将前面的进行覆盖

print('hello\rworld')

world

\t 水平制表符 tab

print('hello\tword')

helloword

\b 退格 backspace 把斜杠前面的字符退没了

print('hello\bworld')

hellworld

range() 函数 用于生成一个整数序列

r = range(10) 一个参数

print(list(r))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 默认从零开始

t = range(4,8) 两个参数 第一个表示开始的数 第二个表示结束的数

print(list(t))

[4, 5, 6, 7]

u = range(1,10,3) 开始数 结束数 间隔数

print(list(u))

[1, 4, 7]

for-in 循环 in表达从(字符串、序列等)中依次取值,又称为遍历

for-in 遍历的对象必须是可迭代对象

for a in "Python":

print(a)

break 用于结束循环结构 通常余分支结构if 一起使用

continue 用于结束当前循环,进入下一个循环

列表的特点

列表元素按顺序有序排列

所有映射唯一数据

列表可以存储重复数据

任意数据类型混存

需要根据动态分配和回收内存

index 获取列表中指定元素的索引

ls = ['hello', 'world', '98', 'hello']

print(ls.index('hello'))

0

print(ls.index('hello',1,4)) 从索引为1到4的位置找 hello

3

in ; not in 判断元素在列表中是否存在

lst = [10, 20, "yes"]

print("yes" in lst)

True

列表的添加:

在列表末尾添加一个元素:append()

lst.append(100)

print(lst)

[10, 20, 'yes', 100]

lst2 =['helllo', 'good', 'nice']

lst.append(lst2)

print(lst)

[10, 20, 'yes', 100, ['helllo', 'good', 'nice']]

在末尾至少添加一个元素:expend

lst.extend(lst2)

print(lst)

[10, 20, 'yes', 100, 'helllo', 'good', 'nice']

在列表任意位置添加元素:insert()

lst2.insert(1,0)

print(lst2)

['helllo', 0, 'good', 'nice']

切片方法: 从第一个开始切出来,no he, 部分等于lst4

lst3 = ['hi', 'no', 'he']

lst4 = ['go', 'do', 30]

lst3[1::] = lst4

print(lst3)

['hi', 'go', 'do', 30]

列表的删除:

remove 从列表中移除一个元素,如果有重复元素只移第一个

lst = [10, 20, 30, 40, 50, 30]

lst.remove(30)

print(lst)

[10, 20, 40, 50, 30]

pop: 删除指定的元素

lst = [10, 20, 30, 40, 50, 30]

lst.pop(4)

print(lst)

[10, 20, 30, 40, 30]

lst.pop() 不指定参数则删除最后一个

print(lst)

[10, 20, 30, 40]

clrar :

ls = [1, 2, 3, 4, 5, 6]

ls.clear()

print(ls)

[]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值