Python基礎(簡明Python教程)

2021-08-08 17:07:41 字數 3968 閱讀 6254

參考簡明python教程

number = 20

guess = int(raw_input('enter an integer:'))

if guess == number

print

'true'

elif guess < number

print

'higher'

else:

print

'lower'

running = true

while

running:

guess = int(raw_input('enter an integer : '))

if guess == number:

print 'congratulations, you guessed it.'

running = false # this causes the while loop to stop

elif guess < number:

print 'no, it

is a little higher than that'

else:

print 'no, it

is a little lower than that'

else:

print 'the while loop is

over.'

# do anything else you want to do here

print 'done'

for i in

range(1, 5):

print i

else:

print 'the

forloop

is over'

def

sayhello

():print

'hello world!'

# block belonging to the function

sayhello() # call the function

def

func

():global x

print

'x is', x

x = 2

print

'changed local x to', x

x = 50

func()

print

'value of x is', x # x變成2

def

say(message, times = 1):

print message * times

say('hello')

say('world', 5) # 輸出worldworldworldworldworld

tip:有預設值的引數必須放在形參表末尾

def

printmax

(x, y):

'''prints the maximum of two numbers.

the two values must be integers.'''

x = int(x) # convert to integers, if possible

y = int(y)

if x > y:

print x, 'is maximum'

else:

print y, 'is maximum'

printmax(3, 5)

print printmax.__doc__

輸出:

$ python func_doc.py

5 is maximum

prints the maximum of

two numbers.

the two values must be integers.

zoo = ('wolf', 'elephant', 'penguin')

print 'number

of animals in the zoo is', len(zoo)

new_zoo = ('monkey', 'dolphin', zoo)

print 'number

of animals in the new zoo is', len(new_zoo)

print 'all animals in

new zoo are', new_zoo

print 'animals brought from old zoo are', new_zoo[2]

print 'last animal brought from old zoo is', new_zoo[2][2]

輸出:

number of animals in the zoo is

3number of animals in the new zoo is

3all animals in

new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))

animals brought from

old zoo are ('wolf', 'elephant', 'penguin')

last animal brought from

old zoo is penguin

tip:

singleton = (2 , ) 必須在第乙個專案後跟乙個逗號

# 'ab' is short for

'a'ddress'b'ook

ab =

print

"swaroop's address is %s" % ab['swaroop']

# adding a key/value pair

ab['guido'] = '[email protected]'

# deleting a key/value pair

del ab['spammer']

print

'\nthere are %d contacts in the address-book\n' % len(ab)

for name, address in ab.items():

print

'contact %s at %s' % (name, address)

if'guido'

in ab: # or ab.has_key('guido')

print

"\nguido's address is %s" % ab['guido']

if

name.startswith('swa'): # 測試字串是否以給定字串開始

print 'yes, the

string

starts with

"swa"'

if 'a' in

name: # 一部分

print 'yes, it

contains

thestring

"a"'

ifname.find('war') != -1: #找到給定字串在另乙個字串中的位置,-1表示找不到

print 'yes, it

contains

thestring

"war"'

class

person:

pass

# an empty block

p = person()

print p

tip:

python中所有類成員都是公共的,所有方法都是有效的;如果資料成員名稱以 雙下劃線字首 如__privatevar,python的名稱管理體系會有效地把它作為 私有變數

簡明Python基礎教程二

檔案操作 檔案內建方法 open成功執行並返回乙個檔案物件之後,所有對該檔案的後續操作都通過這個 控制代碼 進行,操作包括 輸入 輸出 檔案內移動或者雜項操作。幾個例子 1.read 方法 2.readlines方法 3 write 方法 sys模組通過sys.arv屬性提供了對命令列引數的訪問,命...

簡明Python學習筆記1 基礎

0 python注釋 單行 多行 1 字面常量 字面上的意義 2 數字 int 和 floats 3 字串 or the same 多行字串 or 字串不可變 4 格式化方法 format name,age format方法所做的事情便是將每個引數值替換至格式所在的位置 name or age or...

python 類class基礎簡明筆記

感謝莫煩老師 詳情class calculator 首字母要大寫,冒號不能缺 name good calculator 該行為class的屬性 price 18 def add self,x,y print self.name result x y print result def minus se...