<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









0 0
原创粉丝点击