Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4025060
  • 博文数量: 626
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 11080
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-23 13:08
文章分类

全部博文(626)

文章存档

2015年(72)

2014年(48)

2013年(506)

分类: Python/Ruby

2013-09-29 10:04:34

Python学习笔记(4)


1)Python Objects
所有的Python对象都有下列三个特征:
  id:对象间相互区别的唯一标识符。任何对象的id均可通过内建的id()函数获得,其值为内存地址。
  类型:对象的类型指示对象能保持的类型、能应用的操作。可使用内建的type()函数显示对象的类型。
  值:对象表示的数据项。
确定的Python对象有熟悉、数据值或可执行代码(如方法)等。

2)标准类型
· 数字(4种单独的子类型)
  -Regular或纯整数
  -长整数
  -浮点实数
  -复杂数
· 字符串String
· 列表List
· 元组Tuple
· 字典Dictionary
标准类型也作为原始数据类型。在Python中,标准数据类型不是类,因此创建整数和字符串时不涉及实例化。还意味着你不能子类化一个标准类型。

3)其它内建类型
· Type类型
· None空
· File文件
· Function函数
· Module模块
· Class类
· Class Instance类实例
· Method方法

Types和内建的type()函数
type()内建函数:得到对象并返回其类型。
>>> type(4)

>>> type('Hello world!')

>>> type (type(4))


4)None
Python有一个特殊的None对象,它仅有一个值,None。None值等同于C语言的null。None没有属性,总等于布尔值的false。

5)内部类型
-Code
  Code对象是字节编译的Python源码的可执行块,通常通过调用内建函数compile()返回值。
  此对象通过exec或内建函数eval()执行。
-Frame
  Frame对象包含Python解释器在Runtime执行环境所需的所有信息。
-Traceback
  Traceback对象是一数据项,存储异常创建或出现时的堆栈跟踪信息。
-Slice
  当使用Python扩展slice语法时,将创建Slice对象。此扩展语法允许不同的索引类型。这些索引类型包括步距(stride)索引、多维(multi-dimensional)索引,以及使用省略(Ellipsis)类型的索引。
-Ellipsis
  当使用扩展slice符号时,使用Ellipsis对象。
-Xrange
  Xrange对象通过内建函数xrange()创建。
  
6)标准操作类型
f1和f2引用同一对象:f1 = f2 = 4
f1和f2引用不同对象:f1 = 4; f2 = 3+1

7)内建函数标准类型
cmp(obj1, obj2):比较obj1和obj2的值,返回值为:-1,0,1。
repr(obj):obj转换为字符串,且两边加上“'”符号。
str(obj):obj转换为字符串。
type(obj):查看类型。

import string
alphas = string.letters+'_'
nums = string.digits
print alphas
print nums
print 'Welcome to the Identifier Checker v1.0'
print 'Testees must be at least 2 chars long.'
inp = raw_input('Identifier to test?')
if len(inp)>1:
  if inp[0] not in alphas:
  print '''invalid: first symbol must be alphabetic'''
  else:
  for otherChar in inp[1:]:
  if otherChar not in alphas + nums:
  print '''invalid: remaining symbols must be alphanumeric'''
  break
  else:
  print "okey as an identifier"

8)String的内建方法
string.capitalize() 大写字符串的第一个字母
string.center(width) 字符串以给定宽度显示,如长度不足,两边补充空格
string.count(str,beg=0,end=len(string)) 计算str在字符串中出现的次数
string.encode(encoding='UTF-8', errors='strict') 字符串转换编码,error还有ignore或replace
string.endswith(str,beg=0,end=len(string)) 查看字符串是否以str结尾
string.expandtabs(tabsize=8) 返回字符串,其所有/t用空格代替
string.find(str, beg=0,end=len(string)) 查看子字符串,如子串未发现则返回-1
string.index(str, beg=0,end=len(string)) 同find(),但子串未发现则返回ValueError
string.isalpha() 字符串所有字母均为字符则返回真
string.isdigit() 字符串至少有1个字符,且所有字符均为数字,则返回真
string.islower() 判断字符串所有字符是否小写
string.isspace() 判断字符串是否为空格
string.istitle()  
string.issupper()
string.lower() 转换所有大写字母为小写
string.replace(str1,str2,num=string.count(str1)) 在字符串中用str2替换str1
string.split(str="",num=string.count(str))  
string.upper() 转换所有小写字母为大写

例子:
>>> quest = 'what is your favorite color?'
>>> quest.capitalize()
'What is your favorite color?'
>>> quest.count('or')
2
>>> quest.endswith('blue')
False
>>> quest.endswith('color')
False
>>> quest
'what is your favorite color?'
>>> quest.endswith('color?')
True
>>> quest.find('or',30)
-1
>>> quest.find('or',22)
25
>>> quest.index('or',10)
16
>>> ':'.join(quest.split())
'what:is:your:favorite:color?'
>>> quest.upper()
'WHAT IS YOUR FAVORITE COLOR?'
阅读(1194) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~