How to sort a list in Python?
Answer:
Use the sorted method.
list = [3, 2, 1]
print sorted(list)
It will give:
[1, 2, 3]
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
How to sort a list in Python?
Answer:
Use the sorted method.
list = [3, 2, 1]
print sorted(list)
It will give:
[1, 2, 3]
What is "The Zen of Python"?
Answer:
Try the following command in shell:
# python -c "import this"
And it will print out...
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
How to debug in Python?
Answer:
Assume you have a simple python script like:
a = 1
b = 2
c = a + b
print c
To enable debugging, add the following line at the top of the program
import pdb
and in the line you want to break, add
pdb.set_trace()
So the whole program become:
import pdb
a = 1
b = 2
pdb.set_trace()
c = a + b
print c
When you execute the script by python test.py, you will in the debug mode.
Some useful commands:
1. Print variables: p a
2. Step over: n
3. Continue: c
4. Quit: q
Quick and easy SMTP server with Python
Answer:
You can start a Python SMTP server, with only a single command, without installing any additional software in modern Linux.
E.g.
sudo python -m smtpd -n -c DebuggingServer localhost:25
Convert string to integer in Python
Answer:
You can use the built-in int() type constructor, e.g.
int('123') == 123