Core Python Programming筆記,Note

來源:互聯網
上載者:User

>>> logfile=open('/tmp/mylog.txt','a')

>>> print >> logfile, 'fatail error: invalid arguments'

>>> logfile.close()

>>>

user=raw_input("please input your name:")

Numbers:

int

long

bool(True,False)

float

complex(6.23+1.5j)

tuple

1.元組,數組(是個線性結構,可以用整數索引)

Lists [] and Tuples (): 

hold any arbitrary number of arbitrary Python objects, the items are ordered

and accessed via index offsets, similar to arrays, except that lists and tuples can store 

different types of objects.

Tuples can be thought of for now as "read-only" lists.

Subsets can be taken with the slice operator ([] and [:]) in the same manner as strings.

Dictionaries

Dictionaries (or "dicts for short) are Python's mapping type and work like associative arrays

or hashes found in perl.

>>> aDict = {'host':'earth'}

>>> aDict

{'host': 'earth'}

>>> aDict['port']=8080

>>> aDict

{'host': 'earth', 'port': 8080}

>>> aDict.keys()

['host', 'port']

>>> for key in aDict:

...     print key, aDict[key]

...

host earth

port 8080

if expression1:

if_suite

elif expression2:

elif_suite

else:

else_suite

>>> while cnt<3:

...     print 'loop %d'%(cnt)

...     cnt+=1

...

loop 0

loop 1

loop 2

>>> for item in range(10):

...     print item

>>> for item in ['e-mail','net-surfing','homework']:

...     print item

...

e-mail

net-surfing

homework

List Comprehensions:

>>> squared = [x**2 for x in range(4)]

>>> squared

[0, 1, 4, 9]

>>> for i in [x**2 for x in range(6)]:

...     print i

...

0

1

4

9

16

25

Useful Built-In Functions for New Python Programmers

dir([obj]),help([obj]),int(obj),len(obj),open(fn,mode),range([start,]stop[,step]),

rraw_input(str),str(obj),type(obj)

Typical Python file structure

#!/usr/bin/env python(1)Startup line (Unix)"this is a test module"(2)Module documentationimport sysimport os(3)Module importsdebug = True(4)(Global) Variable declarationsclass FooClass (object):"Foo class"pass(5)Class declarations (if any)def test():"test function"foo = FooClass()if debug:print 'run test()'(6)Funcation declarations(if any)if __name__ == '__main__':test();

  

P 115 / 1155

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.