【C010】Python - 基础教程学习(一)
生活随笔
收集整理的這篇文章主要介紹了
【C010】Python - 基础教程学习(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?第一章:基礎知識
>>> 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+5 15>>> 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
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:2011 Month(1-12):11 Day(1-31):11 November 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.com Domain 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 = 80 text_width = len(sentence) box_width = text_width + 6 left_margin = (screen_width - box_width) // 2print print ' ' * 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 permissions True >>> 'x' in permissions False >>> users = ['mlh','foo','bar'] >>> raw_input('Enter your user name:') in users Enter your user name:mlh True >>> subject = '$$Get rich now!!!$$$' >>> '$$$' in subject True2-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: smith PIN code: 7524 Access granted內建函數
?
?
>>> numbers = [100,34,679] >>> len(numbers) 3 >>> max(numbers) 679 >>> min(numbers) 34 >>> max(2,3) 3 >>> min(8,3,4,2) 2list函數
?
?
>>> 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]) 1extend
>>> 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') 4insert
?
>>> 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) >>> 43 43 >>> 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)總結
以上是生活随笔為你收集整理的【C010】Python - 基础教程学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat,JVM内存设置
- 下一篇: java编程点滴(3)--ubuntu下