当前位置: 首页>前端>正文

一看就会的20个“非常有用”的python小技巧,你一定要试试

Hello,大家好,我是Alex~

Python现在非常流行,主要是因为它简单,容易学习。你可以用它来完成很多任务,比如数据科学和机器学习、 web开发、脚本编写、自动化等。

这里总结了20条很有用的tips给你:

01?把列表中的值作为参数传递给方法

可以使用" * "提取列表中的所有元素:

my_list= [1, 2, 3, 4]print(my_list)? # [1, 2, 3, 4]print(*my_list)? # 1 2 3 4

当我们想将列表中的所有元素作为方法参数传递时,这很有用:

def sum_of_elements(*arg):

? ? total = 0

? ? for i in arg:

? ? ? ? total += i

? ? return total

result = sum_of_elements(*[1, 2, 3, 4])

print(result)? # 10

02?获取列表的所有中间元素

_, *elements_in_the_middle, _ = [1, 2, 3, 4, 5, 6, 7, 8]

print(elements_in_the_middle) # [2, 3, 4, 5, 6, 7]

03?一行赋值多个变量

one, two, three, four = 1, 2, 3, 4

04?列表推导

你可以使用推导如,让我们将列表中的每个数字都取二次方:

numbers = [1, 2, 3, 4, 5]

squared_numbers = [num * num for num in numbers]

print(squared_numbers)

推导不仅仅局限于使用列表。还可以将它们与字典、集合和生成器一起使用。让我们看另一个例子,使用字典推导将一个字典的值提升到二阶:

![](100 Helpful Python Tips You Can Learn Before Finishing Your Morning Coffee.assets/1_4D3OCbHOCfHiI8A3ru4xRQ.png)

Comprehensions are not just limited to working with lists. You can also use them with dictionaries, sets, and generators as well.

dictionary = {'a': 4, 'b': 5}squared_dictionary= {key: num * num for (key, num) in dictionary.items()}print(squared_dictionary)? # {'a': 16, 'b': 25}

05?一行打印多个元素

print("Hello", end="")

print("World")? # HelloWorld

print("Hello", end=" ")

print("World")? # Hello World

print('words',? 'with', 'commas', 'in', 'between', sep=', ')

# words, with, commas, in, between

06 不使用循环来重复字符串

name = "Banana"

print(name * 4)? # BananaBananaBananaBanana

07?打印多个值,每个值之间使用自定义分隔符

你可以很容易地做高级打印:

print("29", "01", "2022", sep="/")? # 29/01/2022

print("name", "domain.com", sep="@")? # name@domain.com

08 不能在变量名的开头使用数字

four_letters = “abcd” # this works4_letters = “abcd” # this doesn’t work

09 不能在变量名的开头使用操作符

+variable = “abcd”? # this doesn’t work

10?颠倒列表的顺序

my_list = ['a', 'b', 'c', 'd']

my_list.reverse()

print(my_list)? # ['d', 'c', 'b', 'a']

11 使用step函数对字符串切片

my_string= "This is just a sentence"print(my_string[0:5])? # This# Take three steps forwardprint(my_string[0:10:3])? # Tsse

12 反向切片

my_string = "This is just a sentence"

print(my_string[10:0:-1])? # suj si sih

# Take two steps forward

print(my_string[10:0:-2])? # sjs i

13 只有开始或结束索引的部分切片

表示切片的开始和结束的索引可以是可选的。

my_string = "This is just a sentence"

print(my_string[4:])? # is just a sentence

print(my_string[:3])? # Thi

14?你不能把0作为数字的第一个数字

number = 0110 # this doesn't work

15 Floor 除法

print(3/2)? # 1.5

print(3//2)? # 1

16 == 和 “is” 的差别

" is "检查两个变量是否指向内存中的同一个对象。" == "比较这两个对象的值是否相等。

first_list = [1, 2, 3]

second_list = [1, 2, 3]

# Is their actual value the same

print(first_list == second_list)? # True

# Are they pointing to the same object in memory

print(first_list is second_list)?

# False, since they have same values, but in different objects in memory

third_list = first_list

print(third_list is first_list)?

# True, since both point to the same object in memory

17 更改分配给另一个变量的变量的值

当一个变量被赋值给另一个变量时,它的值实际上被复制到第二个变量中。这意味着第一个变量之后的任何变化都不会反映在第二个变量中:

first = "An initial value"

second = first

first = "An updated value"

print(first)? # An updated value

print(second)? # An initial value

18 检查一个字符串是否大于另一个字符串

first = "abc"

second = "def"

print(first < second)? # True

second = "ab"

print(first < second)? # False

19 检查字符串是不是从特定字符开始的

my_string = "abcdef"

print(my_string.startswith("b"))? # False

20 使用id()找到变量的唯一id

print(id(1))? # 4325776624

print(id(2))? # 4325776656

print(id("string"))? # 4327978288


https://www.xamrdz.com/web/27k1874578.html

相关文章: