Some easy-to-forget points in the Python Learning Manual (the first three parts)

Source: Internet
Author: User
Tags python script

1.ubuntu allows the Python script to run directly:
test.py file (suffix can be saved)
#!/usr/bin/python
Print (' wwwww ')

sudo chmod +x./test.py (sudo chmod 755./2)
Execute./test.py can
Can be added as a shortcut:
sudo ln-s/home/zh/Desktop/test.py/usr/bin/test
Execute test

2. Remove 0-20, can be divisible by 3 number (list parsing)
>>> [x for x in range (0,20) if x% 3 ==0]
[0, 3, 6, 9, 12, 15, 18]
>>>

3. Run a script
>>> a= "{1:1,2:2, ' 1 ': 22}"
>>> Eval (a)
{' 1 ': 22, 1:1, 2:2}
>>> B=eval (a)
>>> type (b)
<type ' Dict ' >

>>> eval (' list (' qqqqqqqqqqq ') ')
[' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ', ' Q ']

4. Character and number conversion
>>> chr (115)
' s '
>>> ord (' C ')
99

5. String formatting
>>> ' aaaa%s bbbbb%s CCCCC '% ($)
' AAAA 1 bbbbb 2 CCCCC '
>>>
>>> t= ' {}+{}={} '. Format (+/-)
>>> T
' 1+2=3 '
>>>

6. Dictionary and List parsing
>>> Dict ([(2,2)])
{1:1, 2:2}
>>> zip ([1,2,3],[1,2,3])
[(1, 1), (2, 2), (3, 3)]
>>> {a:b*2 for (A, b) in [(1, 1), (2, 2), (3, 3)]}
{1:2, 2:4, 3:6}
>>>

7. Achieve Python3-like printing
>>> from __future__ import print_function
>>> for line in open ('/home/zh/desktop/Notes '):p rint (line, ')

8. Conditional judgment in an assignment statement
>>> c= ' 1111122222333334444455555 '
>>> d=c[:5]+ ' # ' +c[5:] If Len (c) <5 Else (c[:5]+ ' # ' +c[5:10]+ ' # ' +c[10:] If Len (c) ==15 Else (c[:5]+ ' # ' +c[5:10]+ ' # ' +c[10:15]+ ' # ' +c[15:] If Len (c) ==20 Else (c[:5]+ ' # ' +c[5:10]+ ' # ' +c[10:15]+ ' # ' +c[15:20]+ ' # ' +c[20:] If Len (c) ==25 Else 3)))
>>> D
' 11111#22222#33333#44444#55555 '
>>>

9.zip and Map,enumerate
>>> A
[1, 2, 3]
>>> b
[5, 6, 7]
>>> Zip (A, B)
[(1, 5), (2, 6), (3, 7)]
>>> type (Zip (b))
<type ' list ' >
>>> Map (chr,a)
[' \x01 ', ' \x02 ', ' \x03 ']
>>> p=[' 1 ', ' 2 ', ' 3 ']
>>> Enumerate (p)
<enumerate Object at 0x7f93e36688c0>
>>> type (enumerate (p))
<type ' Enumerate ' >
>>> for II in Enumerate (p):p rint (ii)
...
(0, ' 1 ')
(1, ' 2 ')
(2, ' 3 ')
>>> N=enumerate (P)
>>> Next (N)
(0, ' 1 ')
>>>

5. Get Help
DIR (SYS)
Help (SYS)

Some easy-to-forget points in the Python Learning Manual (the first three parts)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.