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 'int'>
>>> type('Hello world!')
<type 'str'>
>>> type (type(4))
<type 'type'>
4)NonePython有一个特殊的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?'
分享到:
相关推荐
Python学习笔记4全文共8页,当前为第1页。Python学习笔记4全文共8页,当前为第1页。Python学习笔记4:编写一个窗口程序 Python学习笔记4全文共8页,当前为第1页。 Python学习笔记4全文共8页,当前为第1页。 上节我们...
Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
很适合零基础学习Python的同学。
最新Python学习笔记,Day 4,总结性学习笔记,适合初学者归纳总结。
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
Python学习笔记
Python学习笔记.pdf
4、Python中的常见运算符 4.1 算术运算符 4.2 关系运算符 4.3 逻辑运算符 4.4 位运算符 4.5 成员运算符 4.6 身份运算符 5、Python中的分支结构 5.1 单向分支-if语句 5.2 双向分支-if 5.3 多分支-if 5.4 ...
python学习笔记python学习笔记python学习笔记python学习笔记python学习笔记
Python 学习笔记
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
python 学习笔记资料python 学习笔记资料python 学习笔记资料python 学习笔记资料
王纯业的Python学习笔记非常适合初学者入门的Python教程
python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf ...
Python学习笔记--皮大庆
王纯业版《Python学习笔记》.pdf
python学习笔记+源码练习,简单易懂,让你从入门到不放弃