python基本数据类型

int
#======================================基本使用======================================
#1、用途:记录年龄\等级\各种号码

#2、定义方式
age=18
age=int(18)

x=int('123')只能将纯数字的字符串转换成整型
print(type(x))显示类型 int
print(int(3.7))强转结果为3
#3、常用操作+内置的方法

#======================================该类型总结====================================
#存一个值
#可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
print(hash(10))可hash
print(hash([1,2,3]))不可hash

二 浮点型
#======================================基本使用======================================
#1、用途:记录身高\体重\薪资

#2、定义方式
salary=1.3
salary=float(1.3)
x=float('3.1')只能这样转换 不能使用int来转
print(x,type(x))


#3、常用操作+内置的方法

#======================================该类型总结====================================
#存一个值
#不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
x=3.1
print(id(x))
浮点类型也是不可变类型

了解:
复数
x=1-2j
print(x,type(x))
print(x.real)实部
print(x.imag)虚部

长整型
py2有长整型
py3没有长整型

其他进制转十进制
十进制:0-9
11=1*10^1+1*10^0

二进制 0-1
11=1*2^1+1*2^0

八进制 0-7
11=1*8^1+1*8^0

十六进制 0-9 A-F
11 = 1*16^1+1*16^0

十进制=>其他进制
print(bin(13)) 0b1101 十进制转二进制
print(oct(13)) 0o15 十进制转八进制
print(hex(13)) 0xd 十进制转十六进制
字符串类型 str
#======================================基本使用======================================
#1、用途:记录描述性质的特征,比如:名字\地址\性别

#2、定义方式:在单引号\双引号\三引号内包含一串字符
msg='aaa"bbb"'
msg=str(...)

str可以将任意类型转化为字符串
str(1)
str(1.3)
x=str([1,2,3])
print(x,type(x))

#3、常用操作+内置的方法
#作用:名字,性别,国籍,地址等描述信息

#定义:在单引号\双引号\三引号内,由一串字符组成
name='egon'

#优先掌握的操作(************):
#1、按索引取值(正向取+反向取) :只能取
msg='hello word'
print(msg[0])
print(msg[5])
len(msg)
print(msg[len(msg)-1])
print(msg[-1])

msg[0]='h' 不能这么写 字符串不支持这种赋值操作

#2、切片(顾头不顾尾,步长):想要从一个大字符串中切出一个小的字符串
msg='hello world'
print(msg[0:5]) 取出hello
print(msg) 原字符串不变
print(msg[0:5:2]) 步长为2的取值 0 2 4

print(msg[-1:-5:-1]) d l r o
print(msg[0:5:1])

全部输入的形式
print(msg[0::1])
print(msg[::])
不写最后 默认取到最后

#3、长度len
msg='你好a'
print(len(msg))长度为3
#4、成员运算in和not in
msg='liugang是大帅b'
print('liugang' in msg)
print('liugangs' not in msg)
print(not '钢' in msg)
都返回true
#5、移除空白strip 默认去空格
pwd=' 1 23 '中间带空格不可去
res=pwd.strip(' ')#去除字符串左右两边的东西 名字()这是python的很多内置的功能self一般当做不存在
print(res)
print(pwd) #查看字符串是不是可变类型

pwd=input('>>:').strip(' ')
if pwd=='123':
print('密码输入正确')

pwd='***/&^123**^**/&'
print(pwd.strip('*/&')) 把所有想去的都去掉^不在他的前面不去 ^123**^
print(pwd.strip('*/&^')) 只取123 把所有想去的都去掉

#6、切分split:针对有规律字符串按照某个字符切成列表
info='yyhbsd:18:female'
li=info.split(':',1)指定切分次数1 可以是:也可以是其他的
print(li) 切成一个列表yyhbsd 18 female 将:去除
#7、循环
msg='hello'
for item in msg: #从msg取出一个值给item输出 然后继续取
print(item)

