Python笔记

来自Jack's Lab
(版本间的差异)
跳转到: 导航, 搜索
(Variable)
(Format)
 
(未显示1个用户的17个中间版本)
第9行: 第9行:
 
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
 
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
 
$ sudo python get-pip.py
 
$ sudo python get-pip.py
 +
</source>
 +
 +
 +
Using the pip to install the numpy and tensorflow package:
 +
 +
<source lang=bash>
 +
$ sudo pip install numpy
 +
$ sudo pip install tensorflow
 
</source>
 
</source>
  
第94行: 第102行:
 
=== Indentation ===
 
=== Indentation ===
  
Whitespace at the beginning of the line is important in Python. This is called indentation
+
'''Whitespace at the beginning of the line is important!'''
  
Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.
+
Spaces and tabs at the beginning of the logical line are used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.
  
 
Statements which go together must have the same indentation. Each usch set of statements is called a '''block'''
 
Statements which go together must have the same indentation. Each usch set of statements is called a '''block'''
 
  
 
<br>
 
<br>
 +
 +
=== Format ===
 +
 +
<source lang=python>
 +
#!/usr/bin/python
 +
 +
#width = 10,precise = 3,align = left
 +
print "PI = %10.3f" %(math.pi)
 +
 +
# don't add the '\n' at the end of line
 +
print "PI = %10.3f" %(math.pi),
 +
 +
#width = 10,precise = 3,align = rigth
 +
print "PI = %-10.3f" % math.pi
 +
 +
 +
</source>
 +
 +
Output:
 +
 +
<source lang=bash>
 +
PI = 3.141593
 +
PI =      3.142
 +
</source>
 +
 +
 +
<source lang=bash>
 +
Python 2.7.9 (default, Mar  1 2015, 12:57:24)
 +
>>> print "%.3s " % ("jcodeer") #precise = 3
 +
jco
 +
>>> print "%.*s" % (4,"jcodeer") #precise = 4
 +
jcod
 +
>>> print "%10.3s" % ("jcodeer") #width = 10,precise = 3
 +
      jco
 +
>>>
 +
</source>
 +
 +
 +
 +
<source lang=python>
 +
fmat = "%r %r %r %r"
 +
 +
print fmat % (1, 2, 3, 4)
 +
print fmat % ("one", "two", "three", "four")
 +
print fmat % (True, False, False, True)
 +
</source>
 +
 +
<source lang=bash>
 +
1 2 3 4
 +
'one' 'two' 'three' 'four'
 +
True False False True
 +
</source>
 +
 +
 +
<source lang=python>
 +
print('{0:.3}'.format(0.3333333))
 +
#print('{0:.3}'.format(1/3))
 +
print('{0:_^11}'.format('Hello'))
 +
print('{0:+^11}'.format('Hello'))
 +
print('{name} wrote {book}'.format(name='Jack', book='Hello world'))
 +
</source>
 +
 +
<source lang=bash>
 +
PI = 3.141593
 +
PI =      3.142
 +
 +
0.333
 +
___Hello___
 +
+++Hello+++
 +
Jack wrote Hello world
 +
</source>
 +
 +
<br><br>
  
 
== Operators ==
 
== Operators ==
第120行: 第200行:
  
 
== Exceptions ==
 
== Exceptions ==
 +
 +
=== with...as ===
 +
 +
<source lang=python>
 +
#!/usr/bin/python
 +
 +
import sys
 +
 +
class test:
 +
    def __enter__(self):
 +
        print("enter")
 +
        return 1
 +
    def __exit__(self, *args):
 +
        print("clean up")
 +
        return False
 +
        #return True
 +
 +
 +
with test() as t:
 +
    print("t is not the result of test(), it's __enter__ returned")
 +
    print("t is 1, yes, it's {0}".format(t))
 +
    print "t is 1, yes, it's", t
 +
    raise NameError("Error")
 +
 +
sys.exit()
 +
print("Never here")
 +
</source>
  
 
<br><br>
 
<br><br>

2017年5月25日 (四) 18:37的最后版本

目录

[编辑] 1 Quick Start

