奥运五环的绘制

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

python从入门到精通书-Python从入门到精通相关推荐

  1. python北京理工大学推荐的书-Python语言程序设计

    Q1:Python语言.C语言.Java语言.VB语言--到底哪种适合作为入门编程语言呢? A1: Python是最好的程序设计入门语言.也是最先进的程序设计语言. 如果只想学一门程序设计语言,请学P ...

  2. python模拟登录爬虫 简书_python爬虫入门之模拟登陆新浪微博

    很多网页浏览都需要首先登陆,比如说新浪微博.当然,这里有一个小技巧,用手机3G版登陆.电脑版会有各种加密,动态加载什么的.我们就耍一下小流氓,柿子拣软的捏么. 浏览器保持登陆是利用我们登陆时收到的co ...

  3. python北京理工大学推荐的书-Python教程书籍(北理工第2版)思考练习-第三章

    题-3.12 题目:一年365天,初始水平值为1.0,每工作一天水平增加N,不工作时水平不下降,一周连续工作4天,计算年终值: N = 0.001.0.002.0.003--0.010 #思考与练习 ...

  4. php 到精通 书,PHP从入门到精通——读书笔记(第20章:Zend Framwork框架)

    第二十章:Zend Framwork 框架 1:概述 2:Zend Framwork 环境搭建1)环境配置:使用ZF框架进行项目开发,首先需要对PHP运行环境进行配置,从而使整个运行环境能够支持ZF的 ...

  5. 〖Python零基础入门篇(58)〗- Python中的虚拟环境

    订阅 Python全栈白宝书-零基础入门篇 可报销!白嫖入口-请点击我.推荐他人订阅,可获取扣除平台费用后的35%收益,文末名片加V! 说明:该文属于 Python全栈白宝书专栏,免费阶段订阅数量43 ...

  6. myrio与fpga编程_myRIO入门实验指导书

    文件名大小更新时间 myRIO入门实验指导书\LabVIEW Codes\Exercise\7-seg display\AI to LED Converter.vi255552015-04-16 my ...

  7. python编程从入门到精通pdf-Python编程从入门到精通.pdf

    作 者 :叶维忠 出版发行 : 北京:人民邮电出版社 , 2018.11 ISBN号 :978-7-115-47880-1 页 数 : 429 原书定价 : 79.00 主题词 : 软件工具-程序设计 ...

  8. 自学python入门-学python入门看什么书

    python语言是最近几年流畅起来的编程语言,因其应用范围广,支持跨平台操作,使得python越来越受欢迎,学习python的人也越来越多.python学习网,大量的免费python视频教程,欢迎在线 ...

  9. python入门到精通需要学多久-入门到精通python要多久

    对于大多数python学习者来说,入门是相对简单的,但是要做到精通python,并非那么容易!python有很多可以学习的方向,选择一感兴趣的1去学习就好,不必所有方向都掌握! 一:明确自己的学习目标 ...

  10. python推荐入门书籍-学python入门看什么书

    python语言是最近几年流畅起来的编程语言,因其应用范围广,支持跨平台操作,使得python越来越受欢迎,学习python的人也越来越多.python学习网,大量的免费python视频教程,欢迎在线 ...

最新文章

  1. 照抄不翻车:抗住千万流量的大型分布式系统架构设计
  2. python分割数字_python实现整数拆分,输出拆分序列
  3. 如何用express+node+ejs 搭建一个简单的页面
  4. 实践2.4 ELF文件格式分析
  5. java任何表达式都可以当作语句_在Java语言中语句用分号终止,并不是所有的表达式都可以构成语句...
  6. GDIPlus灰度化图像
  7. 使用SQL:2003 MERGE语句的奥术魔术
  8. oracle+trace参数设置,Oracle autotrace参数详解
  9. 弹出无边框网页的Javscrpt代码
  10. HDU 1176 免费馅饼(记忆化搜索)
  11. nginx编译包含perl模块
  12. DolphinPHP(海豚PHP)实战教程
  13. 如何把微信和支付宝的收款二维码合成一个?
  14. 计算机网络--基站 NFC 蓝牙 RFID ETC 云计算 云桌面
  15. html华文行楷英文,HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)...
  16. [AHK]获取通达信软件上的股票代码
  17. (转)iOS 上的相机捕捉
  18. MyBatis Plus 看这篇就够了,一发便入魂!
  19. 最受DBA欢迎的数据库技术文档-巡检篇
  20. AutoSAR系列讲解 - 交流专区

热门文章

  1. getprivateprofilestring读不到数据_从零到千万用户,我是如何一步步优化MySQL数据库的?...
  2. Java多线程闲聊(五):AQS
  3. (转载)解决umount: /home: device is busy
  4. 深入解析Dropout——基本思想:以概率P舍弃部分神经元,其它神经元以概率q=1-p被保留,舍去的神经元的输出都被设置为零...
  5. Xshell高级后门完整分析报告
  6. Centos ab测试工具
  7. iPhone XS MAX全球首碎!一看官方维修价 网友:修不起!
  8. 表操作,数据操作,单表查询,python操作数据库
  9. 基于tiny4412的Linux内核移植 -- eMMC驱动移植(六)
  10. Keil 二进制数输入宏