程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python novices fantastic skills and sense of fullness

編輯:Python

Here are some of the things I've collected for a long time Python Practical skills and tools , Hope to be able to just learn Python Help novices .

1. Exchange variables

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

2.if Statement in line

print "Hello" if True else "World"
>>> Hello

3. Connect

The last way to bind two different types of objects looks like cool.

nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
print nfc + afc
>>> ['Packers', '49ers', 'Ravens', 'Patriots']
print str(1) + " world"
>>> 1 world
print `1` + " world"
>>> 1 world
print 1, "world"
>>> 1 world
print nfc, 1
>>> ['Packers', '49ers'] 1

4. Digital skills

# Divide and round down
print 5.0//2
>>> 2
# 2 Of 5 Power
print 2**5
>> 32

5. Pay attention to division of floating-point numbers

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

6. Numerical comparison

This is a simple method that I have rarely seen in many languages

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

7. Iterate over two lists at the same time

nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
for teama, teamb in zip(nfc, afc):
print teama + " vs. " + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots

Python Sharing of basic learning resources , Part time communication , Technical communication , They are all friends who study hard together , Welcome to join , Little surprise .

8. Indexed list iterations

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

9. List derivation

Know a list , We can brush out the even list method :

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

Change to the following :

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

Isn't it amazing , ha-ha .

10. Dictionary derivation

Similar to list derivation , Dictionaries can do the same thing :

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

11. Initializes the value of the list

items = [0]*3
print items
>>> [0,0,0]
12. List to string
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print ", ".join(teams)
>>> 'Packers, 49ers, Ravens, Patriots'

13. Get elements from Dictionary

I admit that try/except The code is not elegant , But here's a simple way , Try to find... In the dictionary key, If there is no corresponding alue Set the second parameter to its variable value .

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

14. Get a subset of the list

Sometimes , You only need some elements in the list , Here are some ways to get a subset of the list .

x = [1,2,3,4,5,6]
# front 3 individual
print x[:3]
>>> [1,2,3]
# middle 4 individual
print x[1:5]
>>> [2,3,4,5]
# Last 3 individual
print x[-3:]
>>> [4,5,6]
# Odd term
print x[::2]
>>> [1,3,5]
# Even term
print x[1::2]
>>> [2,4,6]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved