python学习笔记(1)
基本数据类型
1.整数型
2.浮点数
3.字符串型
4.列表
5.元组
6.字典
7.布尔值
8.集合
判断数据类型函数
type()
int整数 float浮点数 list列表 tuple元组 dict字典 set集合 str字符串
eval()与exec()
eval()
将括号内的字符串作为执行命令,执行并返回结果
如
x=2
str1='x**2-8*x+2+2**x'
y=eval(str1)
print(y)
-6
exec()
将括号内的字符串作为执行命令执行
如
exec('print("hello")') #单引号内引号用双引号
eval('print("hello2")')
x=2
print(eval('x+2'))
print(exec('x+2')
exec(x+=2)
print(x)
hello
hello2
4
None
4
类型转换
int(str) : 转换为int型。
int('1') >>> 1
float(int/str) : 将int型或字符型转换为浮点型。float('1') >>> 1.0
str(int) : 转换为字符型。
str(1) >>> '1'
bool(int) : 转换为布尔类型。
bool(0) >>> False
bool(None) >>> False
bytes(str,code) : 接收一个字符串,与所要编码的格式,返回一个字节流类型。
bytes('abc', 'utf-8') >>> b'abc'
bytes(u'爬虫', 'utf-8') >>> b'\xe7\x88\xac\xe8\x99\xab #u表示后接unicode字符串
list(iterable) : 转换为list。
list((1,2,3)) >>> [1,2,3]
iter(iterable): 返回一个可迭代的对象。 `
iter([1,2,3]) >>> <list_iterator object at 0x0000000003813B00>`
dict(iterable) : 转换为dict。
dict([('a', 1), ('b', 2), ('c', 3)]) >>> {'a':1, 'b':2, 'c':3}
enumerate(iterable) : 返回一个枚举对象。
tuple(iterable) : 转换为tuple。
tuple([1,2,3]) >>>(1,2,3)
set(iterable) : 转换为set。
set([1,4,2,4,3,5]) >>> {1,2,3,4,5}
set({1:'a',2:'b',3:'c'}) >>> {1,2,3}
hex(int) : 转换为16进制。`
hex(1024) >>> '0x400'
oct(int) : 转换为8进制。 `
oct(1024) >>> '0o2000'
bin(int) : 转换为2进制。
bin(1024) >>> '0b10000000000'
chr(int) : 转换数字为相应ASCI码字符。
chr(65) >>> 'A'
ord(str) : 转换ASCI字符为相应的数字。
ord('A') >>> 65