python学习血泪史

一.变量和简单的数据类型

	message = "Hello Python world!" 
	print(message)
	message = "Hello Python Crash Course world!" 
	print(message) 

现在如果运行这个程序,将看到两行输出:

    Hello Python world! 
    Hello Python Crash Course world!

在程序中可随时修改变量的值,而Python将始终记录变量的最新值。

字符串

字符串就是一系列字符。
在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号。

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

变量名.title() 首字母大写
变量名.upper() 全部大写
变量名.lower() 全部小写

	name = "ada lovelace" 
	print(name.title())
	print(name.upper()) 
	print(name.lower())

输出:

Ada Lovelace
ADA LOVELACE 
ada lovelace

2.合并(拼接)字符串

使用 + 合并字符串

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

要在字符串中添加制表符,可使用字符组合 \t
要在字符串中添加换行符,可使用字符组合 \n

4.删除空白

剔除字符串末尾的空白,可使用方法 rstrip()
这种删除只是暂时的,要永久删除这个字符串中的空白,必须将删除操作的结果存回到变量中:

	message = "python "
	message = message.rstrip()

剔除字符串头的空白,可使用方法 lstrip()
剔除字符串两端的空白,可使用方法 strip()

5.Python2中的print语句

在Python 2中,无需将要打印的内容放在括号内。
些Python 2 print语句也包含括号,但其行为与Python 3中稍有不同。

数字

1.数字的计算

在Python中,可对数字执行加(+)减(-)乘(*)除(/)运算,使用两个乘号表示乘方运算

可以使用括号来修改运算次序

空格不影响Python计算表达式的方式

2.使用str()避免类型错误

使用变量打印字符串时,不能直接将数字类型的数字放过在一起,而要用一个函数 str() 来将数字类型的变量在打印时变成字符串。

3.Python 2 中的整数除法问题

在Python 2中,将两个整数相除得到的结果稍有不同

	python2.7
	3/2

输出 1

必须确保有一个操作数为浮点数

	print(3.0/2)
	print(3/2.0)
	print(3.0/2.0)

输出 1.5 1.5 1.5

4.注释

5.Python 之禅

Python程序员笃信代码可以编写得漂亮而优雅。

  1. 如果有两个解决方案,一个简单,一个复杂,但都行之有效,就选择简单的解决方案吧。
  2. 现实是复杂的,有时候可能没有简单的解决方案。
  3. 即便是复杂的代码,也要让它易于理解。

列表简介

列表是什么

在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素。

1.访问列表元素

列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。
索引从 0 而不是 1 开始

	massage = [ 'a' , 'b' , 'c' , 'd' ]
	print(massage[0])

输出 a

2.修改、添加和删除元素

(1).修改列表元素
	massage[0] = 'A'
(2).在列表中添加元素
1. 在列表末尾添加元素

1.将元素附加到列表末尾

	massage = [ 'a' , 'b' , 'c' , 'd' , 'e' ]

2.方法append()将元素添加到列表末尾

	massage.append('e')
2. 在列表中插入元素

使用方法insert(location, element)可在列表的任何位置添加新元素

	massage.insert(1, e)

massage = [ ‘a’ , ‘e’ , ‘b’ , ‘c’ , ‘d’ ]

(3).从列表中删除元素

1. 使用del语句删除元素

del massage[0]

massage = [ ‘a’ , ‘e’ , ‘b’ , ‘c’ , ‘d’ ]

2. 使用方法pop()删除元素
删除列表末尾的元素,并获取元素的值

3. 弹出列表中任何位置处的元素
使用方法pop(location),括号中写要删除元素的索引,
即删除该位置的元素并获取元素的值

4. 根据值删除元素
使用方法remove(element)

使用remove()从列表中删除元素时,也可接着使用它的值

	motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
	print(motorcycles) 
	too_expensive = 'ducati' 
	motorcycles.remove(too_expensive) 
	print(motorcycles) 
	print("\nA " + too_expensive.title() + " is too expensive for me.")

输出结果:

	['honda', 'yamaha', 'suzuki', 'ducati'] 
	['honda', 'yamaha', 'suzuki'] 
	A Ducati is too expensive for me.

方法remove()只删除第一个指定的值

3.组织列表

(1).使用方法 sort()对列表进行永久性排序

sort() 由小到大的排序(字母即是按字典顺序排序)

sort(reverse=True) 由大到小排序 (字母即使按相反字典顺序的排序)

(2).使用函数 sorted()对列表进行临时排序

