Post文章

Python QuickStart

Contents目录

Python Version: 2.7 We use python for rapid development. Mainly using the matrix operations, so we quickly introduce python collection type and control structure.

List

1
2
3
4
>>> wow=[]
>>> wow.append(1)
>>> wow.append('nice hat')
>>> wow

python has arryay data type, which similar to c/cpp/java/…, can contain only one type of data. This array type is faster than list when you are looping.

Dicthinaries

1
2
3
4
>>> wow={}
>>> wow['name']='euryugasaki'
>>> wow[123]=456
>>> wow

Set

1
2
3
4
5
6
7
8
>>> Set1 = [1,2,3,4,5,6,7,8,9]
>>> pSet1 = set(Set1)
>>> pSet1
>>> pSet2 = set([4,5,6,7])
>>> pSet2
>>> pSet1-pSet2
>>> pSet1|pSet2
>>> pSet1&pSet2

if

1
2
3
4
5
>>> val=10
>>> if val<9: print "hehe"
>>> if val<9:
...    print "hehe"
...

for

In for,while,or if statements, we use indentation to tell python which line of code belong inside these loops.

1
2
3
4
>>> pset=set([1,2,3,4,5])
>>> for item in pset:
...    print item
...

You can also loop over a dictionary, The item iterated over are acturally the dictionary keys.

list comprehensions

1
2
3
>>> wow = [1,2,3,4,5,6,7,8,9]
>>> newWOW = [item*3 for item in array if item>5]
>>> newWOW