让我们来深入学习一点Python

学习一下变量吧

字符定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
message="Hello Python World"
print(message)
#变量不需要声明,出现时即可定义
#python始终记录变量的最新值

#变量的命名:只包含字母、数字、下划线
#且不能以数字打头如 0_message
#变量不允许有空格
#变量应当用小写

#字符串:用引号括起来的都是字符串
"hello world"
'hello world'

name="ada lovelace"
print(name.title())#每个首字母大写
print(name.upper())#全部大写
print(name.lower())#全部小写
#title,upper,lower对name的修改是暂时的

字符串中使用变量

1
2
3
4
5
6
first_name="ada"
last_name="lovelace"
full_name=f"{first_name}{last_name}"#f字符串

#制表符\t
#换行符\n

空白操作

1
2
3
4
5
6

favorite_language=" python "
print(favorite_language.strip())#删除最左,右侧所有的空格
print(favoriet_language.Lstrip())#删除最左侧的所有空格
print(favorite_language.rstrip())#删除最右侧的所有空格
#注意strip,lstrip,rstrip的修改是暂时的

==在使用字符串时,应当注意单双引号的匹配;==

整数

1
2
3
4
5
6
7
8
2+3#加法
2-3#减法
2*3#乘法
2/3#除法
2**3#乘方
#运算顺序
2+3*4
(2+3)*4

浮点数

1
2
3
#浮点数不能用于当除数
#将任意两个整数相除,即使整除,结果也是浮点数
#如果是一个操作数为整数,一个为浮点数,那么结果也总是浮点数

数中的下划线

1
2
universe_age=14_000_000_000
print(universe_age)

同时给多个变量赋值

1
2
x,y,z=0,0,0
print(x,y,z)

常量

1
2
3
MAX_CONNECTION=500
#常量类似于变量,其在程序的整个生命周期内不变
#python程序员一般将常量以全大写的变量来指出其为常量

注释

==用#来对这一行后面的内容进行注释==
注释需要有意义才方便其他程序员理解

列表是什么东西呢

创建一个列表

1
2
3
4
5
6
bicycles=['trek','cannondale','redline','specilize']
print(bicycle)
#输出结果:['trek','cannondale','redline','specilize'](包含括号)
print(bicycle[0])
#输出结果:trek
#索引从零开始

==当你想要访问最后一个元素时,可以将索引指定为-1==
==因此你可以指定的索引范围为从-n到n-1==

修改列表元素

1
2
3
4
motobicycle=['honda','yamaha','suzuki']
print(motobicycle)#输出结果['honda','yamaha','suzuki']
moto[0]='ducati'
print(motobicycle)#输出结果['ducati','yamaha','suzuki']

添加

1
2
3
4
5
6
7
#append()方法
motobicycle=['honda','yamaha','suzuki']
print(motobicycle)#输出结果['honda','yamaha','suzuki']
motobicycle.append("ducati")
print(motobicycle)
#输出结果['honda','yamaha','suzuki','ducati']
#append()对列表的添加是永久的

插入

1
2
3
4
5
6
#insert()方法
motobicycle=['honda','yamaha','suzuki']
motobicycle.insert(0,'ducati')
print(motobicycle)
#输出结果:['ducati','honda','yamaha','suzuki']
#在索引为零处插入'ducati',索引>=0的列表元素后移

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#del语句
motobicycle=['honda','yamaha','suzuki']
del motobicycle[0]
print(motobicycle)#输出结果:['yamaha','suzuki']
#pop()方法
motobicycle=['honda','yamaha','suzuki']
popped_motobicycle=motobicycle.pop()
print(motobicycle)#输出结果:['honda','yamaha']
print(popped_motobicycle)#输出结果:suzuki
#当你使用了pop()后,被弹出的元素就不在列表中
//弹出任意位置的元素
first_motobicycle=motobicycle.pop(0)
//根据元素的值删除元素
#remove()方法
motobicycle=['honda','yamaha','suzuki']
motobicycle.remove('honda')
#remove只删除第一个指定的值,如果想要删除所有的,需要利用循环来确保该值彻底删除

组织列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#sort()方法对列表进行永久排序
cars=['bnw','audi','toyota','subaru']
cars.sort()#按字母顺序排列
print(cars)#输出结果:['audi','brw','subaru','toyota']
当向sort()方法传值reverse=True时;按相反的字母顺序排列
print(cars.sort(reverse=True))
#输出结果为['toyota','subaru','brw','audi']
#sorted()对列表进行临时排序
cars=['bnw','audi','toyota','subaru']
print(cars)#['bnw','audi','toyota','subaru']
print(cars.sorted())#['audi','brw','subaru','toyota']
print(cars)#['bnw','audi','toyota','subaru']
#倒着打印列表
#reverse()方法
cars=['bnw','audi','toyota','subaru']
print(cars.reverse())
#输出结果:['subaru','toyota','audi','bnw']
#确定列表长度:len()方法
cars=['bnw','audi','toyota','subaru']
print(cars.len())#输出结果:4

