python从入门到精通-Python从入门到精通
奥运五环的绘制
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从入门到精通相关推荐
- python 二进制流转图片_Python零基础入门到精通-5.1节:Python程序的执行过程
教程引言: 系统地讲解计算机基础知识,Python的基础知识, 高级知识,web开发框架,爬虫开发,数据结构与算法,nginx, 系统架构.一步步地帮助你从入门到就业. 5.1.1 在命令行中执行Py ...
- python基础一入门必备知识-Python从入门到精通要掌握哪些基础知识?
Python从入门到精通要掌握哪些Python基础知识?Python作为一门编程语言,已经发展了近三十年,近几年,随着人工智能时代的来临分不开,python人才已经成为一线互联网企业的青睐的对象,Py ...
- 零基础python从入门到精通 pdf-PYTHON从入门到精通 PDF 下载
相关截图: 资料简介: <Python从入门到精通>从初学者角度出发,通过通俗易懂的语言.丰富多彩的实例,详细介绍了使用Python进行程序开发应该掌握的各方面技术.全书共分22章,包括初 ...
- 自学python要看哪些书籍-Python入门自学到精通需要看哪些书籍?
Python语言在近几年可以算得上如日中天,越来越火爆的同时,学习Python的人也越来越多了.对于不同基础的学习者来讲,学习的重点和方式也许会有差别,但是基础语法永远都是重中之重.在牢牢掌握基础知识 ...
- python入门到精通需要学多久-入门到精通python要多久
对于大多数python学习者来说,入门是相对简单的,但是要做到精通python,并非那么容易!python有很多可以学习的方向,选择一感兴趣的1去学习就好,不必所有方向都掌握! 一:明确自己的学习目标 ...
- python安装目录结构_1.5 python安装目录介绍《Python基础开发入门到精通》
第一章 Python的概述与环境安装 本章所讲内容: 1.1 Python介绍 1.2 Python2与Python3的比较 1.3 Python3的安装 1.4 Python环境变量配置 1.5 P ...
- python从入门到精通-python从入门到精通视频(大全60集)
教程名称:python从入门到精通视频(全60集) 0'1 Python编程语言历史及特性.mp4 02 Python编程语言初接触.mp4 03 Python程序文件结构.mp4 04 准备Pyth ...
- 想精通 Python 数据挖掘?清华博士带你入门!
在我看来,基本上可以负责任地认为,Python 可以做任何事情.无论是从入门级选手到专业级数据挖掘.科学计算.图像处理.人工智能,Python 都可以胜任.或许是因为这种万能属性,周围好更多的小伙伴都 ...
- 视频教程-Python入门精讲视频,从入门到精通-Python
Python入门精讲视频,从入门到精通 10年Linux使用及管理经验,7年IT在线教育培训经验.拥有RHCA高级架构师及Openstack证书.精通Linux.Python.思科.C++.安全渗透等 ...
- micropython视频_零基础如何优雅入门“网红”Python?小白必看的MicroPython视频合集:从入门到精通!...
零基础如何优雅入门"网红"Python?小白必看的MicroPython视频合集:从入门到精通! 若问时下最火的一门编程语言是什么?答案一定是Python. 就连高考都开始考Pyt ...
最新文章
- SQLSERVER中统计所有表的记录数
- mysql中括号_mysql进阶知识点,启动项、系统变量、字符集介绍!
- c#接口和抽象类对比学习
- [转] DataSet的的几种遍历
- 优化SQL步骤——查看SQL执行频率 || 定位低效率执行SQL
- GDCM:把DICOM文件存在vector<char>里面的测试程序
- 《网络空间欺骗:构筑欺骗防御的科学基石》一1.1 主动网络空间防御中网络空间抵赖与欺骗的视图...
- 招商银行信用卡中心华泰证券暑期实习软开笔试小结
- poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)
- python3集合(set)
- 希尔和归并排序的异同
- Linux录音软件audacity安装:sudo yum install audacity
- L1-024. 后天-PAT团体程序设计天梯赛GPLT
- zTree树形中的搜索定位
- access中本年度的四月一日_《四月一日灵异事件簿》一部打工人的励志故事,哈哈哈~(诙谐,温馨,人性,可爱,悬疑,友情,羁绊)...
- 影响你选择职业的,跟个人相关的主要的因素
- 3步解决AS提示:Compilation is not supported for following modules
- mysql 中添加和删除字段
- Char2Wav:End-to-End Speech Synthsis
- 1、Windows如何删除右键新建菜单中的某些选项
热门文章
- TensorFlow精进之路(六):CIFAR-10图像是被(下)
- VS2013 community卸载后不能重装的问题
- Bug(四)——error LNK1112:模块计算机类型x86与目标计算机类型x64冲突
- TensorFlow入门篇(二):线性回归
- C语言之测试程序运行时间
- jQuery中的attr()与prop()设置属性、获取属性的区别
- FatFs源码剖析(转)
- 去除Xcode6创建工程时自带的storyboard
- Bailian2733 判断闰年【入门】(POJ NOI0104-17)
- POJ NOI0105-43 质因数分解