0
点赞
收藏
分享

微信扫一扫

python从入门到实践 读书笔记摘要 (1-3)

青青子衿谈育儿 2022-02-07 阅读 53
python

全是精华,没有废话,让你最快学会python!!!

1.

print('hello world!')
print("Hello Python interpreter!")

2.

2.1

print("Hello Python world!")

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

2.3 字符串

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

c = "This is a string."
print(c)

2.3.1

name = "ada lovelace"
print(name.title())
# title()以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写
# 要将字符串改为全部大写 .upper()
# 或全部小写 .lower()

name = "Ada Lovelace"
print(name.upper())
print(name.lower())

 2.3.2 合并(拼接)字符串


 拼接:

first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")
message = "Hello, " + full_name.title() + "!"
print(message)

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

# 在字符串中添加制表符,可使用字符组合\t

print('\tpython')
# 在字符串中添加换行符,可使用字符组合\n

print('Language:\nC\nJava\nPython')

# 可以组合使用 \t\n

2.3.4 删除空白
​​​​​​
# 暂时删除  .rstrip()
 

favorite_language = "python "
print(favorite_language.rstrip())

# 确保字符串末尾没有空白,可使用方法.rstrip()
# 永久删除操作,将删除后的结果再次储存到原变量里
# 你还可以剔除字符串开头的空白(lstrip()),或同时剔除字符串两端的空白(strip())。为此,可分别使用方法
# lstrip()和strip():

2.3.5 使用字符串时避免语法错误
 

message = "One of Python's strengths is its diverse community."
print(message)
#  对
# 错

# message = 'One of Python's strengths is its diverse community.'
# print(message)

2.4 数字

2.4.1 整数


# 在Python中,可对整数执行加(+)减(-)乘(*)除(/)运算。

# Python使用两个乘号表示乘方运算

2.4.2 浮点数


# 结果包含的小数位数可能是不确定的
# 0.2+0.1
# 0.30000000000000004
# 2.4.3 使用函数str()避免类型错误
age = 23
message = "Happy " + str(age) + "rd Birthday!" #将23读取为一个整体 用str()
print(message)

2.5 注释


# 向大家问好
 

print("Hello Python people!")

3.列表简介

3.1 列表是什么


# 列表由一系列按特定顺序排列的元素组成 ,用方括号([])来表示列表 ,并用逗号来分隔其中的元素
 

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

3.1.1 访问列表元素

 


# 列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。
# 要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。
 

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])

# 当你请求获取列表元素时,Python只返回该元素,而不包括方括号和引号:
 

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())

# 可以和方法联合使用



3.1.2 索引从0 而不是1 开始

 


# Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1,可让Python返
# 回最后一个列表元素
 

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

3.1.3 使用列表中的各个值

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)

3.2 修改、添加和删除元素

​​​​​​


3.2.1 修改列表元素
 

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati' # 列表名加修改的位置
print(motorcycles)

# 你可以修改任何列表元素的值,而不仅仅是第一个元素的值。

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

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
# 方法append()将元素'ducati'添加到了列表末尾,而不影响列表中的其他所有元素:

# 2. 在列表中插入元素
# 使用方法insert()可在列表的任何位置添加新元素。

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

3.2.3 从列表中删除元素

# (1)使用del语句删除元素
# 如果知道要删除的元素在列表中的位置,可使用del语句。
# 使用del可删除任何位置处的列表元素,条件是知道其索引。
 

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
# (2) 使用方法pop()删除元素
# 有时候,你要将元素从列表中删除,并接着使用它的值
# 方法pop()可删除列表末尾的元素,并让你能够接着使用它

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
# (3)弹出列表中任何位置处的元素你可以使用pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素的索引即可

motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
# 每当你使用pop()时,被弹出的元素就不再在列表中了
# 如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续
# 使用它,就使用方法pop()。

# (4)根据值删除元素
# 如果你只知道要删除的元素的值,可使用方法remove()
# 使用remove()从列表中删除元素时,也可接着使用它的值
# 方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要
# 循环使用来判断是否删除了所有这样的值

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

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.")

# 值'ducati'已经从列表中删除,但它还存储在变量too_expensive中
​​​


3.3 组织列表


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

注意区别  方法和函数 的使用
# 方法sort()永久性地修改了列表元素的排列顺序。现在,汽车是按字母顺序排列的
# 再也无法恢复到原来的排列顺序
 

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
# ['audi', 'bmw', 'subaru', 'toyota']
# 你还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort()方法传递参数
# reverse=True。下面的示例将汽车列表按与字母顺序相反的顺序排列:

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

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




# 函数sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
 

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

# 如果你要按与字母顺 序相反的顺序显示列表,也可向函数sorted()传递参数reverse=True。


3.3.3 倒着打印列表
 


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

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

3.3.4 确定列表的长度



# 使用函数len()可快速获悉列表的长度

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

相关推荐

0 条评论