最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python2.5/2.6實用教程 入門基礎(chǔ)篇

 更新時間:2009年11月29日 21:17:46   作者:  
本文方便有經(jīng)驗的程序員進入Python世界.本文適用于python2.5/2.6版本.
起步走
復(fù)制代碼 代碼如下:

#! /usr/bin/python

a=2
b=3
c="test"
c=a+b
print "execution result: %i"%c

知識點

Python是動態(tài)語言,變量不須預(yù)先聲明.
打印語句采用C風(fēng)格
字符串和數(shù)字
但有趣的是,在javascript里我們會理想當(dāng)然的將字符串和數(shù)字連接,因為是動態(tài)語言嘛.但在Python里有點詭異,如下:
復(fù)制代碼 代碼如下:

#! /usr/bin/python

a=2
b="test"
c=a+b


運行這行程序會出錯,提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進行轉(zhuǎn)換
復(fù)制代碼 代碼如下:

#! /usr/bin/python

a=2
b="test"
c=str(a)+b
d="1111"
e=a+int(d)
#How to print multiply values
print "c is %s,e is %i" % (c,e)

知識點:

用int和str函數(shù)將字符串和數(shù)字進行轉(zhuǎn)換
打印以#開頭,而不是習(xí)慣的//
打印多個參數(shù)的方式
國際化
寫膩了英文注釋,我們要用中文!


#! /usr/bin/python
# -*- coding: utf8 -*-

print "上帝重返人間:馬拉多納出任阿根廷國家足球隊主帥."
知識點:

加上字符集即可使用中文
列表
列表類似Javascript的數(shù)組,方便易用
復(fù)制代碼 代碼如下:


#! /usr/bin/python
# -*- coding: utf8 -*-

#定義元組
word=['a','b','c','d','e','f','g']

#如何通過索引訪問元組里的元素
a=word[2]
print "a is: "+a
b=word[1:3]
print "b is: "
print b # index 1 and 2 elements of word.
c=word[:2]
print "c is: "
print c # index 0 and 1 elements of word.
d=word[0:]
print "d is: "
print d # All elements of word.

#元組可以合并
e=word[:2]+word[2:]
print "e is: "
print e # All elements of word.
f=word[-1]
print "f is: "
print f # The last elements of word.
g=word[-4:-2]
print "g is: "
print g # index 3 and 4 elements of word.
h=word[-2:]
print "h is: "
print h # The last two elements.
i=word[:-2]
print "i is: "
print i # Everything except the last two characters
l=len(word)
print "Length of word is: "+ str(l)
print "Adds new element"
word.append('h')
print word

#刪除元素
del word[0]
print word
del word[1:3]
print word

知識點:

列表長度是動態(tài)的,可任意添加刪除元素.
用索引可以很方便訪問元素,甚至返回一個子列表
更多方法請參考Python的文檔
字典
復(fù)制代碼 代碼如下:

#! /usr/bin/python

x={'a':'aaa','b':'bbb','c':12}
print x['a']
print x['b']
print x['c']

for key in x:
print "Key is %s and value is %s",(key,x[key])

keys=x.items();
print keys[0]
keys[0]='ddd'
print keys[0]

知識點:

將他當(dāng)Java的Map來用即可.
字符串
比起C/C++,Python處理字符串的方式實在太讓人感動了.把字符串當(dāng)列表來用吧.

復(fù)制代碼 代碼如下:

word="abcdefg"
a=word[2]
print "a is: "+a
b=word[1:3]
print "b is: "+b # index 1 and 2 elements of word.
c=word[:2]
print "c is: "+c # index 0 and 1 elements of word.
d=word[0:]
print "d is: "+d # All elements of word.
e=word[:2]+word[2:]
print "e is: "+e # All elements of word.
f=word[-1]
print "f is: "+f # The last elements of word.
g=word[-4:-2]
print "g is: "+g # index 3 and 4 elements of word.
h=word[-2:]
print "h is: "+h # The last two elements.
i=word[:-2]
print "i is: "+i # Everything except the last two characters
l=len(word)
print "Length of word is: "+ str(l)

不過要注意Asc和Unicode字符串的區(qū)別:
復(fù)制代碼 代碼如下:

#! /usr/bin/python
# -*- coding: utf8 -*-

s=raw_input("輸入你的中文名,按回車?yán)^續(xù)");
print "你的名字是 : " +s;

l=len(s)
print "你中文名字的長度是:"+str(l);
a=unicode(s,"utf8")
l=len(a)
print "對不起,剛才計算錯誤.我們應(yīng)該用utf8來計算中文字符串的長度, \
你名字的長度應(yīng)該是:"+str(l);

知識點:

用unicode函數(shù)進行轉(zhuǎn)碼
條件和循環(huán)語句
復(fù)制代碼 代碼如下:

#! /usr/bin/python
x=int(raw_input("Please enter an integer:"))
if x<0:
x=0
print "Negative changed to zero"

elif x==0:
print "Zero"

else:
print "More"


# Loops List
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)

知識點:

條件和循環(huán)語句
如何得到控制臺輸入
函數(shù)
復(fù)制代碼 代碼如下:

#! /usr/bin/python
# -*- coding: utf8 -*-

def sum(a,b):
return a+b


func = sum
r = func(5,6)
print r

# 提供默認(rèn)值
def add(a,b=2):
return a+b
r=add(1)
print r
r=add(1,5)
print r

一個好用的函數(shù)
復(fù)制代碼 代碼如下:

#! /usr/bin/python
# -*- coding: utf8 -*-

# The range() function
a =range(5,10)
print a
a = range(-2,-7)
print a
a = range(-7,-2)
print a
a = range(-2,-11,-3) # The 3rd parameter stands for step
print a

知識點:

Python 不用{}來控制程序結(jié)構(gòu),他強迫你用縮進來寫程序,使代碼清晰.
定義函數(shù)方便簡單
方便好用的range函數(shù)
異常處理
復(fù)制代碼 代碼如下:

#! /usr/bin/python
s=raw_input("Input your age:")
if s =="":
raise Exception("Input must no be empty.")

try:
i=int(s)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unknown exception!"
else: # It is useful for code that must be executed if the try clause does not raise an exception
print "You are %d" % i," years old"
finally: # Clean up action
print "Goodbye!"

相關(guān)文章

  • python3使用scrapy生成csv文件代碼示例

    python3使用scrapy生成csv文件代碼示例

    這篇文章主要介紹了python3使用scrapy生成csv文件代碼示例,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • matlab中imadjust函數(shù)的作用及應(yīng)用舉例

    matlab中imadjust函數(shù)的作用及應(yīng)用舉例

    這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應(yīng)用舉例,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • python 實現(xiàn)快速生成連續(xù)、隨機字母列表

    python 實現(xiàn)快速生成連續(xù)、隨機字母列表

    今天小編就為大家分享一篇python 實現(xiàn)快速生成連續(xù)、隨機字母列表,具有很好的價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Pycharm配置PyQt5環(huán)境的教程

    Pycharm配置PyQt5環(huán)境的教程

    這篇文章主要介紹了Pycharm配置PyQt5環(huán)境的教程,本文通過圖文實例詳解給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Python切片操作去除字符串首尾的空格

    Python切片操作去除字符串首尾的空格

    這篇文章主要介紹了Python切片操作去除字符串首尾的空格 的相關(guān)資料,需要的朋友可以參考下
    2019-04-04
  • Python利用PyAutoGUI模塊實現(xiàn)控制鼠標(biāo)鍵盤

    Python利用PyAutoGUI模塊實現(xiàn)控制鼠標(biāo)鍵盤

    PyAutoGUI是一個簡單易用,跨平臺的可以模擬鍵盤鼠標(biāo)進行自動操作的python庫。本文將詳細(xì)講講它是如何實現(xiàn)控制鼠標(biāo)鍵盤的,感興趣的可以了解一下
    2022-06-06
  • Flask配置Cors跨域的實現(xiàn)

    Flask配置Cors跨域的實現(xiàn)

    這篇文章主要介紹了Flask配置Cors跨域的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • PyTorch中的Subset類簡介與應(yīng)用示例代碼

    PyTorch中的Subset類簡介與應(yīng)用示例代碼

    在深度學(xué)習(xí)框架PyTorch中,torch.utils.data.Subset是一個非常有用的類,用于從一個較大的數(shù)據(jù)集中選擇一個子集,本文將介紹Subset的概念、基本用法以及一些實際應(yīng)用示例,感興趣的朋友一起看看吧
    2024-08-08
  • numpy基礎(chǔ)教程之np.linalg

    numpy基礎(chǔ)教程之np.linalg

    這篇文章主要給大家介紹了關(guān)于numpy基礎(chǔ)教程之np.linalg的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • python使用原始套接字發(fā)送二層包(鏈路層幀)的方法

    python使用原始套接字發(fā)送二層包(鏈路層幀)的方法

    今天小編就為大家分享一篇python使用原始套接字發(fā)送二層包(鏈路層幀)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07

最新評論

丹凤县| 襄汾县| 大同市| 新龙县| 凤阳县| 九寨沟县| 邹平县| 宝山区| 玛曲县| 汝城县| 华池县| 新蔡县| 新竹县| 凤城市| 山东省| 桂林市| 沙坪坝区| 黑龙江省| 城口县| 邵东县| 泸溪县| 和平县| 阿坝县| 西充县| 仙桃市| 正安县| 南漳县| 桂林市| 东台市| 栖霞市| 永吉县| 盐源县| 崇州市| 济南市| 翼城县| 贺兰县| 宜春市| 玛曲县| 巢湖市| 青铜峡市| 全州县|