Python笔记

Python笔记~Tandaly


常用异常类型
Exception 所有异常
TypeError 类型异常
ValueError 值转换异常
EOFError 输入输出异常
IndexError 索引异常


Integral类型(int、bool)
int:表示整型 可以表示任意长度的整数,除非内存不够用了
bool:表示布尔类型,由True和False,他们用int表示时True是1 False是0


使用进制表示方法:
十进制:任意数字 decimal
二进制:以0b开头 binary
八进制:以0o开头 octal
十六进制:以0x开头 hexadecimal
进制间的转换方法:
bin()
oct()
hex()
int()


字符串类型(str)
str是由引号包含起来的字符串,可以用单引号、双引号、三引号,如果出现要换行请使用以下三种情况的一种即可
s = '''My name is
      Tandaly
     '''
     
s = "My name is" + \
     "Tandaly"


s = ("My name is"
     "Tandaly")
字符串分片操作:
seq[start]
seq[start:end]
seq[start:end:step] 
其中seq表示任意序列,包括列表、元组、字符串
字符串反转:
>>>s = "Hello World"
>>>s[::-1]
    'dlroW olleH'
字符串格式化(str.format())
str.format()会返回一个新的字符串,是经过一定格式处理的字符串
如:
>>>“hello {0} {1}”.format("world", "Tandaly")
'hello world Tandaly'


>>>"{0} years".format(2013)
'2013 years'


>>>"{{0}} years".format(2013)
'{2013} years'


>>>"{{0}} years".format(0)
'{0} years'


>>>"{who} is age of {0}".format(23, who="tandaly")
'tandaly is age of 23'


>>> stock=["fei", "tandaly"]
>>> "{0[0]} is {0[1]}".format(stock)
'fei is tandaly'


>>> d = dict(name="fei", nick="tandaly")
>>> "{0[name]} is {0[nick]}".format(d)
'fei is tandaly'


>>> import sys
>>> import math
>>> "math.pi={0.pi}, sys.maxunicode={1.maxunicode}".format(math, sys)
'math.pi=3.141592653589793, sys.maxunicode=1114111'


>>> "{} {} {}".format("fei", "tandaly", "tan")
'fei tandaly tan'


>>> a = "fei"
>>> b = "tandaly"
>>> "{a} is {b}".format(**locals())
'fei is tandaly'


>>> d = dict(name="fei", nick="tandaly")
>>> "{name} is {nick}".format(**d)
'fei is tandaly'


>>> "{0:0=10}".format(232323)
'0000232323'
>>> "{0:2=10}".format(314)
'2222222314'


>>> "{0:*<10}".format(314)
'314*******'
>>> "{0:*>10}".format(314)
'*******314'
>>> "{0:*^10}".format(314)
'***314****'


>>> "[{0: }] [{1: }]".format(314, -314)
'[ 314] [-314]'
>>> "[{0:}] [{1:}]".format(314, -314)
'[314] [-314]'
>>> "[{0:+}] [{1:+}]".format(314, -314)
'[+314] [-314]'
>>> "[{0:-}] [{1:-}]".format(314, -314)
'[314] [-314]'


>>> "{0:d} {0:b} {0:o} {0:x} {0:X}".format(314)
'314 100111010 472 13a 13A'
>>> "{0:#d} {0:#b} {0:#o} {0:#x} {0:#X}".format(314)
'314 0b100111010 0o472 0x13a 0X13A'


>>> "{0:,} {0:*>15,}".format(int(3.1415926e7))
'31,415,926 *****31,415,926'


转换操作:
>>>import decimal
>>> "{0} {0!s} {0!r} {0!a}".format(decimal.Decimal("3.14"))
"3.14 3.14 Decimal('3.14') Decimal('3.14')"






获取当前使用场所
>>> import locale
>>> locale.getlocale()
("Chinese_People's Republic of China", '936')


>>> ord("*")
42
>>> chr(42)

'*'



python博客:

http://www.cnblogs.com/txw1958/category/398980.html




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值