自学python整理

Python 自学笔记

前言:Python之禅 import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

基础篇

变量

1
2
3
4
5
6
1.变量不能以数字开头命名,test_1 可以,1_test不可以
2.变量不能包含空格,分隔命名单词最好用下划线,test_first
3.变量不能使用关键字和函数名
4.最好使用小写字母命名变量
5.同时给多个变量赋值 x,y,z = 0,0,0
6.常量用大写

字符串

1
2
3
4
5
6
7
1.字符串首字母大写方法:xxx.title()
2.字符串全部大写方法:xxx.upper()
3.字符串全部小写方法:xxx.lower()
4.在字符串中使用变量“f字符串”:test = f"{one}{two}"
5.字符串末尾删除空白方法:xxx.rstrip()
6.字符串开头删除空白方法:xxx.lstrip()
7.字符串两端删除空白方法:xxx.strip()

数学计算

1
2
1.+ - * / , ** 表示^ 乘方
2.浮点数直接计算,相除结果都是浮点数

列表

1
2
3
4
5
students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
--------
结果:
['zhangsan','lisi','wangwu','zhaoliu']

访问列表元素

1.提取students中第一个元素:

1
print(students[0]),结果: zhangsan

配合xxx.title() 有奇效:

1
print(students[0].title()),结果:Zhangsan

2.列表索引从0开始,最后一个代码位[-1],

1
print(students[-1]) 结果:zhaoliu

3.增删改——改:修改第一个学生名为huangjie

1
2
students[0] = 'huangjie'     //直接将字符串赋值到所在位置
print(students[0]) 结果:huangjie

4.增删改——增:添加一个学生huangjie

1
2
3
4
5
students.append('huangjie')
print(students)
--------
结果:
['zhangsan','lisi','wangwu','zhaoliu','huangjie']

关于append(): 可以先创建空列表,然后使用append()添加字符串

1
2
3
4
5
6
7
8
teachers = []
teachers.appedn('Mis.Wang')
teachers.appedn('Mr.Wu')
teachers.appedn('Mr.Hu')
print(teachers)
-----------
结果:
['Mis.Wang','Mr.Wu','Mr.Hu']

5.增删改——插入:insert()

1
2
3
4
5
6
students = ['zhangsan','lisi','wangwu','zhaoliu']
students.insert(0,'qianyi')
print(students)
---------
结果:
['qianyi','zhangsan','lisi','wangwu','zhaoliu']

6.增删改——删:

del() 永久删除

1
2
3
4
5
6
7
students = ['qianyi','zhangsan','lisi','wangwu','zhaoliu']

del student[0]
print(students)
---------
结果:
['zhangsan','lisi','wangwu','zhaoliu']

pop()出栈后赋值变量可保留:

1
2
3
4
5
6
7
8
students = ['qianyi','zhangsan','lisi','wangwu','zhaoliu']
lastone = students.pop()
print(students)
print(lastone)
---------
结果:
['qianyi','zhangsan','lisi','wangwu']
zhaoliu

remove()直接删除关键字:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
students = ['qianyi','zhangsan','lisi','wangwu','zhaoliu']
students.remove('qianyi')
print(students)
---------
结果:
['zhangsan','lisi','wangwu','zhaoliu']
或者:
leave_student = 'qianyi'
students.remove(leave_student)
print(students)
print(f"{leave_student.title()} is leaving.")
---------
结果:
[zhangsan','lisi','wangwu','zhaoliu']
Qianyi is leaving

组织排序

1
2
3
4
5
1.sort()方法 按字母顺序排序,永久性。
2.sort(reverse=True),添加参数,按字母顺序反向排序。
3.sorted(),临时排序
4.反向打印列表内容:xxx.reverse()
5.len()确定列表的长度

操作列表

1.遍历整个列表:python的for循环

1
2
3
students = ['qianyi','zhangsan','lisi','wangwu','zhaoliu']
for student in students:
print(student)

例子:99乘法表,两个for循环,第二个循环变量引用第一个循环的变量,第二个循环变量自增长 x 第一个循环变量 加以空格,达到三角形结构乘法表

1
2
3
4
5
6
# 99乘法表
for one in range(1,10):
for two in range(one):
two = two + 1
print(f"{two} x {one} =", one * two, end=" ")
print("")

2.range(1,5)函数,第一个参数 1为开始值,第二个参数5为结束值,从0开始,0,1,2,3,4,共计5个数,显示从1,5 因此结果是1,2,3,4

1
2
3
4
5
6
7
8
for number in range(1,5):
print(number)
--------
结果:
1
2
3
4

单个参数range(6) 结果:0,1,2,3,4,5

三个参数,第三个参数为步长间隔range(2,11,2) 打印结果2,4,6,8,10 翻译为十以内的偶数

统计计算

1
2
3
4
5
6
7
8
9
arrys = [1,2,3,4,5]
min(arrays)
max(arrays)
sum(arrays)
-----
结果:
1
5
15

列表解析

array = [表达式 for循环],例:打印3~30中 3能被三整数的数 (实际就是3的倍数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
array = [n*3 for n in range(1, 11)]
for m in array:
print(f"{m}")
-----------
结果:
3
6
9
12
18
21
24
27
30

切片

格式:xxx[x:x] ,【第一个数:截止的数】

1
2
3
4
5
6
7
8
9
10
11
12
13
students = ['0','1','2','3','4']
print(students[0:2]) //打印列表中02的值
print(students[1:4]) //打印列表中14的值
print(students[:3]) //打印列表中03的值
print(students[2:]) //打印列表中2到末尾的值
print(students[-3:]) //打印列表中倒数三个数
-----------
结果:
['0','1','2']
['1','2','3','4']
['0','1','2','3']
['2','3','4']
['2','3','4']

切片+for循环=遍历切片

1
2
3
4
5
6
7
8
students = ['0','1','2','3','4']
for i in students[:3]:
print(i)
--------
结果:
0
1
2

复制列表 [:],用切片复制列表后,再在两张表随意添加,可获得各自的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
students = ['0','1','2','3','4']
children = students[:]

students.append('5')
children.append('-1')

print("students :")
print(students)

print("children :")
print(children)
--------
结果:
students :
['0','1','2','3','4','5']
children :
['0','1','2','3','4','-1']

元组

1
2
3
1.元组------不可变的列表,比如 当前坐标点xy = (1000, 500)
2.创建一个元素的元组,结束必须加逗号,其实没意义... xy=(1000,)
3.组不可修改值,但可以修改变量,比如坐标变动xy = (1000,200)

if语句

条件测试

1
2
3
4
5
6
7
8
9
10
11
a == b
a > b
a != b
a = b and a > c
a > b or a = c

array = ['1','2','3','4']
n = 9
'1' in array
if n not in array:
print('not in')

布尔表达式:True False

if语句:

1
2
3
4
5
if xxx:
xxxx
elif xxx:
xxxx
else

扩展:判定列表非空

1
2
3
4
5
6
list = []
if list:
for i in list:
print(i)
else
print("null!")

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!