整型(int型,只保存整数)
常用函数及方法
可使用abs()函数取绝对值
abs(...)
abs(number) -> number
Return the absolute value of the argument.
例子:
a=-10
print (abs(a))
输出为10
可使用dir()函数查看该整型有哪些方法可以使用
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attribut
es
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
print (dir(a))
浮点型(float型,可以保存小数)
常用函数及方法
round函数
round(...)
round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative.
b=2.3333
print (round(b))
输出为2
布尔型
下面是python中布尔操作:x or y:if x is false,then y, else x
x and y:if x is false, then x, else y
not x:if x is false, then True, else False
python的字符串和常用方法
字符串是有下标的,常用方法
a='1234567'
print (a[0],a[5])
输出1 6
find()查找
a='1234567'
print (a.find('45'))
输出3 返回子字符串下标
join()插入
print ('99'.join('aaa'))
输出a99a99a
split()拆分
a='1234567'
print (a.split('45'))
输出['123', '67']列表
replace()替换
a='1234567'
print (a.replace('123','abc'))
输出abc4567
strip()去掉字符串前后空格
b=' a b c '
print (b.strip())
输出 a b c
format()
print "{0} is {1} years old".format("FF", 99)
print "{} is {} years old".format("FF", 99)
print "Hi, {0}! {0} is {1} years old".format("FF", 99)
print "{name} is {age} years old".format(name = "FF", age = 99)