python从入门到实践笔记chapter1-5

1 起步
cd 切换目录
ls 显示当前目录(不包括隐藏文件)
python的主页http://python.org/
2.1 .py告诉编辑器使用python解释器来运行
2.2 变量命名规则:字母、数字和下划线;只能字母,下划线开头;慎用l和o容易误会;不能用python关键字和函数名
python关键字和内置函数
2.3 字符串用单引号或者双引号
修改大小写 首字母大写title() ,upper(), lower()
合并字符串 用+
制表符 \t 换行符\n
删除空白 rstrip() lstrip() strip()注意这种删除只是暂时的
2.4 数字 乘方**
str()将数字转换为字符串,直接print很容易出现语法错误
2.5 用#来注释

3.1 列表按特定顺序排列的元素组成,用[ , ,]来表示
访问list[0]第一个,list[-1]最后一个
3.2 修改添加和删除元素

motorcycles=['honda','yamaha','suzuki']
motorcycles[0]='ducati'#修改第一个元素
motorcycles.append('ducati') #末尾添加,重复的也能添加上
motorcycles.insert(0,'ducati') #在指定索引处添加
del motorcycles[0] #删除元素,一定要指定元素
popped_motorcycles=motorcycles.pop(-1) #删除元素,不指定索引删除最后一个元素
motorcycles.remove('ducati') #根据值删除元素,不能删除重复值,一次只删除一个

3.3 对列表排序
lists.sort(reverse=True) 对列表按字母表顺序相反排列,注意是永久改变
sorted(lists) 暂时改变顺序
lists.reverse() 反转列表元素
len(lists) 确定列表长度

4.1
遍历列表

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

for循环不要遗漏冒号
4.3 range(a,b,c)产生包括a,不包括b,距离为c的一系列数字
list()转换为列表
list(range())产生数字列表

squares=[]  #注意列表要提前初始化,否则会出错
for value in range(1,10):
    square=value**2
    squares.append(square)  
squares=[]
for value in range(1,10):
	squares.append(value**2)
squares=[value**2 for value in range(1,10)]

上面三种方法,第三个列表解析比较简洁,应尽量使用
4.4 切片
lists[0:3],lists[:3],lists[3:],lists[-3:]
lists[开始:结束:步长]#步长可以为负数,反着数
exp: lists[1:5:-1]会出错,1比5小
遍历切片

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

复制列表

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:] #不能写成friend_foods=mu_foods,这样是关联两个列表
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

4.5元组
列表是可以修改的,而元组是不可以修改的(注意这种不可修改是相对的),Python将不能修改的值称为不可变的,而不可变的列表被称为元组,用圆括号定义元组
元组元素值不能修改

dimensions=(200,50)
dimensions[0]=222#给出一个TypeError
Traceback (most recent call last):
	File "dimensions.py", line 3, in <module>
		dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment

遍历元组

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

元组变量可以修改(整体修改)

dimensions=(200,50)
dimensions=(400,100,12)#这样是可以的

4.6 代码格式
指南:PEP8 (https://python.org/dev/peps/pep-0008/)
缩进固定空格,不要与制表符混用,行长不超过80字符,可以设置一条竖线,使用空行

5 if语句
5.2条件测试
可以用==判断相等, !=判断不等 < ,> ,<= ,>=,and ,or
in 来判断是否在列表中

>>>requested_toppings=['mushrooms', 'onions', 'pineapple']
>>>'mushrooms' in requested_toppings
True
>>>'mushrooms' not in requested_toppings
False

5.3

if condition:
	pass
if condition:
	do one
else:
	do other
if conditon_one:
	do one
elif conditon_two:
	do two
else:
	do other

可以使用任意数量的elif,程序顺序检测是否符合,注意条件顺序
else可以省略,如果知道最终要测试的条件,应考虑使用一个elif代码块来代替else代码块
5.4 检查列表是否为空,在if语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True,并在列表为空时返回False
一个循环加if列子,外层循环取元素,用if判断

available_toppings = ['mushrooms', 'olives', 'green peppers','pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
	if requested_topping in available_toppings:
		print("Adding " + requested_topping + ".")
	else:
		print("Sorry, we don't have " + requested_topping + ".")
print("\nFinished making your pizza!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值