Python入门

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

title()以首字母大写的方式显示每个单词
upper()大写
lower()小写
存储数据时lower很有用

合并(拼接)字符串

first_name = 'ada'
last_name = 'love'
full_name = first_name+""+last_name

print(full_name)
print("Hello,"+full_name.title()+"!")

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

>>>print("Python")
Python
>>>print("\tPython")
	Python

>>>print("Languages:\nPython\nC\nJavaScript")
Languages:
	Python
	C
	JavaScript

删除空白

rstrip()
能够找到字符串开头和末尾多余的空白
lstrip()剔除字符串开头的空白
strip()提出字符串两端的空白

#永久删除这个字符串中的空白,必须将删除操作的结果存到变量中
>>>favorite_language = ’python '
>>>favorite_language = favorite_language.rstrip()
>>>favorite_language
'python'

python2中的整数

输入3/2,python的返回结果为1,在python2中,整数除法只包含整数部分,小数部分直接被删除,而不是四舍五入,为了避免这类操作,请务必保证至有一个操作数为浮点数,这样结果也是浮点数

列表

bycycle = ['1','2','3','4']
print(bycycle[0])

Python为访问最后一个列表元素提供了一种特殊的语法

bycycle = ['1','2','3','4']
print(bycycle[-1])

倒数第二为-2

修改列表元素

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

#修改列表元素
motorcycles[0] = 'ducati'
print(motorcycles)
#添加列表元素
motorcycles1 = []
motorcycles1.append('honda')
motorcycles1.append('yamaha')
motorcycles1.append('suzuki')

print(motorcycles1)
#在列表中插入元素
motorcycles = ['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
#从列表中删除元素
#使用del语句删除元素
del motorcycles[0]

使用del语句删除元素后不可访问,可指定删除哪一个元素
术语弹出(pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。
pop()可弹出列表中任何位置的元素,只需在括号中指定要删除的元素索引即可,并且让你可以继续使用它。每当你使用pop()时,被弹出元素就不再列表中了

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

根据值删除元素

不知道位置,只知道删除元素的值,可以用remove()

motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)

motorcycles.remove('ducati')
print(motorcycles)

使用remove()从列表删除元素后,也可以接着使用它的值,remove()只能删除第一个指定的值。如果要删除的可能出现多次,就需要使用循环来判断是否删除的了所有这样的值

motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)

too_expensive = 'ducati'
motorcycles.remove(too_expenvise)
print(motorcycles)

组织列表

使用sort()对列表进行永久性排列

#使下列列表按字母顺序排列
cars = ['bmw','audi','toyota''subaru']
cars.sort()
print(cars)
#还可以按与字母顺序相反来排列,只需向sort()传递参数reverse = True
cars = ['bmw','audi','toyota''subaru']
cars.sort(reverse = True)
print(cars)

使用sort()对列表进行临时排列

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:") 
print(sorted(cars))
print("\nHere is the original list again:") 
print(cars)

结果:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']

倒着打印列表

要反转列表元素的排列顺序,可以使用reverse()

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

注意,reverse()不是按字母顺序相反的顺序排列元素,而只是反转列表元素的排列顺序。reverse()永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,只需要再次对列表调用reverse()

确定列表的长度

注意 Python计算列表元素时从1开始,因此确定列表长度时,你应该不会遇到差一错误

cars = ['bmw','audi','toyota','subaru']
len(cars)

使用列表时避免索引错误

注意:发生索引错误却找不到解决办法时,请尝试将列表或其长度打印出来。列表可能与你以为的截然不同在程序对其进行了动态处理时尤其如此。通过查看列表或其包含的元素数,可帮助你找出这种逻辑错误。

遍历整个列表

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

注意缩进问题

使用函数range()

range()让你能够轻松生成一系列数字

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

使用range()创建数字列表,可以使用list()函数将range()的结果直接装换成一个列表,可使用再list()

numbers = list(range(1,6))
print(numbers)
``使用range()还可以指定步长

```python
#打印1-10的偶数
even_numbers = list(range(2,11,2))
print(even_numbers)

range可以创建任何需要的数集

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

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

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

列表解析

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

切片

要创造切片,可指定第一个元素和最后一个元素的索引,假如没有指定索引的话,python将自动从列表开头开始

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

要切片终止于结尾也可以使用这种语法,如果你要输出名单上最后三名队员,可使用切片players[-3:]

players = ['charles','martina','michael','florence','eli']
print(players[2:])

遍历切片

players = ['charles','martina','michael','florence','eli']
for player in players[:3]:
	print(player.title())

复制列表

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

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']

元组

列表非常适合存储在程序运行期间可能变化的数据集,列表时可以修改的,而不可变的列表被称为元组,定义元组我们使用的是圆括号而不是方括号

#修改元组变量
dimensions = (200,50)
for dimension in dimensions
	print(dimension)

dimensions = (400,100)
for dimension in dimensions
	print(dimension)

相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都不变,可以使用元组

if简单事例

cars = ['audi','bmw','subaru','toyota']

for car in cars:
	if car == 'bmw':
		print(car.upper())
	else
		print(car.title())

if是条件测试,只返回的值为True和False,如果返回Ture就执行if后面的代码,如果返回False,Python就会忽略这些代码。

检查多个多个条件

使用and检查多个条件,检查两个条件都为Ture。

>age_0 = 22
>age_1 = 18
>age_0 >= 21 and age_1 >=21
False

>age_1 = 22
>age_0 >= 21 and age_1 >= 21
Ture
>

使用or检查多个条件,只要满足一个条件,就可以通过测试

>age_0 = 22
>age_1 = 18
>age_0 >= 21 or age_1 >=21
True

>age_1 = 22
>age_0 >= 21 or age_1 >= 21
False

检查特定值是否包含在列表中,可使用关键字in
检查特定值是否不包含在列表中,可使用关键字not in

>requested——toppings = ['mushrooms','onions','pieapple']
>'mushroons' in requested_toppings
True
>'pepperoni' in requested_toppings
False 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值