17 Tips for Beginners in Python

Source: Internet
Author: User
Swap variables

x = 6y = 5x, y = y, xprint x>>> 5print y>>> 6

If statement in row

print "Hello" if True else "world" >>> Hello

Connection

The last way to do this is to be cool when binding two different types of objects.

NFC = ["Packers", "49ers"]AFC = ["Ravens", "patriots"]print NFC + afc>>> [' Packers ', ' 49ers ', ' Ravens ', ' Patriots ']print str (1) + "World" >>> 1 worldprint ' 1 ' + "World" >>> 1 worldprint 1, "World" >>> 1 WORLDP Rint NFC, 1>>> [' Packers ', ' 49ers '] 1

Calculation techniques

#向下取整print 5.0//2>>> 2 5-time print 2**5>> 32

Note the division of floating-point numbers

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

Numerical comparison

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

Two simultaneous iteration of lists

NFC = ["Packers", "49ers"]AFC = ["Ravens", "patriots"]for TeamA, Teamb in Zip (NFC, AFC):     print TeamA + "vs." + Teamb >>> Packers vs. Ravens>>> 49ers vs. Patriots

List Iteration with index

Teams = ["Packers", "49ers", "Ravens", "patriots"]for index, team in enumerate (teams):    Print index, team>>> 0 Packers>>> 1 49ers>>> 2 ravens>>> 3 Patriots

List derivation

A list of known, brush to select even list method:

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

Replace it with the following

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

Dictionary derivation

Teams = ["Packers", "49ers", "Ravens", "patriots"]print {Key:value for value, key in enumerate (teams)}>>> {' 49er S ': 1, ' Ravens ': 2, ' Patriots ': 3, ' Packers ': 0}

Initialize the value of the list

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

Convert a list to a string

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

Get elements from a dictionary

Do not use the following methods

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

Replaced by

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

Get child list

x = [1,2,3,4,5,6] #前3个print x[:3]>>> [x-ray] #中间4个print x[1:5]>>> [2,3,4,5] #最后3个print x[-3:]>> > [4,5,6] #奇数项print x[::2]>>> [1,3,5] #偶数项print x[1::2]>>> [2,4,6]

60 Character Resolution Fizzbuzz

A short time ago Jeff Atwood promoted a simple programming exercise called Fizzbuzz, which referred to the following questions:


Write a program that prints the number 1 to 100, 3 multiples print "Fizz" to replace this number, 5 multiples print "Buzz", for a number that is both a multiples of 3 and a multiples of 5 to print "Fizzbuzz".

Here is a short way to solve this problem:

For x in range (101):p rint "Fizz" [x%3*4::]+ "Buzz" [X%5*4::]or X

Collection

Use the Counter library.

From collections import Counterprint Counter ("Hello") >>> Counter ({' L ': 2, ' h ': 1, ' E ': 1, ' O ': 1})

Iterative Tools

Like the collections library, there's a library called Itertools.

From itertools Import combinationsteams = ["Packers", "49ers", "Ravens", "patriots"]for game in Combinations (teams, 2): 
  print game>>> (' Packers ', ' 49ers ') >>> (' Packers ', ' Ravens ') >>> (' Packers ', ' patriots ') >>> (' 49ers ', ' Ravens ') >>> (' 49ers ', ' Patriots ') >>> (' Ravens ', ' patriots ') False = = True

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

False = Trueif false:   print "Hello" else:   print "World" >>> Hello
  • Related Article

    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.