Python学习:Python第二周知识点回顾(数据类型及常用用法)


不可变数据类型:value改变,id也跟着改变
数字
字符串
布尔

一.

数字

整型int:年级,年纪,等级,身份证号,qq号,手机号
  level=10
浮点型float:身高,体重,薪资,温度,价格
  height=1.81
  salary=3.3

二.

字符串(str):包含在引号(单,双,三)里面,由一串字符组成。

取值:
  首先要明确,字符串整体就是一个值,只不过特殊之处在于:
  python中没有字符类型,字符串是由一串字符组成,想取出字符串中
  的字符,也可以按照下标的方式取得

  name:取得是字符串整体的那一个值
  name[1]:取得是第二位置的字符

字符串拼接:

  >>> msg1='hello'
  >>> msg2=' world'
  >>>
  >>> msg1 + msg2
  'hello world'
  >>> res=msg1 + msg2
  >>> print(res)
  hello world

  >>> msg1*3
  'hellohellohello'

字符串的内置方法:

移除空白strip

  

#移除空白strip
msg='             hello         '
print(msg)
print(msg.strip())
#移除星星
msg='***hello*********'
msg=msg.strip('*')
print(msg)
#移除左边星星,移除右边星星
print(msg.lstrip('*'))
print(msg.rstrip('*'))
#用处
while True:
    name=input('user: ').strip()
    password=input('password: ').strip()

    if name == 'egon' and password == '123':
        print('login successfull')

#    切分split,切分为列表
info='root:x:0:0::/root:/bin/bash'
print(info[0]+info[1]+info[2]+info[3])

user_l=info.split(':')
print(user_l[0])

msg='hello world egon say hahah'
print(msg.split()) #默认以空格作为分隔符

cmd='download|xhp.mov|3000'
print(cmd.split('|'))#切分返回切分后的列表,但是cmd不做改变
cmd_l=cmd.split('|')
print(cmd_l)
print(cmd_l[1])
print(cmd_l[0])

print(cmd.split('|',1))#后面加数字,表示切分第几位

#用处
while True:
cmd=input('>>: ').strip()
if len(cmd) == 0:continue
cmd_l=cmd.split()
print('命令是:%s 命令的参数是:%s' %(cmd_l[0],cmd_l[1]))
 

 

#     长度len

# print(len('hell 123'))
#     索引
a=[1,2,2131,314,12]
print(a[2])
#    切片:切出子字符串
msg='hello world'
print(msg[1:3]) #1 2
print(msg[1:4]) #1 2 3
#    isdigit查询是否是数字类型
oldboy_age=84
while True:
    age=input('>>: ').strip()
    if len(age) == 0:continue
    if age.isdigit():
        age=int(age)
    else:
        print('must be int')
#    查询首字母与尾字母是否正确:startswith,endswith
name='alex_SB'
print(name.endswith('SB'))
print(name.startswith('alex'))
#替换.replace
name='alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))

print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))
print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
print('my name is {0} my age is {1} my sex is {0}:{2}'.format('egon',18,'male'))
print('my name is {name} my age is {age} my sex is {sex}'.format(
    sex='male',
    age=18,
    name='egon'))
在name中查找需要查找需要的子字符串
name='goee say hello'
print(name.find('S',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
print(name.index('S')) #同上,但是找不到会报错

print(name.count('S',1,5)) #顾头不顾尾,如果不指定范围则查找所有

 

#join    
str = "-";
seq = ("a", "b", "c"); # 字符串序列
print str.join( seq );
#lower,upper把字符串更改大小写
# name='eGon'
# print(name.lower())
# print(name.upper())
# expandtabs\t默认是4个空格,expandtabs括号是几,就把\t变成几个空格,
name='egon\thello'
print(name)
print(name.expandtabs(1))
#center,ljust,rjust,zfill
name='egon'
print(name.center(30,'-'))#engon用-凑成一排30个子字符串,egon在中间。
print(name.ljust(30,'*'))#egon在最左边
print(name.rjust(30,'*'))#egon在最右边
print(name.zfill(50)) #用0填充,egon在最右边
#captalize,swapcase,title
name='eGon'
print(name.capitalize()) #首字母大写,其余部分小写
print(name.swapcase()) #大小写翻转
msg='egon say hi'
print(msg.title()) #每个单词的首字母大写
#在python3中
num0='4'
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='' #中文数字
num4='' #罗马数字


#isdigt:str,bytes,unicode
# print(num0.isdigit())
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())

#isdecimal:str,unicode
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
# print(num0.isdecimal())
# # print(num1.)
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())

