<PY><core python programming笔记>C2 快速入门
来源:互联网 发布:网络视频广告费用 编辑:程序博客网 时间:2024/05/22 17:02
C2快速入门
程序输出
print 调用str()函数 #带换行
print aline
print line1,line2
交换解释调用 repr()函数
aline
程序输入
raw_input("please enter a string:")
查看帮助
help()
可查函数 模块
按q退出一大堆的help文档
退出控制台
ctrl+d
或者
ctrl+z 回车
或者quit()
或者sys.exit()
注释
# 号
或者
def foo():
"this is a doc string."
return True
操作符
+ - * / // % **(阶乘)
!= > < <= >= ==
and or not
合理使用括号 增强可读性
关键字 变量 函数 都是大小写敏感 #可用ide的自动填词功能来弥补
a="ok"
数字
#动态定义类型 无需声明即可使用 可强制转化 与string间 int(s) str(i)
有符号整型 int (后期版本两种整型都整合了)
长整型 long (注意,比c语言可表示范围多,仅受限虚拟内存数,类java中 BigInteger)
布尔类型 bool :True or False
浮点型 float
复数 complex(直接支持)
十进制浮点 decimal(需导入decimal模块)#对处理金钱等高精度有用
字符串
#string 类型
支持成对的单双三括号
索引操作 [] (第一个字符索引为0,倒数第一个为-1) s[1] s[-2]
切片操作 [:] s[:2] s[1:] s[3:8] s[:-3]
连接操作 + s1+s2
重复操作 * s*4
列表和元组
列表用 []包裹 [1,2,3]
元组用 ()包裹 (1,2,3)
元组可看成只读的列表 除内容外不可更改
操作与字符串类似
字典
用{,,,}包裹
key-value
key 一般是数字或字符串
adict={'host':'earth','destination':'moon'}
adict['host']
代码块及缩进
#if
if expression1:
if_suite
elif expression2:
elif_suite
else:
else_suite
#while
while expression:
while_suite
#for_in alist可以是列表 元组 字符串 range() enumerate()
#range()可用来计数 range(3)相当于[0,1,2]
for x in alist:
for_suite
#一层缩进一般采一个tab 即4个空格
列表解析
sqdEvens=[x**2 for x in range(8) if not x%2]
文件
handle=open(file_name,access_mode='rwa+b')
handle.readline(1)
handle.readlines()
handle.close()
错误和异常
try:
pass
except IOError,e:
print e
函数
#若无return 则返回None
#函数定义可以用self参数
def function_name([arguments]):
“optional documentation string"
function_suite
#指定默认参数 argument=some
#调用 也可以指定默认参数传入位置
fuction_name([arguments])
类
#定义
class ClassName(base_class[es]):
“optional documentation string"
static_member_declarations
method_declarations
#双下划线特殊函数 初始化的 __init__() 类名self.__class__.__name__
#或者查看 print name.__doc__
#内部是 self.xxx
#实例化成对象 a=ClassName()
模块
#导入
import decimal
或者 from decimal import *
或者from decimal import Decimal(方便直接使用Decimal)
#使用
decimal.Decimal('1')
实用函数
dir() #显示对象的属性 #useful dir(s1)
help() #useful
int() str()
len() #get length
open()
range()
raw_input()
type() #返回对象类型
习题:
https://code.csdn.net/snippets/313982
- <PY><core python programming笔记>C2 快速入门
- <PY><core python programming 笔记>C1 概述
- <PY><core python programming笔记>C5 数字
- <PY><core python programming笔记>C12 模块
- <PY><core python programming笔记>C3 Python基础
- <PY><core python programming笔记>C4 Python对象
- <PY><core python programming笔记>C22 扩展Python
- <PY><core python programming笔记>C7 字典和集合类型
- <PY><core python programming笔记>C8 条件与循环
- <PY><core python programming笔记>C9 文件和输入输出
- <PY><core python programming笔记>C10 错误和异常
- <PY><core python programming笔记>C13 面向对象编程
- <PY><core python programming笔记>C14 执行环境
- <PY><core python programming笔记>C15 正则表达式
- <PY><core python programming笔记>C16 网络编程
- <PY><core python programming笔记>C17 网络客户端编程
- <PY><core python programming笔记>C18 多线程编程
- <PY><core python programming笔记>C19 图形用户界面编程
- bsh for android : 查标准时间
- 浏览器工作原理
- UVA 12538 Version Controlled IDE
- 黑马程序员 ---- java基础加强(上)
- ubuntu文件系统(FHS)
- <PY><core python programming笔记>C2 快速入门
- 软件项目经理必备素质
- 大话设计模式之外观模式
- 二叉树的创建及递归和非递归遍历
- mysql中文乱码问题
- 代码注释中的aka
- activereports报表控件7.0免费下载/破解教程/购买价格/功能介绍大全
- HDU Dividing (多重背包+二进制优化)
- eclipse maven 插件