17 tips on Python

Source: Internet
Author: User
This article mainly shares 17 tips on Python, which are basic and commonly used and useful tips. we recommend these tips to you. 1. exchange variables

The 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

The 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.

The 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

The 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.

The code is as follows:


Print. 3/. 1
>>> 2.9999999999999996
Print. 3 //. 1
>>> 2.0

5. numerical comparison

The 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.

The 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

The 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:

The 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

The code is as follows:


Numbers = [1, 2, 3, 4, 5, 6]
Even = [number for number in numbers if number % 2 = 0]

9. dictionary derivation

The 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.

The code is as follows:


Items = [0] * 3
Print items
>>> [0, 0]

11. convert the list to a string

The 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

The code is as follows:


Data = {'user': 1, 'name': 'Max ', 'Three': 4}
Is_admin = data. get ('admin', False)

13. obtain the sublist

The 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

The 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

The 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:

The 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.