【C010】Python – 基礎教程學習(一)

來源:互聯網
上載者:User

 第一章:基礎知識

>>> from __future__ import division    #實現斜杠/為除,而不是整除>>> 1 // 2    #實現整除>>> 2 ** 3    #實現指數>>> pow(2,3)    #實現指數函數>>> 0xAF    #十六進位>>> 010     #八進位>>> x = input("x:")    #輸入的時候要帶著格式,字串要加引號>>> x = raw_input("y:")    #輸入的時候不用加格式,直接轉換為字串>>> import math    #匯入模組>>> math.floor(342.34)342.0 >>> from math import sqrt    #下面不用再加math>>> sqrt(9)3.0>>> import cmath    #複數>>> cmath.sqrt(-1)1j>>> print "Hello"    #單引號和雙引號可以交叉使用,都可以用Hello>>> print 'Hello'Hello>>> print "Let's go!"    #字串有單,就用雙Let's go!>>> print '"Hello!"'    #字串有雙,就用單"Hello!">>> print 'Let\'s go'    #逸出字元Let's go>>> 1+2+3\    #反斜線可以作為分行符號       +4+515>>> print 'Hello,\nworld'    #\n作為分行符號Hello,world>>> print r'C:\nowhere'     #前面加個r就實現原始字元,不再考慮逸出字元了C:\nowhere>>> print r'C:\Program Files\fnord'C:\Program Files\fnord

本章的新函數

 第二章:列表和元組

參考學習內容:http://blog.sina.com.cn/s/blog_4b5039210100e9yd.html

>>> name = 'Alex'>>> name[0]    #第一個'A'>>> name[-1]    #從最後一個倒回去'x'>>> 'Hello'[1]    #直接用'e'>>> fourth = raw_input('Year:')[3]Year:2005>>> fourth'5'

2-1

 

months = ['January','Feburuary','March','April','May','June','July','August','September','October','November','December']endings=['st','nd','rd'] + 17*['th']\ +['st','nd','rd'] + 7 * ['th']\ +['st']year = raw_input('Year:')month = raw_input('Month(1-12):')day = raw_input('Day(1-31):')month_number = int(month)day_number = int(day)month_name = months[month_number-1]ordinal = day + endings[day_number-1]print month_name + ' ' + ordinal + ', ' + year

運行結果如下(不得不說今天是11.11.11)

 

 

Year:2011Month(1-12):11Day(1-31):11November 11th, 2011

分區(格式關係很大)

 

 

>>> tag = '<a href="http://www.python.org">Python web site</a>'>>> tag[9:30]'http://www.python.org'>>> tag[32:-4]'Python web site'

 

