在本文中,我们重要介绍类课程的内容,自我感觉有个不错的建议和大家分享下
《Python for Beginners》为LearnStreet上的Python入门课程。
本节重要习学内容分为两部分:
1. Scoping and Mutability(量变作用域与可变性)
2. Classes and Objects(类和对象)
Lesson 8 Scoping and Mutability
1. Global and Local Variables
全局量变和局部量变
1 >>> name = "Gladys"
2 >>> other_name = "Mario the plumber"
3 >>> defrun():4 name = "GLaDOS"
5 #write your code here
6 returnname, other_name7
8 >>> printrun()9 ('GLaDOS', 'Mario the plumber')
总结:Python中全局量变和局部量变的使用方法与C语言基本同相。
Lesson 9 Classes and Objects
1. Overview
In Python, everything we work with is an object.
self is the variable that the instance of the class is assigned to, so when initializing an instance, don't include self as a parameter.
2. Creating a class
创立一个类
每日一道理
流逝的日子像一片片凋零的枯叶与花瓣,渐去渐远的是青春的纯情与浪漫。不记得曾有多少雨飘在胸前风响在耳畔,只知道沧桑早已漫进了我的心爬上了我的脸。当一个人与追求同行,便坎坷是伴,磨难也是伴。
1 classBottle:2
3 def __init__(self, liquid):4 self.amount= 500
5 self.height = 10
6 self.empty =False7 self.liquid =liquid8
9 defamount_left(self):10 printself.amount11
12 defdrink(self, amount):13 if amount <14 self.amount elif amount="=self.amount:16" self.empty="True18" else:19 print drink more self.liquid there is>
20
21 defrefill(self, amount):22 if amount + self.amount > 500:23 print "Cannot add that much" + self.liquid + "to the bottle!"
24 else:25 self.amount +=amount26 self.empty = False
总结:初始化一个类的例实时,__init__(self, arg)数函会动自执行,相似C++中的构造数函。
至此,《Python for Beginners》网络课程经已习学结束,并获得了一枚结课勋章,其图片如下所示。该课程简练易懂,配合线在编程习练,够能实时检测习学成果,是Python入门者不错的择选。
文章结束给大家分享下程序员的一些笑话语录: 雅虎最擅长的不是开通新业务,是关闭旧业务。
14>