Show toolbar
顯示具有 Python3 標籤的文章。 顯示所有文章
顯示具有 Python3 標籤的文章。 顯示所有文章

2011年11月16日 星期三

Python Class

標題:Python Class 類別範例
Python (main.py):
# -*- coding: utf-8 -*-
class codeboxy:
    b = 2
    def power(self, a):
        ans = a ** self.b #計算次方
        return ans

boxy = codeboxy()
a = 3
print("%d ^ %d = %d"%(a, boxy.b, boxy.power(a)))
範例結果:
說明:
使用Class類別的方式計算整數a的b次方為多少。

Python String

標題:Python3常用字串語法
Python (main.py):
# -*- coding: utf-8 -*-
english = "-*-QQBoxy Say Hello CodeBoxy-*-"
print(english[:3]) #由最前面印到第3個字
print(english[3:-3]) #由第3個字印到倒數第3個字
print(english[-3:]) #由倒數第3個字印到最後
print(english.strip("-*")) #清除字串前後字元
print(english.strip("-*").lower()) #轉成小寫
print(english.strip("-*").upper()) #轉成大寫
print(english.strip("-*").replace("Boxy", "Box", 1)) #取代1次字串
print(english.strip("-*").split(" ")) #陣列切割
print(len(english)) #字串長度
print(english.find("Boxy", 3, 27)) #位置3到位置27字串第一次出現的位置
print(english.count("Boxy", 0, 27)) #位置0到位置27字串出現的次數
範例結果: 說明:
Python3常用字串語法整理。

2011年10月12日 星期三

Python Practice ex2

標題:Python ex2 練習
Python (main.py):
# -*- coding: utf-8 -*-
def ex2():
    name = input("Please enter a character string: ")
    print("The string capitalized is ", end="")
    for i in range(0,len(name)):
        dec = int(ord(name[i]))
        if(dec>=97 and dec<=122):
            newstr = chr(dec-32)
        elif(dec>=65 and dec<=90):
            newstr = chr(dec)
        else:
            newstr = "?"
        print(newstr,end="")
    print("\n")

ex2()
說明:
使用Python3進行基礎輸入輸出大小寫轉換練習。

Python Practice ex1

標題:Python ex1 練習
Python (main.py):
# -*- coding: utf-8 -*-
def ex1():
    name = input("Please enter your name: ")
    for i in range(0,len(name)):
        print(name[i],"ASCII value is",str(ord(name[i])))
ex1()
說明:
使用Python3進行基礎輸入輸出ASCII編碼轉換練習。

2011年8月14日 星期日

Python Calendar

標題:Python月曆
#import sys
#路徑為安裝時的預設路徑,版號可能不同
#sys.path.append('C:\tcc_gnuplot_lite\data\SciTE\Lib')
import calendar
#month(年, 月, 寬度, 高度)
print (calendar.month(2011, 12, 6, 2))
範例結果:


說明:
直接執行於Python interpreter。
使用python內建library迅速寫出月曆。