奥运五环的绘制
import turtle
turtle.width(10)
turtle.color('blue')
turtle.circle(50)
turtle.penup()
turtle.goto(120,0)
turtle.pendown()
turtle.color('black')
turtle.circle(50)
turtle.penup()
turtle.goto(240,0)
turtle.pendown()
turtle.color('red')
turtle.circle(50)
turtle.penup()
turtle.goto(60,-50)
turtle.pendown()
turtle.color('yellow')
turtle.circle(50)
turtle.penup()
turtle.goto(180,-50)
turtle.pendown()
turtle.color('green')
turtle.circle(50)
值的互换
a,b = 1,2
a,b = b,a
print(a,b)
divmod:获得商和余数
(a,b)=divmod(10,3)
print(a,b)
python采用Unicode编码,16位
print(ord('A'))
print(chr(65))
键盘输入数据
name = input('键盘输入数据。。。')
print(name)
+=和append的运行效率比较
import time
t1 = time.time()
a = ''
for i in range(1000000):
a+='tres'
t2 = time.time()
print(t2-t1)
t1 = time.time()
a = []
for i in range(1000000):
a.append('tes')
b = ''.join(a)
t2 = time.time()
print(t2-t1)
image.png
image.png
image.png
image.png
image.png
image.png
image.png
format 的使用
a = '我是:{name} 年级:{age}'
print(a.format(name='cw',age=60))
同心圆的绘制
import turtle
color_circle = ('red','green','blue')
turtle.width(5)
for i in range(1,10):
turtle.color(color_circle[i%len(color_circle)])
turtle.circle(10*i)
turtle.penup()
turtle.goto(0,-10*i)
turtle.pendown()
函数的使用:
打印注释:
def compare(a,b):
'''
比较两个数的大小
:param a:
:param b:
:return:
'''
if a > b:
print('{0}比较大'.format(a))
else:
print('{0}比较大'.format(b))
#打印注释
print(compare.__doc__)
compare(1,2)
compare(3,2)
print(id(compare))
print(type(compare))
函数也可以传递:
def compare(a,b):
'''
比较两个数的大小
:param a:
:param b:
:return:
'''
if a > b:
print('{0}比较大'.format(a))
else:
print('{0}比较大'.format(b))
c = compare
c(1,2)
global的使用
a = 100
def change():
global a
a = 50
print(a)
change()
print(a)
可变对象引用传递
b = [1,2,3]
def f1(m):
m.append(30)
f1(b)
print(b)
结果
[1, 2, 3, 30]
函数的可变参数
*元组
**字典
def f1(a,b,*c):
print(a,b,c)
f1(1,2,3,4,5)
def f2(a,b,**c):
print(a,b,c)
f2(1,2,name='chini',age=18)
结果:
1 2 (3, 4, 5)
1 2 {'name': 'chini', 'age': 18}
lambda表达式和匿名函数:
f = lambda a,b,c:a+b+c
print(f(1,2,3))
eval 的使用
eval('print("CHINI")')
递归打印斐波那契数列
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
print(factorial(5))
nonlocal的使用
def outer():
b = 10
def inner():
nonlocal b
print('b:',b)
b =20
inner()
print('b:',b)
outer()
面向对象
helloclass
class Student:
def __init__(self,name,score):
self.name = name
self.score = score
def say_acore(self):
print('{0} score {1}'.format(self.name,self.score))
s = Student('chini',100)
s.say_acore()
类方法
class Student:
company = 'apple'
count = 0
def __init__(self,name,score):
self.name = name
self.score = score
Student.count += 1
def say_acore(self):
print('{0} score {1}'.format(self.name,self.score))
s = Student('chini',100)
s.say_acore()
s2 = Student('chini2',99)
s.company = 'facebook'
print(Student.company)
print(Student.count)
print(s.count)
print(s2.count)
静态方法:不需用传递任何参数
class Student:
name = 'chen'
age = 15
def __init__(self,name,age):
self.name = name
self.age = age
@staticmethod
def add(a,b):
c = a+b
print(c)
print()
s = Student('chini',19)
s.add(1,2)
Student.add(3,4)
python 垃圾回收机制
析构函数del 一般不需用重写
class Test:
def __del__(self):
print('{0}已经销毁'.format(self))
t1 = Test()
t2 = Test()
del(t1)
print('程序结束')
运行结果
image.png
call方法和可调用函数
对象名加()可以直接调用call方法
class SalaryAccount:
def __call__(self, salary):
yearSalary = salary*12
monthSalary = salary
daySalary = salary//30
return dict(year=yearSalary,month=monthSalary,day=daySalary)
s = SalaryAccount()
t = s(1000)
print(t.get('year'))
方法的动态性
class Person:
def work(self):
print('work hard')
def say(self,s):
print('{}在说话'.format(s))
#添加方法
Person.sayth = say
p = Person()
p.work()
p.sayth('chini')
#修改方法
def work2(self):
print('work very hard')
Person.work = work2
p.work()
私有变量和私有方法
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
def __say(self):
print('say something')
print(self.__age)
p = Person('chini',18)
print(p.name)
#访问私有变量方式
print(p._Person__age)
p._Person__say()
property的使用:类似于java的setter和getter
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
@property
def ages(self):
return self.__age
@ages.setter
def ages(self,age):
self.__age = age
p = Person('chini',18)
print(p.ages)
p.ages = 100
print(p.ages)
继承:python支持多继承
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
class Stu(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)
self.score = score
s = Stu('chini',18,100)
print(s.name)
print(s.score)
#访问父类的私有变量
print(s._Person__age)
super调用父类的方法
class A:
def say(self):
print('a: i am saying')
class B(A):
def say(self):
A.say(self)
super().say()
print('B: i am saying')
b = B()
b.say()
image.png
多态
class Men:
def eat(self):
print('men eating')
class Chinese(Men):
def eat(self):
print('CHinese eating')
class USA(Men):
def eat(self):
print('USA eating')
def sayEat(m):
if isinstance(m,Men):
m.eat()
else:
print('not men')
sayEat(Chinese())
sayEat(USA())
sayEat(Men())
image.png