#======================================该类型总结====================================
#存一个值

#有序

#不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
s1='hello'
print(id(s1))
s1='world'
print('id(s1)')
值变了id也变了 是不可变类型


需要掌握的操作(*****)
#1、strip,lstrip,rstrip
print('***egon****',strip('*'))
print('***egon****',lstrip('*'))去左边的*
print('***egon****',rstrip('*'))去右边的*
#2、lower,upper
print('AAAbbc'.lower())把大写变小写
print('AAAbbc'.upper())把小写变大写

#3、startswith,endswith
print('alex is qwe'.startswith('alex')) alex开头
print('alex is qwe'.endswith('qwe')) qwe结尾

#4、format的三种玩法
print('my name is %s my age is %s'%('egon',18))
'my name is {x} my age is {y}'.format(y=18,x='egon') 初始化字符串 可以随便修改变量
'my name is {} my age is {}'.format(18,'egon') 这样可以按照位置传
'my name is {1} my age is {0}'.format(18,'egon')
'my name is {0} my age is {1}'.format(18,'egon') 指定索引按照后面的值索引输入
#5、split,rsplit
msg='a:b:c:d:e'
print(msg.split(':',1)) 从前往后切
print(msg.rsplit(':',1)) 从后往前切
#6、join
msg='a:b:c:d:e'
list1=msg.split(':')
msg=':'.join(list1)
print(msg)

info='egon:123:male'
list1=info.split(':')
print(list1) 切片
print(':'.join(list1)) 插入
#7、replace
msg='alex is alex alex is hahahaha'
print(msg.replace('alex','sb',1)) 指定1换一个 也可以不指定全部替换
#8、isdigit
print('123'.isdigit()) 只能判断字符串是否是数字 返回true
print('12.3'.isdigit()) 返回fasle

age_of_db=30
inp_age=input('>>>:').strip()

if inp_age.isdigit():
inp_age=int(inp_age)
if inp_age>age_of_db:
print('too big')
elif inp_age<age_of_db:
print('too small')
else:
print('you get it')
#了解操作
#1、find,rfind,index,rindex,count
msg='hello world'
print(msg.index('wo'))索引找到第六个 6
print(msg.index('wo',0,3)) 在0~3中找不到报错
print(msg.find('wo',0,3)) 在0~3中找不到返回-1
print(msg.count('l')) 统计个数

#2、center,ljust,rjust,zfill

print('egon'.center(50,'=')) 居中输出egon两边用=补齐
name=input('>>:').strip()
print(('%s'%name).center(50,'-')) 输入一个数将这个数居中显示两边各用25个-补齐
print('egon'.ljust(50,'=')) 左对齐 不够用=补齐
print('egon'.rjust(50,'=')) 右对齐 不够用=补齐
print('egon'.zfill(50)) 右对齐不够默认用0填充 50个0
#3、expandtabs
print('hello\tworld'.expandtabs(5)) 制表符变为5个空格
#4、capitalize,swapcase,title
print('hello world'.capitalize()) 首字母变成大写
print('Hello World'.swapcase()) 大写变小写 小写变大写
print('Hello wOrldS'.title()) 每个单词首字母变成大写 其他的小写
#5、is数字系列
num1=b'4' bytes
num2=u'4' unicode,python3中无需加u就是Unicode
num3='四' 中文数字
num4='IV' 罗马数字
#isdigit:只能识别bytes,str
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

#isdecimal:只能识别字符串中包含数字
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())

#isnumberic:可以识别str 中文\罗马数字
print(num2.isnumberic())
print(num3.isnumberic())
print(num4.isnumberic())
#6、is其他
print('aa123'.isalpha()) 字符串当中必须包含纯字母
print('aasdfaA1234'.isalnum()) 字母或数字组成
print('aa123'.isalnum())
print('aa123'.isalnum())
print(' '.isspace)
print(' 123'.isspace)

上一篇:Python基础


下一篇:14.list列表