#isnumeric:str,unicode,中文,罗马
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
#
# print(num0.isnumeric())
# # print(num1)
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())




#is其他
# name='egon123'
# print(name.isalnum()) #字符串由字母和数字组成
# name='asdfasdfa sdf'
# print(name.isalpha()) #字符串只由字母组成
#

# name='asdfor123'
# print(name.isidentifier())
name='egGon'
print(name.islower())
# print(name.isupper())
# print(name.isspace())
name='Egon say'
print(name.istitle())
View Code

 

列表list:

  包含在[]内,用逗号分割开。

用途

  (存多个值,可以修改)

  hobby=['play','eat','sleep']

方法:
  hobby.append
  hobby.remove

操作:
查看:
>>> girls=['alex','wsb',['egon','ysb']]
>>> girls[2]
['egon', 'ysb']
>>> girls[2][0]

增加:
  girls.append(元素)
删除
  girls.remove(元素)
  del girls[元素的索引]
修改
  girls[0]='alex....'

列表内置方法

#     追加
hobbies=['play','eat','sleep','study']
hobbies.append('girls')
print(hobbies)
View Code
#     删除
hobbies=['play','eat','sleep','study']
x=hobbies.pop(1) #不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值
print(x)
print(hobbies)
View Code
#队列:先进先出
queue_l=[]
#入队
queue_l.append('first')
queue_l.append('second')
queue_l.append('third')
print(queue_l)
#出队
print(queue_l.pop(0))
print(queue_l.pop(0))
print(queue_l.pop(0))


#堆栈:先进后出,后进先出
l=[]
#入栈
l.append('first')
l.append('second')
l.append('third')
# #出栈
print(l)
print(l.pop())
print(l.pop())
print(l.pop())
View Code
# del hobbies[1] #单纯的删除
# hobbies.remove('eat') #单纯的删除,并且是指定元素去删除
#     长度
hobbies=['play','eat','sleep','study']
print(len(hobbies))

#     包含in
hobbies=['play','eat','sleep','study']
print('sleep' in hobbies)

msg='hello world egon'
print('egon' in msg)#判断结果输出布尔值

##pat2===》掌握部分
hobbies=['play','eat','sleep','study','eat','eat']
hobbies.insert(1,'walk')
hobbies.insert(1,['walk1','walk2','walk3'])#只能添加一个值
print(hobbies)

print(hobbies.count('eat'))
print(hobbies)
hobbies.extend(['walk1','walk2','walk3'])#分开批量添加
print(hobbies)

hobbies=['play','eat','sleep','study','eat','eat']
print(hobbies.index('eat'))#查询字符串在列表中的索引位置


#pat3===》了解部分
hobbies=['play','eat','sleep','study','eat','eat']
hobbies.clear()
print(hobbies)

l=hobbies.copy()
print(l)

l=[1,2,3,4,5]
l.reverse()
print(l)#列表内的值反转

l=[100,9,-2,11,32]
l.sort(reverse=True)
print(l)#列表内的值由大到小排列
View Code

 

字典dict:

  定义在{},逗号分割,每一个元素的形式都是key:value

用途:

  存多个值,这一点与列表相同,值可以是任意数据类型
特征:

  每一个值都一个唯一个对应关系,即key,强调一点,key必须是不可变类型:字符串,数字

 

操作:
查看
  >>> student_info={
  ... 'age':81,
  ... 'name':'alex',
  ... 'sex':None,
  ... 'hobbies':['zsb0','zsb1','zsb2','zsb30']
  ... }
  >>>
  >>> student_info['age']
  81
  >>> student_info['hobbies']
  ['zsb0', 'zsb1', 'zsb2', 'zsb30']
  >>> student_info['hobbies'][2]
  'zsb2'
增加
  student_info['stu_id']=123456

删除
  del student_info['stu_id']

修改
  student_info['name']='alex....'

五.

布尔:True False
用途:用来判断

  >>> pinfo={'name':'oldboymei','age':53,'sex':'female'}
  >>>
  >>>
  >>> pinfo['age'] > 50
  True
  >>> pinfo['sex'] == 'female'
  True

 

posted @ 2017-07-23 15:26  最咸的咸鱼  阅读(183)  评论(0)    收藏  举报