sorted() 由小到大的排序(字母即是按字典顺序排序)
sorted(reverse=True) 由大到小排序 (字母即使按相反字典顺序的排序)

	print(sorted(列表名)
	print(sorted(列表名,reverse=True)
(3).使用方法reverse()倒置列表
	massage = [ 'a' , 'b' , 'c' , 'd' ]
	massage.reverse();
	print(massage)

输出:

['d', 'c', 'b', 'a']
(4).使用函数len()确定列表的长度
massage = [ 'a' , 'b' , 'c' , 'd' ]
a = len(massage)
print(a)

三.操作列表

1遍历整个列表

magicians = ['alice', 'david', 'carolina'] 
for magician in magicians: 		//让Python从列表magicians中取出一个名字,并将其存储在变量magician中
	print(magician)					//让Python打印前面存储到变量magician中的名字

输出:

alice 
david 
carolina

2. 避免缩进错误

3.创建数值列表

3.1 使用函数 range()

Python函数range()让你能够轻松地生成一系列的数字。

for value in range(1,5): 
	print(value)

上述代码好像应该打印数字1~5,但实际上它不会打印数字5:

1 
2 
3 
4

函数range()让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止,因此输出
不包含第二个值(这里为5)。

使用函数range()时,还可指定步长。例如,下面的代码打印1~10内的偶数:

even_numbers = list(range(2,11,2)) 
print(even_numbers)

输出:

[2, 4, 6, 8, 10]

在Python中,两个星号(**)表示乘方运算。下面的代码演示了
如何将前10个整数的平方加入到一个列表中:

squares = [] 
for value in range(1,11): 
square = value**2 
squares.append(square) 
print(squares)

输出:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3.2 对数字列表执行简单的统计计算

你可以轻松地找出数字列表的最大值、最小值和总和:

>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0 
>>> max(digits)
9 
>>> sum(digits)
45

3.3 列表解析

下面的示例使用列表解析创建你在前面看到的平方数列表:

squares = [value**2 for value in range(1,11)] 
print(squares)

当你觉得编写三四行代码来生成列表有点繁复时,就应考虑创建列表解析了。

3.4 使用列表的一部分

3.4.1 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli'] 
print(players[0:3])

指的是:

['charles', 'martina', 'michael']

如果你
要输出名单上的最后三名队员,可使用切片players[-3:]:

print(players[-3:])
3.4.2 遍历切片

如果要遍历列表的部分元素,可在for循环中使用切片。
在下面的示例中,我们遍历前三名队员并打印他们的名字:

players = ['charles', 'martina', 'michael', 'florence', 'eli'] 
print("Here are the first three players on my team:") 
for player in players[:3]: 
 	 print(player.title())

输出的只有前三个队员

Here are the first three players on my team: 
Charles 
Martina 
Michael
3.4.3 复制列表

例如,假设有一个列表,其中包含你最喜欢的四种食品,而你还想创建另一个列表,在其中
包含一位朋友喜欢的所有食品。不过,你喜欢的食品,这位朋友都喜欢,因此你可以通过复制来
创建这个列表:

my_foods = ['pizza', 'falafel', 'carrot cake'] 
friend_foods = my_foods[:] 		#必须这样赋值
print("My favorite foods are:") 
print(my_foods) 
print("\nMy friend's favorite foods are:") 
print(friend_foods)

输出的一样:

My favorite foods are: 
['pizza', 'falafel', 'carrot cake'] 
My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake']

四.元组

1.定义元组

元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来
访问其元素,就像访问列表元素一样。

例如,如果有一个大小不应改变的矩形,可将其长度和宽度存储在一个元组中,从而确保它
们是不能修改的:

dimensions = (200, 50) 
print(dimensions[0]) 
print(dimensions[1])

输出:

200 
50

2.遍历元组中的所有值

dimensions = (200, 50) 
for dimension in dimensions: 
 	 print(dimension)

就像遍历列表时一样,Python返回元组中所有的元素,:

200 
50

3.修改元组变量

dimensions = (200, 50)
print("Original dimensions:") 
for dimension in dimensions: 
  print(dimension) 
dimensions = (400, 100) 
print("\nModified dimensions:") 
for dimension in dimensions: 
  print(dimension)

输出:

Original dimensions: 
200 
50 
Modified dimensions: 
400 
100

五.if语句

5.1 条件测试如果条件测试的值为True,

Python就执行紧跟在if语句后面的代码;如果为False,Python就忽略这些代码。

5.1.1 使用and

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值