美文网首页
Python 编程从入门到实践1

Python 编程从入门到实践1

作者: 蜘蛛的梦呓 | 来源:发表于2018-03-29 00:36 被阅读0次

今天开始读 Python 编程从入门到实践,我会记录下读这本书的随笔笔记,计划10天完成。

2.3 字符串

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

Python upper() 方法将字符串中的小写字母转为大写字母。

#upper()方法语法:
str.upper()

Python lower() 方法转换字符串中所有大写字符为小写

#lower() 方法语法:
str.lower()

2.3.2 合并字符串:

合并字符串:

Python 使用加号(+)来合并字符串 str1 +str2,这种合并字符串的方式称为字符串的拼接

str1 = 'Hello'
str2 = 'World'
print(str1 + str2)

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

\t : 横向制表符(占4位字符)

\n : 换行符

print('Languages:\n\tPython\n\tC\n\tJava')

在同一个字符串中可以同时包含制表符和换行符

2.3.4 删除空白

# rstrip() 删除字符串末尾空白
# lstrip() 删除字符串开头空白
# strip() 方法用于移除字符串头尾指定的字符(默认为空格)
str = "     0000000Runoob0000000  "
str1 = str.lstrip()
print(str1)
str2 = str1.rstrip()
print(str2)
str3 = str2.strip('0')
print(str3)

3.1 列表是什么

列表定义:在 python 中用 [] 来表示列表,并用 “,” 分割列表中的元素

3.1.1 访问列表元素

列表是有序集合,根据索引访问元素(索引从 0 开始),索引为 -1 时返回列表最后一位元素,以此类推。

PS:这种语法很有用,适用于在不知道列表长度的情况下,访问列表的最后元素

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
>>> specialized

3.2 修改列表元素

3.2.1 在列表中添加元素

1.在列表末尾添加元素 list.append()

2.在列表中插入元素 list.inset(index,element)

3.2.2 删除元素

#1.已知索引清空下,使用 del 删除元素(删除后,将无法再访问此元素)
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
del bicycles[0]
print(bicycles)
>>> ['cannondale', 'redline', 'specialized']

# 2.使用 pop(index) 删除元素(默认删除列表末尾元素)
bicycles2 = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles2.pop())
print(bicycles2)
print(bicycles2.pop(0))
print(bicycles2)

>>> specialized
>>> ['trek', 'cannondale', 'redline']
>>> trek
>>> ['cannondale', 'redline']

# 3.根据值删除元素 list.remove(element)
bicycles2 = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles2)
bicycles2.remove('redline')
print(bicycles2)

>>> ['trek', 'cannondale', 'redline', 'specialized']
>>> ['trek', 'cannondale', 'specialized']

判断:如果从列表中删除一个元素,且不再以任何方式使用它,就使用 del 语句;如果你要在删除元素后还要继续使用它,就还使用方法 pop()。

3.3 组织列表

3.3.1 使用 sort() 对列表永久性地排序

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

>>> ['audi', 'bmw', 'subaru', 'toyota']

# 传递参数 reverse=True 按与字母顺序相反的顺序排序
cars.sort(reverse=True)
print(cars)

>>>['toyota', 'subaru', 'bmw', 'audi']

3.3.2 使用函数 sorted() 对列表进行临时排序,列表顺序并没有改变

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

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

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

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

3.3.3 倒着打印列表

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

>>> ['bmw', 'audi', 'toyota', 'subaru']
>>> ['subaru', 'toyota', 'audi', 'bmw']

4.1 遍历整个列表

4.3.1 使用 range() 范围:[)

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

4.3.2 使用 range() 创建数字列表

numbers = list(range(1, 6))
print(numbers)

>>> [1, 2, 3, 4, 5]

# 指定步长
# 打印 [2,11) 的偶数
print(list(range(2, 11, 2)))

>>> [2, 4, 6, 8, 10]

4.3.3 对数字列表进行统计计算

digits = list(range(1,10))
print(min(digits))
print(max(digits))
print(sum(digits))

4.3.4 列表解析

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

>>> [4, 16, 36, 64, 100]

使用创建列表解析可以使你的代码更加简洁

4.4.1 切片

定义

切片:处理列表的部分元素

4.4.2 遍历切片

cars = ['bmw', 'audi', 'toyota', 'subaru']
for car in cars[:2]:
    print(car)
>>>  bmw
>>> audi

4.4.3 复制列表

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

print('My favorite foods are:')
print(my_foods)
print('\nMy friend favorite foods are:')
friend_foods.append('ice cream')
print(friend_foods)

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

>>>  My friend favorite foods are:
>>>  ['pizza', 'falafel', 'carrot cake', 'ice cream']

4.5 元组

定义:不可变的列表。格式: (,)(PS:自己可以尝试修改元组元素)

4.5.2 遍历元组的所有值

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

4.5.3 修改元组变量

# 虽然不能修改元组的元素,但可以给存储元组的变量赋值。
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)
>>> Modified dimensions:
>>> 400
>>> 100

今天看了 4 章的内容,到此为止

相关文章

网友评论

      本文标题:Python 编程从入门到实践1

      本文链接:https://www.haomeiwen.com/subject/mpducftx.html