17 tips for Python and 17 tips for Python

Source: Internet
Author: User

17 tips for Python and 17 tips for Python

1. Exchange Variables

Copy codeThe Code is as follows:
X = 6
Y = 5

X, y = y, x

Print x
>>> 5
Print y
>>> 6

2. The if statement is in the row

Copy codeThe Code is as follows:
Print "Hello" if True else "World"
>>> Hello

3. Connection

The last method below looks cool when binding two different types of objects.

Copy codeThe Code is as follows:
Nfc = ["Packers", "49ers"]
Afc = ["Ravens", "Patriots"]
Print nfc + afc
>>> ['Packers', '49ers', 'raws', 'pattern']

Print str (1) + "world"
>>> 1 world

Print '1' + "world"
>>> 1 world

Print 1, "world"
>>> 1 world
Print nfc, 1
>>> ['Packers', '49ers'] 1

4. computing skills

Copy codeThe Code is as follows:
# Round down
Print 5.0 // 2
>>> 2
# Power 5 of 2
Print 2 ** 5
> 32

Pay attention to the division of floating point numbers.

Copy codeThe Code is as follows:
Print. 3/. 1
>>> 2.9999999999999996
Print. 3 //. 1
>>> 2.0

5. Numerical Comparison

Copy codeThe Code is as follows:
X = 2
If 3> x> 1:
Print x
>>> 2
If 1 <x> 0:
Print x
>>> 2

6. Two lists are iterated simultaneously.

Copy codeThe Code is as follows:
Nfc = ["Packers", "49ers"]
Afc = ["Ravens", "Patriots"]
For teama, teamb in zip (nfc, afc ):
Print teama + "vs." + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots

7. List iteration with indexes

Copy codeThe Code is as follows:
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
For index, team in enumerate (teams ):
Print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots

8. List Derivation

If a list is known, the method for selecting an even number list is as follows:

Copy codeThe Code is as follows:
Numbers = [1, 2, 3, 4, 5, 6]
Even = []
For number in numbers:
If number % 2 = 0:
Even. append (number)

Replace the following

Copy codeThe Code is as follows:
Numbers = [1, 2, 3, 4, 5, 6]
Even = [number for number in numbers if number % 2 = 0]

9. dictionary Derivation

Copy codeThe Code is as follows:
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
Print {key: value for value, key in enumerate (teams )}
>>>{ '49ers': 1, 'raws': 2, 'pattern': 3, 'packers': 0}

10. initialize the value of the list.

Copy codeThe Code is as follows:
Items = [0] * 3
Print items
>>> [0, 0]

11. Convert the list to a string

Copy codeThe Code is as follows:
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
Print ",". join (teams)
>>> 'Packers, 49ers, Ravens, Patriots'

12. Retrieve elements from the dictionary

Do not use the following methods

Data = {'user': 1, 'name': 'max ', 'three': 4}
Try:
Is_admin = data ['admin']
Failed t KeyError:
Is_admin = False
Replace

Copy codeThe Code is as follows:
Data = {'user': 1, 'name': 'max ', 'three': 4}
Is_admin = data. get ('admin', False)

13. Obtain the sublist

Copy codeThe Code is as follows:
X = [1, 2, 3, 4, 5, 6]
# First 3
Print x [: 3]
>>> [1, 2, 3]
# Four in the middle
Print x [1: 5]
>>> [2, 3, 4, 5]
# Last 3
Print x [-3:]
>>> [4, 5, 6]
# Odd items
Print x [: 2]
>>> [1, 3, 5]
# Even items
Print x [1: 2]
>>> [2, 4, 6]

Fix FizzBuzz with 14.60 characters

Some time ago, Jeff Atwood promoted a simple programming exercise called FizzBuzz. The problem is referenced as follows:

Write a program, print the numbers 1 to, and print "Fizz" in multiples of 3 to replace this number, and print "Buzz" in multiples of 5 ", print "FizzBuzz" for numbers that are multiples of 3 and multiples of 5 ".
Here is a short way to solve this problem:

For x in range (101): print "fizz" [x % 3*4:] + "buzz" [x % 5*4:] or x

15. Set

Counter database used

Copy codeThe Code is as follows:
From collections import Counter
Print Counter ("hello ")
>>> Counter ({'l': 2, 'H': 1, 'E': 1, 'O': 1 })

16. iterative tools

Like the collections library, there is also a library called itertools

Copy codeThe Code is as follows:
From itertools import combinations
Teams = ["Packers", "49ers", "Ravens", "Patriots"]
For game in combinations (teams, 2 ):
Print game
>>> ('Packers', '49ers ')
>>> ('Packers', 'raws ')
>>> ('Packers', 'pattern ')
>>> ('49ers', 'raws ')
>>> ('49ers', 'pattern ')
>>> ('Raws', 'pattern ')

17. False = True

In python, True and False are global variables, so:

Copy codeThe Code is as follows:
False = True
If False:
Print "Hello"
Else:
Print "World"
>>> Hello

These are some of the commonly used Pyton tips I have summarized. They are suitable for beginners and I hope you will like them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.