关于python:一看就会的20个非常有用的python小技巧你一定要试试

1次阅读

共计 2809 个字符,预计需要花费 8 分钟才能阅读完成。

Hello,大家好,我是陈程~

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)

推导不仅仅局限于应用列表。还能够将它们与字典、汇合和生成器一起应用。让咱们看另一个例子,应用字典推导将一个字典的值晋升到二阶:

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

最初

心愿以上我所分享的这些,可能给小伙伴们带来一些帮忙!

若小伙伴有其它更好的倡议或意见,欢送在评论区进行探讨。

正文完
 0