# Python super invincible skill sharing

This is a series of articles, mainly sharing python's use suggestions and skills, 3 points at a time, I hope you can get something.
Note: many people will encounter all kinds of problems in the process of learning python. No one can easily give up. For this reason, I have built a python full stack for free answer. Skirt: seven clothes, nine seven seven bars and five (homophony of numbers) can be found under conversion. The problems that I don't understand are solved by the old driver and the latest Python practical course. We can supervise each other and make progress together!

### 1 arrangement and combination

Sample program:

```#!/usr/bin/env python
# coding=utf8

import itertools

for p in itertools.permutations('ABC', 2):
print p

'''
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')
'''

for c in itertools.combinations('ABC', 2):
print c

'''
('A', 'B')
('A', 'C')
('B', 'C')
'''
```

Through itertools module, it is very convenient to arrange and combine elements. As can be seen from the example, take two letters from the three letters of ABC to realize the arrangement and combination. Itertools module has many useful functions, which can be seen if you are interested.

### 2 create temporary files

Sample program:

```#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tempfile

TEMP_FILE = tempfile.NamedTemporaryFile()
print 'temp file name: <{self.name}>\n'.format(self=TEMP_FILE)

with open(TEMP_FILE.name, 'w') as f:
f.write("line 1\nline 2\nline 3\n")

with open(TEMP_FILE.name) as f:
print line
```

Running example:

```\$ python tmp_file_demo.py
temp file name: </tmp/tmpVSppeA>

line 1

line 2

line 3

\$ ls /tmp/tmpVSppeA
ls: cannot access /tmp/tmpVSppeA: No such file or directory
```

With the help of tempfile module, you can easily operate temporary files. As can be seen from the example, the created temporary file / tmp/tmpVSppeA will be deleted automatically after use, and there is no need to delete the file manually. The tempfile module has many useful functions, which you can have a look at if you are interested.

### 3 printing information to standard error

Sample program:

```#!/usr/bin/env python
# coding=utf8

from __future__ import print_function
import sys

def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)

eprint("print to stderr")
print("print to stdout")

'''
print to stderr
print to stdout
'''
```

Running example:

```\$ python print_stderr.py
print to stderr
print to stdout
\$ python print_stderr.py > /tmp/stdout.log
print to stderr
\$ python print_stderr.py 2> /tmp/stderr.log
print to stdout
\$ python print_stderr.py > /tmp/stdout_and_stderr.log 2>&1
\$ cat /tmp/stdout.log
print to stdout
\$ cat /tmp/stderr.log
print to stderr
\$ cat /tmp/stdout_and_stderr.log
print to stderr
print to stdout
```

By importing the print function of the future module and transforming the print function into the print of python3, the output can be printed to the standard error. As can be seen from the example, by encapsulating a new function eprint, the printing function similar to print is realized. The only difference is that the eprint function prints the output to the standard error instead of the standard output.

Summary note: many people will encounter a variety of problems in the process of learning python, no one can easily give up. For this reason, I have built a python full stack for free answer. Skirt: seven clothes, nine seven seven bars and five (homophony of numbers) can be found under conversion. The problems that I don't understand are solved by the old driver and the latest Python practical course. We can supervise each other and make progress together!

The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Keywords: Python