首页 > 代码库 > 基本数据类型

基本数据类型

一:基本数据类型

整型int:年纪,年级,等级,身份证号,qq号,手机号

1数字类型          Level=10

浮点型float:价格,身高,体重,温度,价格。

Height=1.81米

Salary=3.3万

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

用途(描述性的数据):姓名,性别,地址,学历,密码:123aavsv

拼接

>>> msg1=‘hell‘

>>> msg2=‘word‘

>> res=msg1+msg2

查>>> print(res)

Hellword

 

>> msg3=‘$‘

>>> msg3*100

‘$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$‘

 

改字符:

>> student_info=[1,‘a‘,‘b‘]

>>> student_info

[1, ‘a‘, ‘b‘]

>>> student_info[0]

1

>>> student_info[0]=1000

>>> student_info

[1000, ‘a‘, ‘b‘]

3列表类型list:包含在[]内,用逗号隔开。

嵌套取值:

             用途:(存多个值,可以修改)

>>> hobby=[‘play‘,‘eat‘,‘sleep‘]

>>> hobby

[‘play‘, ‘eat‘, ‘sleep‘]

>>> type(hobby)

<class ‘list‘>

用途:(存多个值,可以修改)

方法:新增hobby.append(元素)

>>> hobby.append(‘study‘)

>>> hobby

[‘play‘, ‘eat‘, ‘sleep‘, ‘study‘]

    清除hobby.remove(元素)

          Hobby.remove(元素的索引)

  •        >> hobby.remove(‘play‘)

>>> hobby

[‘eat‘, ‘sleep‘, ‘study‘]

查:name

>>> hobby=[‘play‘,‘eat‘,‘student‘]

>>> hobby

[‘play‘, ‘eat‘, ‘student‘]

在[]中可以包含整型,浮点型,字符串类型,列表等任何类型

查:

>>> girls=[]

>>> girls

[]

>>> girls.append(‘zhang‘)

>>> girls.append(‘wang‘)

>>> girls.append(‘dao‘)

>>> grils

 [‘zhang‘, ‘wang‘, ‘dao‘]

 

>>> girls.append(‘1‘)

>>> girls.append(‘1.2.3‘)

>>> girls.append(‘202‘)

>>> girls.append(‘303‘)

>>> girls

[‘zhang‘, ‘wang‘, ‘dao‘, ‘1‘, ‘1.2.3‘, ‘202‘, ‘303‘]

:hobby[o]=sing

>> hobby=[‘play‘,‘eat‘,‘student‘]

>>> hobby

[‘play‘, ‘eat‘, ‘student‘]

>>> hobby[1]

‘eat‘

>>> hobby[1]=‘sing‘

>>> hobby

[‘play‘, ‘sing‘, ‘student‘]

>>> 

 

 取值:name[]中写对应的索引数字

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

1: name:取的是字符串整体的值。

>>> girls=[‘alex‘,‘wsb‘,[‘egon‘,‘ysb‘]]

>>> girls[2]

[‘egon‘, ‘ysb‘]

>>>irls[0]

‘alex‘

girls[‘王尚龙‘]

>> girls

[‘王尚龙‘]

 

               2: name[1]:取得是第二个位置的字符。

                >> long=‘wang‘

>>> long[0]

‘w‘

>>> long=‘wang shang‘

>>> long[5]

‘s‘

>>> long[4]

‘ ‘

 

4字典类型:定义在花括号里面{},逗号分割,每个元素的的形式都是key:value。

         用途:存多的值,这一点和列表相同,值可以是任何数据类型。

         特征:每一个值都是一个唯一对应关系,即key,强调一点,key必须是不可变。

查:name

>>> student_info={‘name‘:‘ager‘,‘sex‘:‘None‘,‘age‘:‘26‘,‘hobbies‘:[‘zsb0‘,‘zsb1‘,‘zsb3‘,‘zsb4‘]}

>>> student_info

{‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘hobbies‘: [‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘]}

>>> type(student_info)

<class ‘dict‘>

         取值:

> student_info

{‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘hobbies‘: [‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘]}

>>> student_info[‘age‘]

‘26‘

 

>>> student_info[‘hobbies‘]

[‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘]

>>> student_info[‘hobbies‘][2]

‘zsb3‘

增加:tudent_info[‘stu_id‘]=123456

‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘hobbies‘: [‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘]}

>>> student_info[‘stu_id‘]=123456

>>> student_info

{‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘hobbies‘: [‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘], ‘stu_id‘: 123456}

删除:del student_info[‘hobbies‘]

{‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘hobbies‘: [‘zsb0‘, ‘zsb1‘, ‘zsb3‘, ‘zsb4‘], ‘stu_id‘: 123456}

>>> del student_info[‘hobbies‘]

>>> student_info

{‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘stu_id‘: 123456}

改:>>> student_info[‘name‘]=‘sb‘

‘name‘: ‘ager‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘stu_id‘: 123456}

>>> student_info[‘name‘]

‘ager‘

>>> student_info[‘name‘]=‘sb‘

>>> student_info

{‘name‘: ‘sb‘, ‘sex‘: ‘None‘, ‘age‘: ‘26‘, ‘stu_id‘: 123456}

5:布尔类型

          True fase

          用途:用来判断

          >>> age=65

>>> age > 50

True

>>> age < 50

False

>>> 

基本数据类型