国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python2.5/2.6實用教程 入門基礎篇

2020-02-23 04:44:50
字體:
來源:轉載
供稿:網友
起步走
代碼如下:
#! /usr/bin/python

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

知識點

Python是動態語言,變量不須預先聲明.
打印語句采用C風格
字符串和數字
但有趣的是,在javascript里我們會理想當然的將字符串和數字連接,因為是動態語言嘛.但在Python里有點詭異,如下:
代碼如下:
#! /usr/bin/python

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


運行這行程序會出錯,提示你字符串和數字不能連接,于是只好用內置函數進行轉換
代碼如下:
#! /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函數將字符串和數字進行轉換
打印以#開頭,而不是習慣的//
打印多個參數的方式
國際化
寫膩了英文注釋,我們要用中文!


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

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

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

#! /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

知識點:

列表長度是動態的,可任意添加刪除元素.
用索引可以很方便訪問元素,甚至返回一個子列表
更多方法請參考Python的文檔
字典
代碼如下:
#! /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]

知識點:

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

代碼如下:
word="abcdefg"
a=word[2]
print "a is: "+a
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅江市| 当雄县| 溆浦县| 东乡县| 柏乡县| 贵定县| 墨玉县| 嫩江县| 兴安县| 琼中| 拜泉县| 自贡市| 青龙| 台安县| 和林格尔县| 福泉市| 石嘴山市| 林芝县| 雅江县| 南郑县| 成武县| 瑞昌市| 贵南县| 乳山市| 西贡区| 靖西县| 陆河县| 托里县| 于都县| 弥渡县| 河北省| 洪泽县| 义乌市| 辉县市| 彭泽县| 中卫市| 彩票| 萍乡市| 太谷县| 安龙县| 延庆县|