Some programming tips for Python beginners
- 2020-05-05 11:23:15
- OfStack
x = 6
y = 5
x, y = y, x
print x
>>> 5
print y
>>> 6
The if statement is
in the line
print "Hello" if True else "World"
>>> Hello
connects
The last approach below is very 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 world
print `1` + " world"
>>> 1 world
print 1, "world"
>>> 1 world
print nfc, 1
>>> ['Packers', '49ers'] 1
number tip
# Divide and round down
print 5.0//2
>>> 2
# 2 the 5 To the power
print 2**5
>> 32
pays attention to the division
of floating point Numbers
print .3/.1
>>> 2.9999999999999996
print .3//.1
>>> 2.0
numerical comparison
This is one of the few languages I've seen that is as good and simple as
x = 2
if 3 > x > 1:
print x
>>> 2
if 1 < x > 0:
print x
>>> 2
iterates over both 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
's indexed list iterates over
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
list derivation
Given a list, we can select even list method:
numbers = [1,2,3,4,5,6]
even = []
for number in numbers:
if number%2 == 0:
even.append(number)
Convert to the following:
numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 == 0]
Isn't that awesome, haha.
dictionary derives
Similar to list derivation, dictionaries can do the same thing:
print "Hello" if True else "World"
>>> Hello
0
initializes the value
of the list
print "Hello" if True else "World"
>>> Hello
1
The list is converted to the string
print "Hello" if True else "World"
>>> Hello
2
gets the element
from the dictionary
I admit that the try/except code is not elegant, but here's an easy way to do it: try looking up key in the dictionary, and if you don't find a corresponding alue, set the second argument to its variable value.
data = {'user': 1, 'name': 'Max', 'three': 4}
try:
is_admin = data['admin']
except KeyError:
is_admin = False
1
Substitution:
print "Hello" if True else "World"
>>> Hello
4
gets a subset of the list
Sometimes, you only need partial elements in the list. Here are some ways to get a subset of the list.
print "Hello" if True else "World"
>>> Hello
5
60 characters solve FizzBuzz
Some time ago Jeff Atwood popularized a simple programming exercise called FizzBuzz
writes a program that prints the Numbers 1 to 100, replaces this number with "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for Numbers that are both multiples of 3 and 5.
Here's a short, interesting way to solve this problem:
print "Hello" if True else "World"
>>> Hello
6
set
In addition to python's built-in data types, the collection module also includes special use cases, where Counter is useful. If you've attended Facebook HackerCup this year, you might even find it useful.
print "Hello" if True else "World"
>>> Hello
7
iteration tool
Like the collections library, there is a library called itertools that is really efficient at solving certain problems. One of the use cases is to find all combinations, which can tell you all the possible combinations of elements in a group
from itertools import combinations
teams = ["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
This is a more interesting thing than practical techniques. In python, True and False are global variables, so
print "Hello" if True else "World"
>>> Hello
9