《 Python 学习手册 》读书笔记(1)
2015-12-20 22:56
891 查看
关于运行程序
交互提示模式下编写代码
terminal中直接输入python开启
通过导入模块,运行文件中的语句
import
exec(open('module.py').read())
UNIX可执行脚本
在脚本第一行写“#!/usr/bin/env python”; shell命令:sudo chmod +x *.py
IDLE
对象类型简介
对于一对象X,通过dir(X)查看其属性,要查看具体某一属性,通过help(X.property)函数
“不可变”对象
“可变”对象
用户定义的类(包含自定义的“属性”和“行为”)
交互提示模式下编写代码
terminal中直接输入python开启
通过导入模块,运行文件中的语句
import
exec(open('module.py').read())
UNIX可执行脚本
在脚本第一行写“#!/usr/bin/env python”; shell命令:sudo chmod +x *.py
IDLE
对象类型简介
对于一对象X,通过dir(X)查看其属性,要查看具体某一属性,通过help(X.property)函数
“不可变”对象
数字(整数,浮点数,复数) 在内存足够的情况下可以达到任意大小和精度 import math math.pi math.sqrt() import random random.random() #生成0~1之间的数 random.choice([1, 2, 3, 4]) 字符串(可用'',"",""" """)
S = 'Spam' len(S) S[1] 分片:S[1:2], S[:1], S[0:-1], S[:] find, replace, upper, rstrip(去掉空格或特定字符) 元组 T = (1, 2, 3, 4) len(T) T + (5, 6) T[0] T.index(4) #where 4 appears T.count(4)
“可变”对象
列表 L = [123, 'spam', 1.23] L.append('NI') L.pop(2) #delete the 2th object M.sort() M.reserve() 通过嵌套可表示矩阵 *列表解析 col2 = [row[1] for row in M] 字典:一种映射操作 D = {'foor': 'Spam', 'quantity': 4, 'color': 'pink'} D['food'] #return 'Spam' D['quantity'] += 1 ------------------------------------------------------ D = {} D['name'] = 'Bob' D['job'] = 'dev' 字典也可嵌套 append进行扩充 Ks = list(D.keys()) #unordered keys list Ks.sort() for key in sorted(D): #iterate though sorted keys 文件 f = open('data.txt', 'w') f.write('Hello\n') f.write('world\n') f.close() ------------------------------ f = open('data.txt') text = f.read() text.split() 集合 X = set('sapm') # X = {'s', 'a', 'p', 'm'} Y = {'h', 'a', 'm'} X & Y X | Y X - Y
用户定义的类(包含自定义的“属性”和“行为”)
例: class Worker: def_init_(self, name, pay): self.name = name self.pay = pay def lastName(self): return self.name.split()[-1] def giveRaise(self, percent): self.pay *= (1.0 + percent)
相关文章推荐
- python核心编程-定制类
- Python基础----数据变量和变量
- Learning Python ~Part1: Getting Started ~心得整理
- python websocket学习使用
- Python入门-----编辑器
- xrange vs range python
- python学习笔记(一)
- leetcode Combination Sum II python
- leetcode Combination Sum python
- Tmux的使用
- python文档字符串
- python numpy array 的一些问题
- python函数的使用和返回值
- Working with Binary Data in Python
- java和python改变全局变量的对比
- python windows下的安装配置以及django的学习经验
- SecureCRT中python脚本编写
- Python爬虫爬取百度贴吧多线程版
- window7下配置python2.7+tornado3.3开发环境
- Python创建中文目录