Python入门—亲测好用的Python技巧分享(含最优方法对比)

相信每个刚入门学习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工作流程)
  • 逆向工程篇
  • 逆向算法篇
  • 异步爬虫篇
  • 安卓逆向篇

资料持续更新中,目前全部都是免费送给大家,如果有需要,尽管拿走

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Preface Like any programmer, you need to be able to produce reliable code that conforms to a specification, which means that you need to test your code. In this book, you'll learn how to use techniques and Python tools that reduce the effort involved in testing, and at the same time make it more useful—and even fun. You'll learn about several of Python's automated testing tools, and you'll learn about the philosophies and methodologies that they were designed to support, like unit testing and test-driven development. When you're done, you'll be able to produce thoroughly tested code faster and more easily than ever before, and you'll be able to do it in a way that doesn't distract you from your "real" programming. What this book covers Chapter 1: Testing for Fun and Profit introduces Python test-driven development and various testing methods. Chapter 2: Doctest: The Easiest Testing Tool covers the doctest tool and teaches you how to use it. Chapter 3: Unit Testing with Doctest introduces the ideas of unit testing and test-driven development, and applies doctest to create unit tests. Chapter 4: Breaking Tight Coupling by using Mock Objects covers mock objects and the Python Mocker tool. Chapter 5: When Doctest isn't Enough: Unittest to the Rescue introduces the unittest framework and discusses when it is preferred over doctest. Chapter 6: Running Your Tests: Follow Your Nose introduces the Nose test runner, and discusses project organization. This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Preface [ ] Chapter 7: Developing a Test-Driven Project walks through a complete test-driven development process. Chapter 8: Testing Web Application Frontends using Twill applies the knowledge gained from previous chapters to web applications, and introduces the Twill tool. Chapter 9: Integration Testing and System Testing teaches how to build from unit tests to tests of a complete software system. Chapter 10: Other Testing Tools and Techniques introduces code coverage and continuous integration, and teaches how to tie automated testing into version control systems. Appendix: Answers to Pop Quizes contains the answers to all pop quizes, chapter-wise. What you need for this book To use this book, you will need a working Python interpreter, preferably one of the 2.6 version series. You'll also need a source code editor, and occasional access to the internet. You will need to be comfortable enough using your operating system's textual interface—your DOS prompt or command shell—to do basic directory management and to run programs. Who this book is for If you are a Python developer and want to write tests for your applications, this book will get you started and show you the easiest way to learn testing. You need to have sound Python programming knowledge to follow along. An awareness of software testing would be good, but no formal knowledge of testing is expected nor do you need to have any knowledge of the libraries discussed in the book.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值