Python基础

这篇博客详细介绍了Python的基础知识,包括列表、元组、字符串、字典、集合和进程的操作。对于列表,讲解了添加、计数、下标、插入、删除等方法;元组是不可修改的序列;字符串方面,提到了find、join、split、strip等方法;字典的常用操作如清除、复制、get、update等;还讨论了进程的概念和进程间的通信方式,以及僵尸进程和线程。此外,文章还涉及了装饰器、生成器、面向对象的封装、继承、多态等高级主题。
摘要由CSDN通过智能技术生成
1.列表

​ 1.1 添加

append列表末尾添加新对象
a = [1,2,3]
a.append(4)    #[1,2,3,4]
1.2   extend在列表尾部添加一个序列中多个值
a = [1,2,3]
b = [4,5,6]
a.extend(b)    # [1,2,3,4,5,6]

​ 1.2计数

count统计某个元素的出现次数
a = [1,1,2,3,3,2]
a.count(1)     #[2]

​ 1.3 下标

index从列表中找出某个值得下标
a = [1,2,3,4]
a.index(4)     #  [3]

​ 1.5插入

insert将对象插入列表中
a = [1,2,3]
a.insert(0,'aa')  #['aa',1,2,3]

1.6删除

pop删除类表中的一个元素(默认删除最后一个),并且返回该元素的值
a = [1,2,3]
a.pop()   #[1,2]

1.7移除

remove移除列表中一个值得第一个匹配
a = [1,1,2]
a.remove(1)   #  [1,2]

1.8反向

reverse将列表中的元素方向排序
a = [1,2,3]
a.reverse()   #[3,2,1]

1.9排序

sort在原位置的列表进行排序,该遍原来的列表,让其中的元素排序
a = [1,2,aa,bb,3]
a.sort()      #[1,2,3,aa,bb]

1.10范围和xrange

  • 指定范围生成指定数字

  • python3中的范围类似python2中的xrange,类似a = range(1,4):a返回的不是一个列表对象另一个可以重复对象

    #1、range根据start与stop指定的范围以及step设定的步长,生成一个序列:range([start,] stop[, step])
    #2、xrange 用法与 range 完全相同,所不同的是生成的不是一个list对象,而是一个生成器
    for i in range(1,10,2):
        print(i)
    

1.11列表去空

#方法一
filter(None, your_list)

# 法2:
while '' in your_list:
    your_list.remove('')

# 法3:
your_list = [x for x in your_list if x != '']
2.元组
  • 元组定义:元组和列表一样,也是一种序列,唯一的不同就是元组不能修改。

    2.1创建元组

    #1. 创建元组
    a = (1,2,3,4)
    #2. 将列表转换成元组
    tuple([1,2,3,4])     #(1,2,3,4)
    

2.2列表和元组常用函数

方法 作用
com(x,y) 比较两个值
len(seq) 返回序列的长度
清单(seq) 把序列换成列表
最大(参数) 返回序列中的最大值
最小值(参数) 返回序列中的最小值
反转(seq) 对序列进行反转
排序(seq) 返回已经排序的元素列表
元组(seq) 把序列转换成元组
3.字符串

1.字符串常用方法

1.1 find查找方法

  • 作用:find可以在字符串中查找某一个元素,返回下标

    a = 'abcdefghijk'
    print(a.find('abc'))                         #the result : 0
    print(a.find('abc',10,100))                    #the result : 11  指定查找的起始和结束查找位置
    

1.2 join方法

  • 作用:插入元素,元素必须是字符串
a = ['1','2','3']
print('+'.join(a))                                    #the result : 1+2+3

1.3 split 方法

  • 作用:可以将字符串分割成队列
print('1+2+3+4'.split('+'))                            #the result : ['1', '2', '3', '4']

1.4 strip删除首位空格(不包含内部)空格

print("   test   test    ".strip())                #the result :“test   test”

1.5 替换

  • 作用:replace 返回某个字符串所有匹配项,返回替换之后的字符串
print("This is a test".replace('is','is_test'))     #the result : This_test is_test a test

1.6首字母大写

  • 作用:capitalize 将字符串的首字母转换成大写
s = 'aBdkndfkFFD'
s.capitalize()
'Abdkndfkffd'

1.7 字符串格式化

1.7.1 使用百分号(%)字符串格式化

num = 100
print("%d to hex is %x" %(num, num))        #100 to hex is 64
print("%d to hex is %#x" %(num, num))       #100 to hex is 0x64

1.7.2使用format字符串格式化

#1. 位置参数
print("{0} is {1} years old".format("tom", 28))            #tom is 28 years old
print("{} is {} years old".format("tom", 28))             #tom is 28 years old
print("Hi, {0}! {0} is {1} years old".format("tom", 28))      #Hi, tom! tom is 28 years old

#2. 关键字参数
print("{name} is {age} years old".format(name = "tom", age = 28))    #tom is 28 years old

#3. 下标参数
li = ["tom", 28]
print("{0[0]} is {0[1]} years old".format(li))          #tom is 28 years old
4.字典

4.1字典常用方法

1.1清除

  • 作用:clear 清楚字典中的所有项,是一个原地操作,无返回值(或者返回None)
d = {}
d['amg&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值