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

Python3 入門教程 簡單但比較不錯

 更新時間:2009年11月29日 21:22:47   作者:  
Python已經(jīng)是3.1版本了,與時俱進(jìn)更新教程.2.5或2.6請看上一篇文章。
本文適合有Java編程經(jīng)驗的程序員快速熟悉Python
本文程序在windows xp+python3.1a1 測試通過.
本文提到的idle指python shell,即安裝python后你在菜單看到的IDLE(python gui)
在idle里ctrl+n可以打開一個新窗口,輸入源碼后ctrl+s可以保存,f5運(yùn)行程序.
凡打開新窗口即指ctrl+n的操作.
1 你好
復(fù)制代碼 代碼如下:

#打開新窗口,輸入:
#! /usr/bin/python
# -*- coding: utf8 -*-

s1=input("Input your name:")
print("你好,%s" % s1)
'''

知識點(diǎn):
* input("某字符串")函數(shù):顯示"某字符串",并等待用戶輸入.
* print()函數(shù):如何打印.
* 如何應(yīng)用中文
* 如何用多行注釋
'''

2 字符串和數(shù)字
但有趣的是,在javascript里我們會理想當(dāng)然的將字符串和數(shù)字連接,因為是動態(tài)語言嘛.但在Python里有點(diǎn)詭異,如下:
復(fù)制代碼 代碼如下:

#! /usr/bin/python
a=2
b="test"
c=a+b

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

#! /usr/bin/python
#運(yùn)行這行程序會出錯,提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換
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))
'''

知識點(diǎn):
* 用int和str函數(shù)將字符串和數(shù)字進(jìn)行轉(zhuǎn)換
* 打印以#開頭,而不是習(xí)慣的//
* 打印多個參數(shù)的方式
'''

3 列表
復(fù)制代碼 代碼如下:

#! /usr/bin/python
# -*- coding: utf8 -*-
#列表類似Javascript的數(shù)組,方便易用
#定義元組
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)
'''

知識點(diǎn):
* 列表長度是動態(tài)的,可任意添加刪除元素.
* 用索引可以很方便訪問元素,甚至返回一個子列表
* 更多方法請參考Python的文檔
'''

4 字典
復(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]))
'''

知識點(diǎn):
* 將他當(dāng)Java的Map來用即可.
'''

5 字符串
比起C/C++,Python處理字符串的方式實在太讓人感動了.把字符串當(dāng)列表來用吧.
復(fù)制代碼 代碼如下:

#! /usr/bin/python
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))

中文和英文的字符串長度是否一樣?
復(fù)制代碼 代碼如下:

#! /usr/bin/python
# -*- coding: utf8 -*-
s=input("輸入你的中文名,按回車?yán)^續(xù)");
print ("你的名字是 : " +s)
l=len(s)
print ("你中文名字的長度是:"+str(l))

知識點(diǎn):
類似Java,在python3里所有字符串都是unicode,所以長度一致.

6 條件和循環(huán)語句
復(fù)制代碼 代碼如下:

#! /usr/bin/python
#條件和循環(huán)語句
x=int(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))
#知識點(diǎn):
# * 條件和循環(huán)語句
# * 如何得到控制臺輸入

7 函數(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ù)
#! /usr/bin/python
# -*- coding: utf8 -*-
# The range() function
a =range (1,10)
for i in a:
print (i)
a = range(-2,-11,-3) # The 3rd parameter stands for step
for i in a:
print (i)

知識點(diǎn):
Python 不用{}來控制程序結(jié)構(gòu),他強(qiáng)迫你用縮進(jìn)來寫程序,使代碼清晰.
定義函數(shù)方便簡單
方便好用的range函數(shù)

8 異常處理
復(fù)制代碼 代碼如下:

#! /usr/bin/python
s=input("Input your age:")
if s =="":
raise Exception("Input must no be empty.")
try:
i=int(s)
except Exception as err:
print(err)
finally: # Clean up action
print("Goodbye!")

9 文件處理
對比Java,python的文本處理再次讓人感動
復(fù)制代碼 代碼如下:

#! /usr/bin/python
spath="D:/download/baa.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.
f.write("First line 1.\n")
f.writelines("First line 2.")
f.close()
f=open(spath,"r") # Opens file for reading
for line in f:
print("每一行的數(shù)據(jù)是:%s"%line)
f.close()

知識點(diǎn):
open的參數(shù):r表示讀,w寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容.
打開文件之后記得關(guān)閉

10 類和繼承
復(fù)制代碼 代碼如下:

class Base:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
# Child extends Base
class Child(Base):
def plus(self,a,b):
return a+b
oChild =Child()
oChild.add("str1")
print (oChild.data)
print (oChild.plus(2,3))
'''
知識點(diǎn):
* self:類似Java的this參數(shù)
'''

11 包機(jī)制
每一個.py文件稱為一個module,module之間可以互相導(dǎo)入.請參看以下例子:
復(fù)制代碼 代碼如下:

# a.py
def add_func(a,b):
return a+b
# b.py
from a import add_func # Also can be : import a
print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2)) # If using "import a" , then here should be "a.add_func"

module可以定義在包里面.Python定義包的方式稍微有點(diǎn)古怪,假設(shè)我們有一個parent文件夾,該文件夾有一個child子文件夾.child中有一個module a.py . 如何讓Python知道這個文件層次結(jié)構(gòu)?很簡單,每個目錄都放一個名為_init_.py 的文件.該文件內(nèi)容可以為空.這個層次結(jié)構(gòu)如下所示:
parent
--__init_.py
--child
-- __init_.py
--a.py
b.py
那么Python如何找到我們定義的module?在標(biāo)準(zhǔn)包sys中,path屬性記錄了Python的包路徑.你可以將之打印出來:
import sys
print(sys.path)
通常我們可以將module的包路徑放到環(huán)境變量PYTHONPATH中,該環(huán)境變量會自動添加到sys.path屬性.另一種方便的方法是編程中直接指定我們的module路徑到sys.path 中:
復(fù)制代碼 代碼如下:

import sys
import os
sys.path.append(os.getcwd()+'\\parent\\child')
print(sys.path)
from a import add_func

print (sys.path)
print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2))

知識點(diǎn):
如何定義模塊和包
如何將模塊路徑添加到系統(tǒng)路徑,以便python找到它們
如何得到當(dāng)前路徑


12 內(nèi)建幫助手冊
對比C++,Java的突出進(jìn)步是內(nèi)建Javadoc機(jī)制,程序員可以通過閱讀Javadoc了解函數(shù)用法.Python也內(nèi)建了一些方便函數(shù)以便程序員參考.

dir函數(shù): 查看某個類/對象的方法. 如果有某個方法想不起來,請敲dir. 在idle里,試試 dir(list)
help函數(shù): 詳細(xì)的類/對象介紹. 在idle里, 試試 help(list)

相關(guān)文章

  • python 利用for循環(huán) 保存多個圖像或者文件的實例

    python 利用for循環(huán) 保存多個圖像或者文件的實例

    今天小編就為大家分享一篇python 利用for循環(huán) 保存多個圖像或者文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python讀取ini配置的類封裝代碼實例

    python讀取ini配置的類封裝代碼實例

    這篇文章主要介紹了python讀取ini配置的類封裝代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Python中的測試模塊unittest和doctest的使用教程

    Python中的測試模塊unittest和doctest的使用教程

    這篇文章主要介紹了Python中的測試模塊unittest和doctest的使用教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • Python使用OpenCV實現(xiàn)全景拼接功能

    Python使用OpenCV實現(xiàn)全景拼接功能

    全景拼接是將多張圖像拼接成一張全景圖的技術(shù),本文將詳細(xì)介紹如何使用 Python 和 OpenCV 庫進(jìn)行全景拼接,感興趣的小伙伴可以了解一下
    2024-11-11
  • 使用Python批量壓縮tif文件操作步驟

    使用Python批量壓縮tif文件操作步驟

    Tif文件是柵格數(shù)據(jù)最常用的一種格式。圖像數(shù)據(jù)區(qū)以位圖的方式進(jìn)行數(shù)據(jù)的表示。因此Tif文件可以進(jìn)行壓縮,常用的壓縮方式有LZW、RAW、RLE、CCITT等
    2021-09-09
  • 如何利用Python批量處理行、列和單元格詳解

    如何利用Python批量處理行、列和單元格詳解

    因為之后的工作可能會經(jīng)常用到excel,而且也不想荒廢Python,出于為以后做準(zhǔn)備以防不時之需,下面這篇文章主要給大家介紹了關(guān)于如何利用Python批量處理行、列和單元格的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • python numpy中的polyfit函數(shù)用法

    python numpy中的polyfit函數(shù)用法

    這篇文章主要介紹了python numpy中的polyfit函數(shù)用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • python PIL模塊與隨機(jī)生成中文驗證碼

    python PIL模塊與隨機(jī)生成中文驗證碼

    今天我們要學(xué)習(xí)的內(nèi)容是如何利用Python生成一個隨機(jī)的中文驗證碼,并將圖片保存為.jpeg格式,需要的朋友可以參考下
    2016-02-02
  • python將類似json的數(shù)據(jù)存儲到MySQL中的實例

    python將類似json的數(shù)據(jù)存儲到MySQL中的實例

    今天小編就為大家分享一篇python將類似json的數(shù)據(jù)存儲到MySQL中的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python計算圓周率pi的方法

    python計算圓周率pi的方法

    這篇文章主要介紹了python計算圓周率pi的方法,涉及Python針對數(shù)學(xué)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07

最新評論

南木林县| 利津县| 延长县| 浑源县| 稷山县| 南京市| 济宁市| 玉环县| 贵定县| 松溪县| 台山市| 交口县| 泸定县| 夏河县| 东平县| 河津市| 南木林县| 云霄县| 绵阳市| 耿马| 满洲里市| 阿坝| 达日县| 克山县| 五寨县| 阿克陶县| 沧源| 运城市| 右玉县| 宁武县| 手游| 锡林郭勒盟| 永宁县| 象州县| 青浦区| 淳化县| 昂仁县| 攀枝花市| 梓潼县| 同江市| 雷州市|