对列表进行一些华丽花哨的操作吧

遍历整个列表(循环)

1
2
3
4
magicians=['alice','david','carolina']
for magician in magicians:
print(magician)
#注意for循环后的冒号,缺少冒号会引起错误

缩进

==python中十分注重缩进问题,在进行for循环时,如果没有代码缩进,系统会因为找不到循环代码而报错,在不应该用缩进时使用缩进,系统也会报错:==
(1)不要忘记缩进
(2)不要忘记缩进额外的代码行
(3)不要有不必要的缩进
(4)注意循环后不必要的缩进

创建数值列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#使用range函数
for value in range(1,5):
print(value)
#注意range(1,5)的范围是1,2,3,4,不包含5(差一行行为)
#使用range来创建数字列表
#使用函数list()来将range()的结果直接转化成列表
numbers=list(range(1,6))#[1,2,3,4,5]
even_numbers=list(range(2,11,2))
print(even_numbers)#[2,4,6,8,10]
#使用range函数几乎能够创建任何需要的数集
square=[]
for value in range(1.11):
square.append(value**2)
print(square)
#对数字列表进行简单统计
min(square)
max(square)
sum(square)


列表解析:
square=[value**2 for value in range(1,11)]
print(square)

使用列表的一部分(切片)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
players=['charles','martina','michael','florence','eli']
print(players[0:3])#中间是冒号
#['charles','martina','michael'](差一行行为)
print(players[1:4])
#可以从任意合法位置开始,到另一个位置结束
print(players[:4])#自动从表头开始输出
print(players[2:])#从第二个元素开始的所有元素
print(player[-3:])#输出最后三个列表元素
#遍历切片
players=['charles','martina','michael','florence','eli']
print("here are the first three players in my team")
for player in players[:4]:
print(player.title())

#复制列表
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
print(my_foods)#['pizza','falafel','carrot cake']
print(friend_foods)#['pizza','falafel','carrot cake']
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)#['pizza','falafel','carrot cake','cannoli']
print(friend_foods)#['pizza','falafel','carrot cake','ice cream']
#当你不使用切片来复制列表时
friend_foods=my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)#['pizza','falafel','carrot cake','cannoli','ice cream']
print(friend_foods)#['pizza','falafel','carrot cake','cannoli','ice cream']
可以看到,不使用切片时,时将变量friend_foods与my_foods关联,而不是将my_foods的副本拷贝到friend_foods上

元组

1
2
3
4
5
6
7
8
9
10
11
python将不能修改的值称为'不可改变的',而不可变的列表称为'元组'
dimensions=(200,50)
print(dimensions[0])#200
print(dimensions[1])#50
但我们尝试修改元组元素时,会报错
如 :dimensions[0]=250
#遍历元组中的所有值与遍历列表是相同的
但元组变量是可以改变的
需要重新定义整个列表
dimensions=[200,50]
dimensions=[400,50]

if语句好像有点帅气

if语句

1
2
3
4
5
6
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw'
print(car.upper())
else:
print(car.title())

条件测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#检查是否相等
car = 'bmw'
car == 'bmw'
#检查时不可忽略大小写
car='AUdi'
car=='audi'
#检查是否不相等使用!=
#数值比较,大于,小于,大于等于,小于等于
#检查多个条件:and:同时满足、or:满足其中一个即可
#检查是否存在于列表中
requested_toppings=['mushroom','onion','david']
'mushroom' in requested_toppings
#检查特定值不在列表中
#布尔表达式

f语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#单纯的if语句
if age >= 18:
print('you are old enough to vote')
#if-else结构
if age >= 18:
print('you are old enough to vote')
else:
print('sorry,you are too young to vote')
#if-elif-else结构
if age < 4:
print('Your admission cost is $0')
elif:
print('Your admission cost is $25')
else:
print('Your admission cost is $40')
#可使用多个elif代码块
if-elif-else 结构中
else语句可以用用更详细的elif语句代替
如果想要只执行一个代码块,用 if-elif-else 结构
如果想执行多个,使用多个独立的if语句

使用if语句处理列表

1
2
3
#检查特殊元素
#确定列表是不是空的
#使用多个列表

字典(难道我要背单词)

一个简单的字典

1
2
3
alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])

while循环(我要怎么跳出来呢)

函数绝对会很帅的吧

类是个什么东东

文件操作有点深奥