跟着我们一起学 Python 30天课程-第3天-数据类型II(Data Types II)
温馨提示: 30节完整课程目录在最后面
继续我昨天学到的字符串数据类型,今天我探索了其他一些功能。
格式化字符串 Formatted String
字符串格式化是一项简洁的功能,可让我们动态更新字符串内容。假设我们从服务器获取了用户信息,并希望基于该信息显示自定义消息。第一个想法是应用字符串连接,例如
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
字符串索引 String Indexes
Python中的字符串只是字符的有序集合。因此,我们可以用它做很多很酷的技巧。我们可以访问字符串的字符,选择子字符串,反转字符串,而且非常容易。这也称为字符串切片。
language = 'python'
first_character = language[0] # indexing starts from 0
second_character = language[1]
print(first_character) # p
print(second_character) # y
# Strings can be manipulated easily with this syntax [start:stop:step-over]
range_1 = language[0:2] # here it starts from index 0 and ends at index 1
range_2 = language[0::1] # starts at 0, stops at end with step over 1
range_3 = language[::2] # starts at 0, till end with step 2
range_4 = language[1:] # starts at index 1 till end of string
range_5 = language[-1] # selects last character
range_6 = language[-2] # second last character
reverse_string = language[::-1] # starts from end and reverses the string
reverse_string_2 = language[::-2] # reverses string and skips 1 character
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
https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3
不变性 Immutability
字符串本质上是不可变的。这意味着字符串的值不能被篡改或更改。
favorite_website = 'dev.to'
favorite_website[0] = 'w'
print(favorite_website) # TypeError: 'str' object does not support item assignment
内置字符串函数和方法 Built-in string functions and methods
Python具有一些内置函数和方法来对字符串数据类型进行操作。函数通常是可以像这样独立调用的动作,print()
round()
而方法只是对象的一部分并由.
运算符调用的简单函数。
quote = 'javascript is awesome'
print(len(quote)) # 21 (len calculates total no of characters)
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!)
https://www.w3schools.com/python/python_ref_functions.asp
https://www.w3schools.com/python/python_ref_string.asp
布尔型 Boolean
如bool
python中所示的布尔值,并存储True
或False
。
is_cool = True
is_dirty = False
print(10 > 9) # True
注释 Comments
注释是用代码编写的语句,以增强其可读性。在Python中,它们是在#
符号后加上注释的。注释将被解释器忽略,仅用于代码可读性目的。我已经在代码示例中使用它们来打印输出或添加一些注释。根据良好的编程习惯,我们应该尽可能使我们的代码更具可读性,就像阅读英语,并仅在需要时才向其中添加注释,因为带有过多注释的膨胀代码会适得其反。
# This is not a very useful comment but written just for the sake of demonstration
列表 Lists
列表是重要的数据类型。它们是对象的有组织的集合。它也是一个数据结构,这意味着一个容器以某种特定格式组织数据以用于不同目的。它们就像来自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'
列表切片 List Slicing
与字符串类似,可以对列表进行切片。但是,列表与字符串不同,它是可变的,这意味着可以更改其数据。
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
列表可以是多维的。我上面提到的列表示例都是一维或一维的。但是,我们可以在列表内包含列表。所以二维列表看起来像这样
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
同样,我们可以在列表中嵌套任意数量的列表,以创建与我们在小学中学到的数学矩阵相似的不同维度的矩阵。这种矩阵数据有助于存储图像等复杂数据并用于机器学习模型。探索它们并稍后详细查看它们的实际应用将非常有趣。
我今天休息一下,继续学习List的其余概念,例如其功能和方法以及其他模式,然后学习剩余的数据类型字典,元组,集合和全无。在逐步探索这些数据结构方面,我发现了很多兴趣。希望您在跟随的同时也发现它们很酷。明天再见吧!
跟着我们一起学 Python 30天课程目录:
- 跟着我们一起学 Python 30天课程-第30天-免费Python资源
- 跟着我们一起学 Python 30天课程-第29天-自动化测试
- 跟着我们一起学 Python 30天课程-第28天-ML和数据科学II
- 跟着我们一起学 Python 30天课程-第27天-ML和数据科学I
- 跟着我们一起学 Python 30天课程-第26天-机器学习基础
- 跟着我们一起学 Python 30天课程-第25天-Web 开发进阶
- 跟着我们一起学 Python 30天课程-第24天-Web开发基础
- 跟着我们一起学 Python 30天课程-第23天-网页爬虫
- 跟着我们一起学 Python 30天课程-第22天-脚本额外功能Scripting Extras
- 跟着我们一起学 Python 30天课程-第21天-脚本编写基础
- 跟着我们一起学 Python 30天课程-第20天-调试和测试
- 跟着我们一起学 Python 30天课程-第19天-正则表达式
- 跟着我们一起学 Python 30天课程-第18天-文件I / O
- 跟着我们一起学 Python 30天课程-第17天-外部模块External Modules
- 跟着我们一起学 Python 30天课程-第16天-模块基础Module Basics
- 跟着我们一起学 Python 30天课程-第15天-生成器Generators
- 跟着我们一起学 Python 30天课程-第14天-错误处理Error Handling
- 跟着我们一起学 Python 30天课程-第13天-Decorators
- 跟着我们一起学 Python 30天课程-第12天-Lambda Expressions & Comprehensions
- 跟着我们一起学 Python 30天课程-第11天-函数编程Functional Programming基础
- 跟着我们一起学 Python 30天课程-第10天-OOP Missing Pieces
- 跟着我们一起学 Python 30天课程-第9天-OOP Pillars
- 跟着我们一起学 Python 30天课程-第8天-OOP基础知识
- 跟着我们一起学 Python 30天课程-第7天-开发环境搭建(Developer Environment)
- 跟着我们一起学 Python 30天课程-第6天-循环II和函数(Loops II & Functions)
- 跟着我们一起学 Python 30天课程-第5天-条件和循环I(Conditions & Loops I)
- 跟着我们一起学 Python 30天课程-第4天-数据类型III(Data Types III)
- 跟着我们一起学 Python 30天课程-第3天-数据类型II(Data Types II)
- 跟着我们一起学 Python 30天课程-第2天-数据类型I(Data Types I)
- 跟着我们一起学 Python 30天课程-第1天-简介
1. 本站资源转自互联网,源码资源分享仅供交流学习,下载后切勿用于商业用途,否则开发者追究责任与本站无关!
2. 本站使用「署名 4.0 国际」创作协议,可自由转载、引用,但需署名原版权作者且注明文章出处
3. 未登录无法下载,登录使用金币下载所有资源。
IT小站 » 跟着我们一起学 Python 30天课程-第3天-数据类型II(Data Types II)
常见问题FAQ
- 没有金币/金币不足 怎么办?
- 本站已开通每日签到送金币,每日签到赠送五枚金币,金币可累积。
- 所有资源普通会员都能下载吗?
- 本站所有资源普通会员都可以下载,需要消耗金币下载的白金会员资源,通过每日签到,即可获取免费金币,金币可累积使用。