Changkun's Blog

Science and art, life in between.


  • Home

  • Ideas

  • Archives

  • Tags

  • Bio

Python QuickStart

Published at: 2014-03-27   |   Reading: 200 words ~1min   |   PV/UV: /

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
#Python#
  • 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.
论文都是逼出来的
谈谈对编程语言的一般性认识
  • TOC
  • Overview
Changkun Ou

Changkun Ou

Stop Talking. Just Coding.

276 Blogs
165 Tags
Homepage GitHub Email YouTube Twitter Zhihu
Friends
    Frimin ZZZero march1993 qcrao maiyang Xargin Muniao
© 2008 - 2024 Changkun Ou. All rights reserved. | PV/UV: /
0%