Some tips for Python

Source: Internet
Author: User

#coding: Utf-8

#数据交换
x = 6
y = 5
X, y = y, X
Print X
Print Y

#相当于:?
print "Hello" if False else "world"

#一种方式绑定不同类型的数据
NFC = ["Packers", "49ers"]
AFC = ["Ravens", "patriots"]
Print NFC + AFC
Print str (1) + "World"
print ' 1 ' + "world"
Print nfc,1

#注意浮点除法
Print 5.0/2
Print 2**5
Print. 3/.1
Print. 3//.1

#数值比较 very good
x = 2
If 3>x>1:
Print X
If 1<x>0:
Print X
#同时迭代两个列表
one = [' Packers ', ' Atteck ']
both = [' reaf ', ' Dedad ']
For a, b in Zip (one,two):
Print A + "vs." +b
#带索引的列表迭代
Teams = [' one ', ' one ', ' three ', ' four ']
For Index,team in Enumerate (teams): #enumerate枚举列举
Print Index,team

#过滤出偶数
numbers = [1,2,3,4,5,6,7]
even = []
For number in numbers:
If number%2 = = 0:
Even.append (number)
Print even
Even2 = [number for number in numbers if number%2==0]
Print Even2

#字典
Teams = [' Onee ', ' both ', ' three ', ' four ', ' five ', ' six ']
Print {Key:value for key,value in enumerate (teams)} #注意是根据for后面的顺序展示
#初始化列表
Items = [0]*3
Print items

#列表转换为字符串
Teams = [' one ', ' one ', ' one ', ' three ', ' four ', ' five ', ' six ']
Print teams
Print ",". Join (teams)

#从字典中获取元素
data = {' admin ': 1, ' name ': ' Max ', ' Three ': 4}
A = Data.get (' admin ', False)
Print a
Try
Is_admin = data[' name ']
Except Keyerror:
Is_admin = False
Print Is_admin

#获取列表的子集
x = [1,2,3,4,5,6,7]
Print X[:3]
Print X[1:3]
Print X[-5:]
Print X[1::2]

#3的倍数打印fizz 5 multiples print buzz both multiples print Fizzbuzz
For x in range (101):p rint "Fizz" [x%3*4::] + "Buzz" [x%5*4::] or X

#迭代工具 Random Combination
From Itertools import combinations
Teams = [' one ', ' one ', ' three ', ' four ']
For game in Combinations (teams,2):
Print game

Some tips for Python

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.