日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【C010】Python - 基础教程学习(一)

發布時間:2025/7/14 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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

>>> 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: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 True

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: 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) 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) >>> 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 - 基础教程学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。