(翻译)30天学习Python👨‍💻第三天——数据类型2

965 阅读5分钟

30天学习Python👨‍💻第三天——数据类型2

在昨天学习的字符串数据类型的基础上,今天我探讨了其他一些特性。

格式化字符串

字符串格式化是一种非常简洁的特性,它能让我们动态更新字符串中的内容。假设我们有从服务器获取的用户信息,并希望根据该信息显示自定义消息,第一个想法是应用字符串连接之类的东西。

first_name = 'Tom'
last_name = 'Cruise'
welcome_message = "Welcome" + " " + first_name + " " + last_name
print(welcome_message) # Welcome Tom Cruise

如果我们有更多的变量,那么动态字符串可能会有点难以阅读。如果我们有其他类型的数据,我们也需要将它们转为字符串。最简洁的方式是使用格式化字符串。

first_name = 'Tom'
last_name = 'Cruise'
welcome_message = f'Welcome {first_name} {last_name}'
print(welcome_message) # Welcome Tom Cruise

字符串前的f表示格式化的字符串,动态值放置在{}

这是一种非常简洁的语法,等价于JavaScript在ES6中引入的字符串插值表达式或模板字符串,它看起来是这个样子:

firstName = 'Tom';
lastName = 'Cruise';
welcomeMessage = `Welcome ${firstName} ${lastName}`;
console.log(welcomeMessage) // Welcome Tom Cruise

字符串索引

Python中的字符串是简单有序的字符集合,所以我们处理它有很多的技巧。我们可以访问字符串的字符,选择子字符串,反转字符串等等,非常简单,这也被称为切片。

language = 'python'
first_character = language[0] # 索引是从0开始
second_character = language[1]
print(first_character) # p
print(second_character) # y
# 字符串可以用这个简单的方式操作[start:stop:step-over]
range_1 = language[0:2] # 从0开始到1
range_2 = language[0::1] # 从0开始,以步长为1进行迭代,直到最后
range_3 = language[::2] # 从0开始,以步长为2进行迭代,直到最后
range_4 = language[1:] # 从1开始到最后
range_5 = language[-1] # 最后一个字符
range_6 = language[-2] # 倒数第二个字符
reverse_string = language[::-1] # 从尾部开始反转字符串
reverse_string_2 = language[::-2] # 反转字符串并忽略第一个字符

print(range_1) # py
print(range_2) # python
print(range_3) # pto
print(range_4) # ython
print(range_5) # n
print(range_6) # o
print(reverse_string) # nohtyp
print(reverse_string_2) # nhy

www.digitalocean.com/community/t…

不可变性

字符串本质上是不可变的,这意味着字符串的值不可改变。

favorite_website = 'dev.to'
favorite_website[0] = 'w'
print(favorite_website) # TypeError: 'str' object does not support item assignment

内置的字符串函数和方法

Python中有一些内置的函数和方法用来操作字符串类型的数据。函数通常是一个可以被独立调用的行为,比如print() round(),而方法则是一个简单的函数,是对象的一部分,用.操作符调用。

quote = 'javascript is awesome'
print(len(quote)) # 21 (计算字符串的总长度)
new_quote = quote.replace('javascript', 'python')
print(new_quote) # python is awesome
capitalize = new_quote.capitalize()
print(capitalize) # Python is awesome
upper_case = new_quote.upper()
print(upper_case) # PYTHON IS AWESOME

print(quote) # javascript is awesome (Note: Strings are immutable!) 

www.w3schools.com/python/pyth…

www.w3schools.com/python/pyth…

布尔值

布尔值在python中表示为bool,值为TrueFalse

is_cool = True
is_dirty = False
print(10 > 9) # True 

注释

注释是用代码编写的语句,以增强其可读性。在Python中,它们是用#符号写在语句后面的。注释会被解释器忽略,只是为了代码的可读性。我已经在代码示例中使用它们来打印输出或添加一些注释。根据良好的编程实践,我们应该尽量让我们的代码具有可读性,就像阅读英语一样,只在需要的时候添加注释,因为过多的注释会适得其反。

# This is not a very useful comment but written just for the sake of demonstration

集合

集合是一种非常重要的数据类型,它们是一组对象的集合。它也是一种数据结构,这意味着它一种容器,可以把不同用途的数据以某种特定的格式组织起来。就像JavaScript中的数组一样,用[]表示,可以用来存储相同或不同的数据类型。

favorite_languages = ['javascript', 'python', 'typescript']
shopping_cart_items = ['pen','toothbrush', 'sanitizer','eraser']
random_things = ['football', 123, True, 'developer', 777]

first_item = shopping_cart_items[0]
print(first_item) # 'pen'

集合切片

与字符串类似,列表也可以切片。然而,与字符串不同的是,列表是可变的,这意味着它们的数据可以被改变。

soccer_stars = ['ronaldo', 'messi','ibrahimovic','zidane','beckham']
soccer_stars[0] = 'suarez'
print(soccer_stars) # ['suarez', 'messi','ibrahimovic','zidane','beckham']
range = soccer_stars[0:3]
print(range) # ['suarez', 'messi', 'ibrahimovic']
print(soccer_stars) # ['suarez', 'messi','ibrahimovic','zidane','beckham']
# Note : Slicing lists does not mutate them

clone = soccer_stars[:] # copies the list. Commonly used in Python
print(clone) # ['suarez', 'messi','ibrahimovic','zidane','beckham']
reverse_order = soccer_stars[::-1] # reverses the order of data
print(reverse_order) # ['beckham', 'zidane', 'ibrahimovic', 'messi', 'suarez']

矩阵

列表可以是多维的。我上面提到的例子列表都是一维或单维的。但是,我们可以在列表中包含列表。二维列表是这样的:

matrix_2 = [[1,3,2], [1,3,2], [2,3,4], [2,3,5]]
first_item = matrix_2[0]
print(first_item) # [1,3,2]
first_item_first_element = matrix_2[0][0] # or first_item[0]
print(first_item_first_element) # 1

类似地,我们可以在列表中嵌套任意数量的列表,从而创建不同维度的矩阵,这与我们学过的数学矩阵类似。这种矩阵数据有助于存储图像等复杂数据,并用于机器学习模型。探索它们并在以后详细地看到它们的实际应用将是非常有趣的。

今天休息一下,明天继续学习剩下的列表概念,比如它的函数和方法等模式,然后学习剩下的数据类型字典,元组,集合,none。我逐渐发现了探索这些数据结构的乐趣。希望你觉得它们有趣,同时也跟着做。明天再见!