python基础(1)

时间:2022-07-22
本文章向大家介绍python基础(1),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

变量操作

1、使用方法修改字符串的大小写

1 2 3 4 5 6 7

>>> name = "devilf CheN" >>> name.title() 'Devilf Chen' >>> name.upper() 'DEVILF CHEN' >>> name.lower() 'devilf chen'

title()方法以首字母大写的方式显示每个单词。

upper()方法将字符串中的所有单词都大写

lower()方法是将字符串中出现的大写字母全部变成小写

2、合并拼接字符串

1 2 3 4 5 6 7

>>> first_name = "devilf" >>> last_name = "fei" >>> full_name = first_name + " " + last_name >>> print(full_name) devilf fei >>> print(full_name.title()) Devilf Fei

3、使用制表符或换行符添加空白

1 2 3 4 5 6 7 8 9 10 11 12 13

>>> print("thello world") hello world >>> print("hellonworldninlovenyou") hello world i love you >>> print("languages:ntPythonntJavantGo") languages: Python Java Go

4、删除空白

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

>>> name = "devilf " >>> name 'devilf ' >>> name.rstrip() #删除右侧空白(暂时删除,并不会真正删除) 'devilf' >>> name 'devilf ' >>> name_2 = " devilf " >>> name_2.rstrip() #左侧的空格并没有删除 ' devilf' >>> name_2.lstrip() #左侧的空格可以看出已经删除 'devilf ' >>> name_2 ' devilf ' >>> name_2.strip() #两边的空格均删除 'devilf' >>> name_2 ' devilf '

修改、添加、删除列表中的元素

1、修改列表元素

1 2 3 4 5 6 7

>>> names = ['a','b','c','d','e'] >>> >>> names[0] 'a' >>> names[0] = '1' >>> names ['1', 'b', 'c', 'd', 'e']

2、添加列表元素

1 2 3 4 5 6

>>> names [1, 'b', 'c', 'd', 'e'] >>> names.append('f') >>> names.append(2) >>> names [1, 'b', 'c', 'd', 'e', 'f', 2]

1 2 3 4 5

>>> name = [] >>> name.append('a') >>> name.append('b') >>> name ['a', 'b']

3、列表中插入元素

1 2 3 4 5 6 7 8

>>> names [1, 'b', 'c', 'd', 'e', 'f', 2] >>> names.insert(0,'this is first option') >>> names ['this is first option', 1, 'b', 'c', 'd', 'e', 'f', 2] >>> names.insert(-1,'this is last options') >>> names ['this is first option', 1, 'b', 'c', 'd', 'e', 'f', 'this is last options', 2]

4、删除元素

1 2 3 4 5

>>> names ['this is first option', 1, 'b', 'c', 'd', 'e', 'f', 'this is last options', 2] >>> del names[0] >>> names [1, 'b', 'c', 'd', 'e', 'f', 'this is last options', 2]

使用del删除元素时需要知道元素的索引位置。

下面看下使用pop()来删除元素

1 2 3 4 5 6 7 8

>>> names [1, 'b', 'c', 'd', 'e', 'f', 'this is last options', 2] >>> names.pop() 2 >>> names.pop() 'this is last options' >>> names [1, 'b', 'c', 'd', 'e', 'f']

pop()方法是默认将最后一个元素弹出,列表相当于一个栈,而删除列表末尾的元素相当于弹出栈顶元素。我们可以将弹出的元素赋给另一个变量来使用。

1 2 3 4 5

>>> names [1, 'b', 'c', 'd', 'e', 'f'] >>> name = names.pop() >>> name 'f'

实际上使用pop()可以弹出列表中的任何一个元素,只要我们知道该元素的索引即可

1 2 3 4 5 6

>>> names [1, 'b', 'c', 'd', 'e'] >>> names.pop(1) 'b' >>> names [1, 'c', 'd', 'e']

但是有时候我们又不知道从列表中什么位置删除元素,但我们知道删除什么,那么我们可以使用remove()方法

1 2 3 4 5 6 7 8

>>> names [1, 'c', 'd', 'e'] >>> names.remove(1) >>> names ['c', 'd', 'e'] >>> names.remove('c') >>> names ['d', 'e']

组织列表

1、对列表进行排序

1 2 3 4 5

>>> cars ['bmw', 'audi', 'toyota', 'subaru'] >>> cars.sort() >>> cars ['audi', 'bmw', 'subaru', 'toyota']

使用sort()方法进行排序时,可以看到列表进行了永久性的修改。如果我们想反向排序,可以加参数reverse=True

1 2 3 4 5

>>> cars ['audi', 'bmw', 'subaru', 'toyota'] >>> cars.sort(reverse=True) >>> cars ['toyota', 'subaru', 'bmw', 'audi']

如果我们想临时性的对列表进行排序,可以使用sorted()方法

1 2 3 4

>>> name ['james', 'kobe', 'wade', 'paul', 'anthony'] >>> sorted(name) ['anthony', 'james', 'kobe', 'paul', 'wade']

同样,如果我们想对sorted()进行反向排序时,也可以加参数

1 2 3 4

>>> name ['james', 'kobe', 'wade', 'paul', 'anthony'] >>> sorted(name,reverse=True) ['wade', 'paul', 'kobe', 'james', 'anthony']

当然还有一种最直接的方法来反向排序

1 2 3 4 5

>>> cars ['toyota', 'subaru', 'bmw', 'audi'] >>> cars.reverse() >>> cars ['audi', 'bmw', 'subaru', 'toyota']

2、获取列表长度

直接使用len()即可

1 2 3 4

>>> len(cars) 4 >>> len(name) 5