Children learn some of python's tricks

  • 2020-10-23 20:09:40
  • OfStack

Here are 1 practical Python tips and tools that I hope will help you.

Switching variable


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

The if statement is inside the line


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

The connection

The last one below is cool for 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

Digital techniques

Number divided and rounded down


print 5.0//2
>>> 2
# 2 the 5 To the power 
print 2**5
>> 32

Note the division of floating point Numbers


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

The numerical comparison

This is one of the best shortcuts I've seen in many languages


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


Iterate through both lists simultaneously


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


Iterate through the list with indexes


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 1 list, we can swipe to select an even-numbered list:


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


This translates into the following:


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


Isn't that awesome? Haha.

The dictionary is

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


A list is converted to a string


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


Get the element from the dictionary

I'll admit that the try/except code isn't elegant, but here's an easy way to look up key in a dictionary, and if you don't find a 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)


Gets 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]
Before the # 3
print x[:3]
> > > [1,2,3]
Number four in the middle
print x[1:5]
> > > [2,3,4,5]
Student: # last 3
print x[-3:]
> > > [4,5,6]
# odd term
print x[::2]
> > > [1,3,5]
Item # even
print x[1::2]
> > > [2,4,6]

]

60 characters resolve FizzBuzz

Some time ago, Jeff Atwood promoted a simple programming exercise called FizzBuzz. The question is quoted as follows:

Write a program that prints the Numbers 1 through 100, and replaces this number with "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of 3 and 5.

Here is a short and interesting way to solve this problem:

A collection of

In addition to the data types built into python, the collection module also includes a special use case, which is useful in some cases. If you have attended Facebook HackerCup this year, you can even find it useful.


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


Iterative tool

Like the collections library 1, there is another library called itertools, which is really efficient for some problems. One of the use cases is to look for all combinations, which can tell you all the combinations of elements in a group that can't be combined


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


This is an interesting thing compared to practical techniques. In python, True and False are global variables, so:


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


If you have any cool tricks, please feel free to leave a comment


Related articles: