字符串:
myscore = 1000
message = 'I scored %s points'
print(2 * message % (myscore, myscore))
print(10 * 'abc')
列表:
wizard_list = ['spider legs', 'toe of frog', 'eye of newt', 'bat wing', 'slug butter', 'snake dandruff']
print(wizard_list[2])
替换:wizard_list[2] = 'snail tongue'
print(wizard_list[2:5])
增加:wizard_list.append('mandrake')
删除:del wizard_list[3]
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
列表相加:print(list1 + list2)
列表乘法:print(list1 * 5)
元组:
fibs = (0, 1, 2, 3)
print(fibs[3])
元组与列表的主要区别在于元组一旦创建就不能改动
字典(Dictionary):
favorite_sports = {'Ralph Williams': 'Football', 'Michael Tippett': 'Basketball', 'Edward Elgar': 'Baseball', 'Rebecca Clarke': 'Netball', 'Ethel Smyth': 'Badminton', 'Frank Bridge': 'Rugby'}
print(favorite_sports ['Ethel Smyth'])
删除:del favorite_sports ['Ethel Smyth']
替换:favorite_sports ['Ralph Williams'] = 'Ice Hockey'
字典间不能相加
turtle:
import turtle
t = turtle.Pen()
t.forward(50)
t.left(90)
t.reset() 清理画布并把海龟放回原位
t.clear() 只清理屏幕
t.backward(100)
t.up()
t.right(90)
t.down()
t.color(1,0,0) 设置颜色为红
t.color(0,1,0) 设置颜色为绿
t.color(0,0,1) 设置颜色为蓝
t.begin_fill()
t.setheading(0) 让海龟面向制定方向
t.circle(10) 画圆
t.end_fill()
t.color(1,0,0) 设置颜色
if elif else:
if age == 10 or age > 13
print('...')
elif age == 11
print('...')
else
print(...)
age = '10'
convert_age = int(age)
age = 10
convert_age = str(age)
age = 10.5
convert_age = float(age)
循环:
for x in range(0,5)
print('hello')
print(list(range(10,20)))
for i in wizard_list
print(i)
while step < 1000
print(step)
step = step + 1
函数:
def testFunc(myname):
print('hello %s' % myname)
使用模块:
import time
print(time.asctime())
import sys
print (sys.stdin.readline())
类和对象:
继承Animate类
Class Animals(Animate):
def breathe(self):
pass
self: 为了便于调用类或者父类中的其它函数
创建对象 a = Animals()
a.breathe()
初始化对象:
Class Giraffes:
def _init_(self, sport):
self.giraffe_sports = sports
ozwald = Giraffes(100)
print(ozwald.giraffe_sports)
内建函数:
print(abs(-10))
print(bool(0)) 0或者None或者空字符串则返回False
dir函数
显示对一个变量可用的函数
dir(popcorn)
eval函数
eval(10 * 5)
exec函数
exec和eval用法一样 但无返回值, 用于执行小程序
float函数
float('12.23')
int函数
int(123.456)
len函数
len('this is a test string')
当用在字典时,len函数返回字典中的元素个数
max和min函数
print(max(10,300,450,50,90))
print(min(5,4,10,30,22])
range函数
print(list(range(0, 30, 2)))
sum函数
sum(list(range(40, 10, -2)))
打开文件
test_file = open('c:\\test.txt')
text = test_file.read()
print(text)
写入文件
test_file = open('c:\\myfile.txt', 'w')
test_file.write('What is green and loud? A froghorn!')
test_file.close()
使用copy模块来复制:
harry = Animal('hippogriff', 6, 'pink')
harriet = copy.copy(harry)
more_animals = copy.deepcopy(my_animals)
copy不会拷贝对象中的对象, 但deepcopy会深拷贝
Keyword模块记录了所有的关键字
print(keyword.iskeyword('if'))
print(keyword.kwlist)
random:
print(random.randint(1, 100))
list_test = ['a1', 'a2', 'b1', 'b2', 'b3']
print(random.choice(list_test))
洗牌 random.shuffle(list_test)
sys模块:
sys.exit()
sys.stdin.readline()
sys.stdin.readline(13) 读取13个字符
sys.stdout.write("what does a fish say when it swims into a wall?")
print(sys.version) 打印python版本信息
time模块
time.time() 自1970年1月1日以来的秒数
time.asctime() 当前时间
t = (2007, 5, 27, 10 , 30, 48, 6, 0, 0) 代表 年 月 日 时 分 秒 星期几(0代表星期一 1代表星期二) 一年中的第几天
print(time.asctime(t))
time.localtime() 把当前日期作为一个对象返回
year = t[0]
month = t[1]
time.sleep(1) 停止1秒钟
pickle模块:
pickle.dump(game_data, save_file) 将game_data字典存入到save_file文件中
loaded_game_data = pickle.load(load_file) 将文件中内容读取到字典变量里面
tkinter:
def hello()
print 'hello here'
from tkinter import *
tk = TK()
btn = Button(tk, text="click me", command=hello)
btn.pack
具名参数:
def persion(width, height):
...
persion(height=3, width=4)
画布:
tk = Tk()
canvas = Canvas(tk, width=600, height=600)
canvas.pack()
画线:
canvas.create_line(0, 0, 500, 500, fill='red')
画盒子:
canvas.create_rectangle(0, 0, 100, 100, fill='yellow')
画圆弧:
canvas.create_arc(100, 0, 200, 200, extent=90, style=ARC)
extent用来指定圆弧的角度
画多边形:
ploygonId = canvas.create_polygon(200, 0, 250, 0, 250, 100, fill="blue", outline="green")
显示文字:
canvas.create_text(100, 175, text='This is only a test', fill="white", font=('Times', 15))
显示图片:
my_image = PhotoImage(file = '/home/joy/work/python/ajax-loader.gif')
element_id = canvas.create_image(0, 300, anchor=NW, image=my_image)
anchor定义作画的起始点从西北方位开始
创建基本动画:
for x in range(0,50):
canvas.move(1, 5, 5)
tk.update()
time.sleep(0.1)
将ID为1的对象横移5个像素(x坐标),纵移5个像素(y坐标)
让对象操作有反应:
def movetriangle(event):
if event.keysym == 'Up':
canvas.move(element_id, 0, -5)
if event.keysym == 'Down':
canvas.move(element_id, 0, 5)
if event.keysym == 'Left':
canvas.move(element_id, -5, 0)
if event.keysym == 'Right':
canvas.move(element_id, 5, 0)
canvas.bind_all('<KeyPress-Up>', movetriangle)
canvas.bind_all('<KeyPress-Down>', movetriangle)
canvas.bind_all('<KeyPress-Left>', movetriangle)
canvas.bind_all('<KeyPress-Right>', movetriangle)
改变对象ploygonId的填充色及线的颜色:
canvas.itemconfig(ploygonId, outline='yellow', fill='green')