Shortcuts for beginners in python [translate]

Source: Internet
Author: User
Tags float number

The following are the useful points of knowledge and some tools that are summarized in python over the years. Hope to help you!

Exchange variable values

x = 6= 5=print  x>>> 5print  y>>> 6

inline if statement

Print " Hello " if Else "  World ">>> Hello

Join

NFC = ["Packers","49ers"]AFC= ["Ravens","Patriots"]PrintNFC +AFC>>> ['Packers','49ers','Ravens','Patriots'] PrintSTR (1) +" World">>> 1 WorldPrint' 1 ' +" World">>> 1 WorldPrint1," World">>> 1 WorldPrintNFC, 1>>> ['Packers','49ers'] 1

Numeric operations

# Floor Division (rounds down) Print # 2 raised to the 5th power Print 2**5>> 32

Note that the float number is an integer after the divide operation.

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

Digital comparison

if 3 > x > 1:    print  xif 1 < x > 0:    print
     x>>> 2

Traverse two arrays at a time

NFC = ["Packers","49ers"]AFC= ["Ravens","Patriots"]  forTeamA, TeambinchZip (NFC, AFC):PrintTeamA +"vs."+Teamb>>>Packers vs. Ravens>>> 49ers vs. Patriots

To extend the Read Zip method, click here.

Traverse list and get index

 teams = [ packers   ", "  49ers  , "  ravens  , "  patriots   " "  for  index, Team in   Enumerate (teams):  print  Span style= "color: #000000;" > index, team  >>> 0 Packers  >>> 1 49ers  >>> 2 Ravens  >>> 3 Patriots 

Understanding List

This one:

numbers = [1,2,3,4,5,6= [ ] for in numbers:    if number%2 = = 0:        even.append (number)

Can be written as:

numbers = [1,2,3,4,5,6forif number%2 = = 0]

Understanding Dictionary

Similar to list, this dictionary can be written as:

Teams = ["Packers","49ers","Ravens","Patriots"]Print{Key:value forValue, keyinchenumerate (teams)}>>> {'49ers': 1,'Ravens': 2,'Patriots': 3,'Packers': 0}

Initialize the value of the list

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

The list is converted to string

Teams = ["Packers""49ers" "Ravens"  "Patriots"]print""  . Join (teams)'Packers, 49ers, Ravens, Patriots'

Get Item from Dictionary

In general, a try-except block is wrapped in case the key value to be obtained does not exist:

data = {'user''name' 'Max'  three': 4}try:    = data['admin  ']except  keyerror:    = False

But you can do this:

data = {'user''name' 'Max'  three': 4= Data.get ('admin', False)

To take a subset of the list

A very simple way to take a subset:

x = [1,2,3,4,5,6] #First 3PrintX[:3]>>> [About] #Middle 4PrintX[1:5]>>> [2,3,4,5] #Last 3PrintX[-3:]>>> [4,5,6] #ODD NumbersPrintX[::2]>>> [1,3,5] #even numbersPrintX[1::2]>>> [2,4,6]

60 characters Fix fizzbuzz problem

Fizzbuzz problem: Write a program to print numbers from 1 to 100. You encounter multiples of 3 to print "Fizz" to replace this number. Multiples of 5 print "Buzz" for numbers that are both multiples of 3 and 5 are printed "Fizzbuzz" instead of this number

 for  in Range (1,101):print"Fizz"[x%3*4:]+"Buzz" [x%5*4:]or x

Here is a list of knowledge points. If the number on the left side of the colon is greater than the length of the string in square brackets, then nothing will be output! That is, in multiples of 3 or 5, nothing is output.

Collection

Counter under the collections module is sometimes useful.

 from Import  Print Counter ("hello")>>> Counter ({'  L'h'e''o  ': 1})

Itertools

 fromItertoolsImportCombinations Teams= ["Packers","49ers","Ravens","Patriots"] forGameinchCombinations (teams, 2):    PrintGame>>> ('Packers','49ers')>>> ('Packers','Ravens')>>> ('Packers','Patriots')>>> ('49ers','Ravens')>>> ('49ers','Patriots')>>> ('Ravens','Patriots')

False = = True

In Python, true and false are just global variables, so:

false = Trueif  False:    print"Hello"Else  :    print"World" >>> Hello

If you find anything interesting, welcome to the bottom!

from:http://www.maxburstein.com/blog/python-shortcuts-for-the-python-beginner/

Shortcuts for beginners in python [translate]

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.