Python基础笔记
2016-05-20 21:19
411 查看
最近要用到一些Python的东西,开始学习一些基础东西,做以记录。
元组类型
字典:
get()语法:
dict.get(key, default=None)
参数:
下面是详细参数:
key:key在字典中查找。
default:在key不存在的情况下返回值None。也可以指定默认值
字符串:
Python中函数可以返回多个值
相当于返回一个元组
一个模块所在文件夹中必须有一个命名
格式为:from 文件夹.文件 import 要导入的函数或者类名
当需要返回上一级目录时,在文件夹名前面加一个点“.”。
Python特点总结
1、每个语句占一行,结尾没有‘;’, 2、# 是行注释 3、逻辑层次用缩进来区分,每个层次4个空格 4、没有‘{}’,一个语句块实在第一行末尾加一个‘:’ 函数、类的定义,for,if,while语句块等 总的来说Python对格式要求十分严格
#下面定义了一个函数 def printSomething(): """2 的3次幂""" a = 2 **3 a = 3; b=2; #print a>b; """强制取整""" #print 7.0 //2; #函数调用 printSomething()
Python数据类型
列表类型:#定义一个列表 a = [1,4,3,2,1] #在列表末尾添加一个元素 a.append(5) for item in a: print item """截取列表""" print a[0:3] del语句 #删除list中的指定元素: b = [1, 2, 3, 'fss'] del b[0]
元组类型
#元组不能增加或者删除元素,一经创建就不能改变 (1,"123",45) (2)#这不是元组,依旧只是常量2
字典:
dict={ "123":123, 456:"abc" } a={"123":123, 456:"abc"} a["123"] == 123 a.keys()=["123",456] a.values()=[123,"abc"]
get()语法:
dict.get(key, default=None)
参数:
下面是详细参数:
key:key在字典中查找。
default:在key不存在的情况下返回值None。也可以指定默认值
字符串:
#以下四个字符串结果相同 a="hello python" a="hello%s"%"python" a="%s%s"%("hello","python") a="%(verb)s%(name)s"%{"verb":"hello", "name":"python"}
Python中函数可以返回多个值
相当于返回一个元组
def returnSth(a,b,c=4): return a,b-c n1,n2=returnSth(2,9) print n1,n2
Python模块
每一个模块也就意味着是一个包,要用到这个模块里的内容就要先导入;一个模块所在文件夹中必须有一个命名
__init__.py的文件,文件内容可以为空,但不许存在。
格式为:from 文件夹.文件 import 要导入的函数或者类名
当需要返回上一级目录时,在文件夹名前面加一个点“.”。
Python面向对象
以一个实例说明类的定义:#python2中需要object,,而3中是不需要的 class student(object): sex = "man" #这个函数名字是固定的,且必须存在,相当于有参构造函数, #第一个参数必须是self(指向其本身),后面是属性 def __init__(self,stuName,age): self.stuName = stuName self.age = age #类中的方法第一个参数必须都是self def growUp(self): self.age += 1 def printInfo(self): self.sex="famle" print("%s is a %s, and %s years old"%(self.stuName, self.sex, self.age)) #类的继承;()中是所继承的父类的名称 class monitor(student): def __init__(self,stuName,age,career): #super()中的参数只在Python2中存在 super(monitor,self).__init__(stuName,age) self.career = career def printCareer(self): print("He/She is%s"%self.career) #其他文件使用时: from stuClass.students import student from stuClass.students import monitor stu = student("张士诚",123) stu.growUp() stu.printInfo() mon = monitor("李四光",32,"scientist") mon.printInfo() mon.printCareer()
Python文件操作
#第二个参数有三种,r(read)/w(writ,并且文件清空后重新写入)/a(append,直接在打开的文件后面添加) f= file(src,'a')#也可用open函数,用法相同 f.write(content) f.close() #使用with时 不再需要手动关闭文件流 #没有第二个参数时,默认为读取 with open(src) as f1: while True: line = f1.readline() if len(line)== 0: break; #不想print之后换行就在最后加一个‘,’ print line,
相关文章推荐
- 熟悉python中的tkinter图形包--(写一个参数计算器)
- 感知机学习算法初步1
- Python资料锦集
- Python中pydoc命令在windows下不能使用的解决办法
- Python os.popen()方法的用法
- python
- Python 进阶 —— 迭代器与生成器
- VMware workstations 12里配置Ubuntu 12.04及python环境
- Python 扫面文件夹中的文件
- 【Python自动化运维之路Day2】
- leetcode(11), Number of 1 Bits(python)
- TexturePacker批处理python
- selenium+python笔记11
- Python - Django分布式Celery使用
- 【 Python 】函数的参数
- python生成html报告
- python 自定义 包 模块 打包 安装
- Python学习小记2
- python paramiko模块实现sftp上传下载文件
- Searching in baidu using BeautifulSoup in Python3.3