Install python2.7 from https://www.python.org/downloads/


Install pip:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ sudo python get-pip.py


Using the pip to install the numpy and tensorflow package:

$ sudo pip install numpy
$ sudo pip install tensorflow



[编辑] 2 Basics

[编辑] 2.1 Numbers

Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
>>> a = 3.23
>>> b = 4
>>> c = 52.3E-4
>>> print a, b, c
3.23 4 0.00523


[编辑] 2.2 Strings

#!/usr/bin/python

str_1 = 'Jack Tan'
str_2 = "Ga Xiao Duo"
str_3 = '''This is a multi-line string. first line.
second line'''

# using the startswith method of the string object
if str_1.startswith('Ja'):
    print("Yes, str_1 start with 'Ja'")

if 'a' in str_1:
    print('Yes, str_1 contains the string "a"')

substr = 'ac'
if str_1.find(substr) != -1:
    print('Yes, str_1 contains the string "{0}"'.format(substr))
    print('Yes, {0} contains the string "{1}"'.format('str_1', substr))
    print('Yes, {} contains the string "{}"'.format('str_1', substr))
    # using the format method of the string object

deli = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']

# using the join method of the string object
print(deli.join(mylist))

Output:

Yes, name start with 'Ja'
Yes, name contains the string "a"
Yes, name contains the string "ac"
Brazil_*_Russia_*_India_*_China

Once your have created a string, you cannot change it



[编辑] 2.3 Variable

Python refers to anything used in a program as an object.

Do not need type declaration of variable in python

Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
>>> a = 3.1415
>>> b = 100000
>>> c = 'Jack Tan'
>>> d = ['x1', 'x2']
>>> print a, b, c, d
3.1415 100000 Jack Tan ['x1', 'x2']
.

Variables can hold values of different types called data types. The basic types are numbers and strings. We can create our own types using classes.


[编辑] 2.4 Indentation

Whitespace at the beginning of the line is important!

Spaces and tabs at the beginning of the logical line are used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.

Statements which go together must have the same indentation. Each usch set of statements is called a block


[编辑] 2.5 Format

#!/usr/bin/python

#width = 10,precise = 3,align = left
print "PI = %10.3f" %(math.pi)

# don't add the '\n' at the end of line
print "PI = %10.3f" %(math.pi),

#width = 10,precise = 3,align = rigth
print "PI = %-10.3f" % math.pi


Output:

PI = 3.141593
PI =      3.142


Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
>>> print "%.3s " % ("jcodeer") #precise = 3
jco 
>>> print "%.*s" % (4,"jcodeer") #precise = 4
jcod
>>> print "%10.3s" % ("jcodeer") #width = 10,precise = 3
       jco
>>>


fmat = "%r %r %r %r"
 
print fmat % (1, 2, 3, 4)
print fmat % ("one", "two", "three", "four")
print fmat % (True, False, False, True)
1 2 3 4
'one' 'two' 'three' 'four'
True False False True


print('{0:.3}'.format(0.3333333))
#print('{0:.3}'.format(1/3))
print('{0:_^11}'.format('Hello'))
print('{0:+^11}'.format('Hello'))
print('{name} wrote {book}'.format(name='Jack', book='Hello world'))
PI = 3.141593
PI =      3.142

0.333
___Hello___
+++Hello+++
Jack wrote Hello world



[编辑] 3 Operators



[编辑] 4 Control Flow



[编辑] 5 Functions



[编辑] 6 Data Structures



[编辑] 7 Exceptions

[编辑] 7.1 with...as

#!/usr/bin/python

import sys

class test:
    def __enter__(self):
        print("enter")
        return 1
    def __exit__(self, *args):
        print("clean up")
        return False
        #return True


with test() as t:
    print("t is not the result of test(), it's __enter__ returned")
    print("t is 1, yes, it's {0}".format(t))
    print "t is 1, yes, it's", t
    raise NameError("Error")

sys.exit()
print("Never here")



[编辑] 8 Modules



[编辑] 9 Object Oriented



[编辑] 10 Resource





































个人工具
名字空间

变换
操作
导航
工具箱