python第一课~兔子在欢乐的度假期~
2015-10-03 22:06
561 查看
import sys print(sys.platform) # this is the notation print("Hello World") #user = raw_input('Rnter your name:') user = "who ever that is reading that little passage" print 'Hello! My dear friend,',user if user=="rabbit": help(raw_input) for eachNum in range(1): print 'ha ku la ma tata',eachNum #how to open a file handle = open("spy.py",'r')#r:read; w:write; a:add; b:bit; +:read&write for eachLine in handle: print eachLine handle.close() class RabbitClass(): "the first class" version = 0.1 def __init__(self,nm="Emily"): self.name = nm print "the New class's Name is",nm def showName(self): print "your name is",self.name print "the class name is",self.__class__.__name__ print "the notation:",self.__doc__ r = RabbitClass("rabbitEmily") r.showName()
哇喔,上面一下就来了这么多python语言的句子,下面兔几给大家慢慢说~
ps:上面这段代码除了和文件有关的句子(how to open a file以及下面的5行),输出的内容如下:
darwin Hello World Hello! My dear friend, who ever that is reading that little passage ha ku la ma tata 0 the New class's Name is rabbitEmily your name is rabbitEmily the class name is RabbitClass the notation: the first class
import为引入外部模块~
raw_input语句是python的内建函数,用于收集用户的输入行。为了方便输出,在上文中被#注释掉了~
接下来就是if和for语句,python中并不需要使用大括号,而是使用段落(tab或者4个空格)来表明结构的~干净利落~像个梳了马尾的灵巧丫头~
注意if和for第一行要加:
下面,range(n)函数也是内建函数,可以返回一个从0到n-1的列表
open函数用于打开文件,第一个参数为文件名,使用字符串;第二个参数为r、w、+、a或b:其中,r表示读,w表示写,+表示读写,a表示添加,b表示二进制访问。如果未提供该参数,则默认为r读操作。
最后一部分为类的简单操作:
第一行的字符串称为文档字符串,是一种特别注视:在类或者函数的起始添加一个字符串,起到在线文档的作用~高大上~
之后可以用如下的代码访问:self.__doc__
def __init__(self,nm="Emily"): self.name = nm print "the New class's Name is",nm上述代码为“构造函数“,在新建类的对象时会自动调用,注意是两个_ _,不是一个喔~大家不要写错了~一般用__xxx__这样格式的变量,都是系统定义的名字,下划线对于python有特殊的意义~是内建标识符所使用的符号,所以我们程序媛程序员在命名普通的变量的时候就尽量不要使用下划线开始的命名变量方式了~一般来讲,变量名_xxx被看作是private~当一个变量是private的时候,酱紫用下划线还是很好的习惯的啦~
另外self参数是必须写的,否则会报错~
对了,补充一句,python是解释语言,也就是运行的时候才编译~
好了今天就到这里,大家晚安~
相关文章推荐
- python之Numpy使用
- 【python问题系列--2】脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
- 1.Python深入_对象的属性
- python闭包
- Python核心编程笔记 - 杂记第1章至第8章
- 如何在Windows 下安装Python
- 【Python】二分查找算法
- 八大排序算法的Python实现
- 【Python之旅】第五篇(三):Python Socket多线程并发
- 2.Python深入_上下文管理器
- python学习笔记——自定义模块导入
- 【Python之旅】第五篇(二):Python Socket单线程+阻塞模式
- 【Python之旅】第五篇(二):Python Socket单线程+阻塞模式
- Python学习笔记<list,tuple,range再探>
- Python学习笔记<list,tuple,range再探>
- 【Python之旅】第五篇(一):Python Socket通信原理
- 【Python之旅】第五篇(一):Python Socket通信原理
- Python核心编程笔记 - 第11章 函数式编程
- python搭建web应用框架以及一些基本语法
- 191. Number of 1 Bits Leetcode Python