>>> numbers = [1,2,3,4,5,6,7,8,9,10]>>> numbers[3:6][4, 5, 6]>>> numbers[0:1][1]>>> numbers[8:][9, 10]>>> numbers[-3:][8, 9, 10]>>> numbers[:][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> numbers[:3][1, 2, 3]

2-2

# Split up a URL of the form http://www.something.comurl = raw_input('Please enter the URL: ')domain = url[11:-4]print "Domain name: " + domain
Please enter the URL: http://www.baidu.comDomain name: baidu

更大的步長

 

>>> numbers = [1,2,3,4,5,6,7,8,9,10]>>> numbers[0:10:1][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> numbers[0:10:2][1, 3, 5, 7, 9]>>> numbers[0:10:3][1, 4, 7, 10]>>> numbers[0:10:-2][]>>> numbers[::-2][10, 8, 6, 4, 2]>>> numbers[5::-2][6, 4, 2]>>> numbers[:5:-2][10, 8]

序列相加

 

>>> [1,2,3]+[4,5,6][1, 2, 3, 4, 5, 6]>>> a = [1,3,5]>>> b = [2,4,6 ]>>> c = a + b>>> c[1, 3, 5, 2, 4, 6]

乘法

 

>>> 'python'*5'pythonpythonpythonpythonpython'>>> [42]*10[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

初始化一個長度為10的列表

 

 

>>> sequence = [None] * 10>>> sequence[None, None, None, None, None, None, None, None, None, None]

2-3 序列(字串)乘法樣本

sentence = raw_input("Sentence: ")screen_width = 80text_width   = len(sentence)box_width    = text_width + 6left_margin  = (screen_width - box_width) // 2printprint ' ' * left_margin + '+'   + '-' * (box_width-2)  +   '+'print ' ' * left_margin + '|  ' + ' ' * text_width     + '  |'    #注意空格print ' ' * left_margin + '|  ' +       sentence       + '  |'print ' ' * left_margin + '|  ' + ' ' * text_width     + '  |'print ' ' * left_margin + '+'   + '-' * (box_width-2)  +   '+'print

 

成員資格(in)

 

>>> permissions = 'rw'>>> 'w' in permissionsTrue>>> 'x' in permissionsFalse>>> users = ['mlh','foo','bar']>>> raw_input('Enter your user name:') in usersEnter your user name:mlhTrue>>> subject = '$$Get rich now!!!$$$'>>> '$$$' in subjectTrue

2-4 序列成員資格樣本

 

 

database = [    ['albert',  '1234'],    ['dilbert', '4242'],    ['smith',   '7524'],    ['jones',   '9843']]username = raw_input('User name: ')pin = raw_input('PIN code: ')if [username, pin] in database: print 'Access granted'
User name: smithPIN code: 7524Access granted

內建函數

 

 

>>> numbers = [100,34,679]>>> len(numbers)3>>> max(numbers)679>>> min(numbers)34>>> max(2,3)3>>> min(8,3,4,2)2

list函數

 

 

>>> list('hello')['h', 'e', 'l', 'l', 'o']>>> lists = list('hello')>>> lists['h', 'e', 'l', 'l', 'o']
>>> x = [1,1,1]>>> x[1]1>>> x[2] = 2>>> x[1, 1, 2]

刪除元素

 

 

>>> names = ['Alice','Beth','Cecil','Dee-Dee','Earl']>>> del names[2]>>> names['Alice', 'Beth', 'Dee-Dee', 'Earl']

分區賦值、插入新的元素

 

>>> name = list('Perl')>>> name['P', 'e', 'r', 'l']>>> name[2:] = list('ar')>>> name['P', 'e', 'a', 'r']>>> name[1:] = list('ython')>>> name['P', 'y', 't', 'h', 'o', 'n']>>> numbers = [1,5]>>> numbers[1:1] = [2,3,4]>>> numbers[1, 2, 3, 4, 5]>>> numbers[1:4] = []>>> numbers[1, 5]

列表方法

append

 

>>> lst = [1,2,3]>>> lst.append(4)>>> lst[1, 2, 3, 4]

count

 

>>> [2,4,3,2,4,5,6,2,6].count(2)3>>> x = [[2,3],[3,4],[2,5]]>>> x.count(1)0>>> x.count([2,3])1

extend

>>> a = [1,2,3]>>> b = [4,5,6]>>> a.extend(b)>>> a[1, 2, 3, 4, 5, 6]

index

 

>>> knights = ['We','are','the','knight','who','say','in']>>> knights.index('who')4

insert

 

>>> numbers = [1,2,3,4,5,6]>>> numbers.insert(3,'four')>>> numbers[1, 2, 3, 'four', 4, 5, 6]>>> numbers[4:4] = ['four']>>> numbers[1, 2, 3, 'four', 'four', 4, 5, 6]>>> 

pop(棧操作)

 

 

>>> x = [1,2,3]>>> x.append(x.pop())>>> x[1, 2, 3]

remove

 

 

>>> x = ['to','be','or','not','to','be']>>> x.remove('be')>>> x['to', 'or', 'not', 'to', 'be']

reversed和list函數

 

 

>>> x = [1,2,3]>>> list(reversed(x))[3, 2, 1]

sort

 

 

>>> x = [4,6,2,3,8,1]>>> x.sort()>>> x[1, 2, 3, 4, 6, 8]

存入副本

 

 

>>> x = [4,6,2,1,7,9]>>> y = x[:]>>> y.sort()>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]

比較

 

 

>>> x = [3,2,5,4,6]>>> y = x>>> y.sort()>>> x[2, 3, 4, 5, 6]>>> y[2, 3, 4, 5, 6]

使用sorted函數

 

 

>>> x = [4,6,2,1,7,9]>>> y = sorted(x)>>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]

進階排序

>>> cmp(42,32)1>>> cmp(99,100)-1>>> numbers = [5,3,9,7]>>> numbers.sort(cmp)>>> numbers[3, 5, 7, 9]>>> x = ['aadkfjl','dkfjl','eori','dkf']>>> x.sort(key = len)>>> x['dkf', 'eori', 'dkfjl', 'aadkfjl']>>> x = [3,4,6,2,5]>>> x.sort(reverse = True)    #大小寫敏感的!!!>>> x[6, 5, 4, 3, 2]

元組:不可變序列

 

 

>>> 1,2,4(1, 2, 4)>>> 4343>>> 34,(34,)>>> (1,2,5)(1, 2, 5)>>> (43,)(43,)

 

tuple函數

 

>>> tuple([1,2,4])(1, 2, 4)>>> tuple('abc')('a', 'b', 'c')>>> tuple((1,2,3))(1, 2, 3)

基本元組操作

 

 

>>> x = 1,2,4>>> x(1, 2, 4)>>> x[1]2>>> x[0:2](1, 2)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.