相信每个刚入门学习python的时候肯定会遇到很多难题,以及对于新技术的追求,这里我给大家整理了一份资料,是当初自己学习的时候一点点积累下来的,全部是真正有用的东西,内有安装包,学习视频资料,学习书籍资料等等,领取方式放在文末了。
1.交换变量
在Python中如果需要交换变量的值,我们无需定义临时变量来操作。我们一般使用如下代码来实现变量交换
v1 = 100
v2 = 200
# bad practice
temp = v1
v1 = v2
v2 = temp
但是更好的处理方法如下
v1 = 100
v2 = 200
# good practice
v1, v2 = v2, v1
2.判断字符串是否为回文串
将字符串进行反转最简单的实现方式为 [::-1] ,代码如下
print("John Deo"[::-1])
3.反转字符串
在Python中判断一个字符串是否为回文串,只需要使用语句
string.find(string[::-1])== 0,示例代码如下
v1 = "madam" # is a palindrome string
v2 = "master" # is not a palindrome string
print(v1.find(v1[::-1]) == 0) # True
print(v1.find(v2[::-1]) == 0) # False
4.尽量使用 Inline if statement
大多数情况下,我们在条件之后只有一个语句,因此使用 Inline if statement 可以帮助我们编写更简洁的代码。举例如下,一般的写法为
name = "ali"
age = 22
# bad practices
if name:
print(name)
if name and age > 18:
print("user is verified")
但是更好的处理方法如下
# a better approach
print(name if name else "")
""" here you have to define the else condition too"""
# good practice
name and print(name)
age > 18 and name and print("user is verified")
5.使用*args传递多个参数
在Python中我们可以使用*args来向函数传递多个参数,举例如下
def sum_of_squares(n1, n2)
return n1**2 + n2**2
print(sum_of_squares(2,3))
# output: 13
"""
what ever if you want to pass, multiple args to the function
as n number of args. so let's make it dynamic.
"""
def sum_of_squares(*args):
return sum([item**2 for item in args])
# now you can pass as many parameters as you want
print(sum_of_squares(2, 3, 4))
print(sum_of_squares(2, 3, 4, 5, 6))
6.在循环时处理下标
有时我们在工作中,想要获得循环中元素的下标,一般来说,比较优雅的写法如下
lst = ["blue", "lightblue", "pink", "orange", "red"]
for idx, item in enumerate(lst):
print(idx, item)
最后
决心学习一件新的事情是很值得敬佩的事情,在这里我也把自己知道的分享出来,希望大家少走弯路。这是一份入门级别的学习资料,适合新手,包含以下几个方面的内容:
- 爬虫入门篇(内含爬虫工作流程 http工作流程)
- 逆向工程篇
- 逆向算法篇
- 异步爬虫篇
- 安卓逆向篇
资料持续更新中,目前全部都是免费送给大家,如果有需要,尽管拿走