Python QuickStart - Changkun's Blog
source link: https://changkun.de/blog/posts/python-quickstart/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python QuickStart
Published at:
2014-03-27
|
Reading: 200 words ~1min
|
PV/UV: 6/6
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.
>>> 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
>>> wow={}
>>> wow['name']='euryugasaki'
>>> wow[123]=456
>>> wow
>>> 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
>>> val=10
>>> if val<9: print "hehe"
>>> if val<9:
... print "hehe"
...
In for,while,or if statements, we use indentation to tell python which line of code belong inside these loops.
>>> 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
>>> wow = [1,2,3,4,5,6,7,8,9]
>>> newWOW = [item*3 for item in array if item>5]
>>> newWOW
- Author: Changkun Ou
- Link: https://changkun.de/blog/posts/python-quickstart/
- License: All articles in this blog are licensed under CC BY-NC-ND 4.0 unless stating additionally.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK