python之路--day1--输入与输出&&数据类型

输入与输出

输出print()

在括号中加上字符,输出相应字符。

>>>print("hello world!")
hello world!

多行输出

>>>print('''
hello,world!
this is a test py.
''')
hello,world!
this is a test py.

输入input()

>>> name = input('please enter your name:')
>>> print('hello,',name)

name= 该代码为变量赋值
input('please enter your name:')该代码为友好提示语
print('hello,',name)该代码为调用name变量,用来区分字符于变量

 

数据类型

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

>>>level=10

浮点型(float):身高,体重,薪资,温度,价格

>>>height=1.75
>>>salary=1.5

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

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

>>>name='8192bit'

取值:

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

字符串拼接:

>>> msg1='hello'
>>> msg2=' world'
>>> msg1 + msg2
'hello world'

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

>>> msg1*3
'hellohellohello'

列表(list):包含在[]内,用逗号分隔开

用途:(存多个值,可以修改):爱好,装备,女朋友们

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

方法:

hobby.append

hobby.remove

操作:

查看

>>>girls=['alex','wsb',['egon','ysb']]
>>> girls[2]
['egon', 'ysb']
>>> girls[2][0]

增加

>>>girls.append(元素)

删除

>>>girls.remove(元素)

修改

>>>girls[0]='alesSB'

字典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']='alexSB'

布尔值:True False

用途:用来判断

>>> pinfo={'name':'oldboymei','age':53,'sex':'female'}

pinfo['age'] > 50
True
>>> pinfo['sex'] == 'female'
True

 

posted @ 2017-07-17 19:10  8192bit  阅读(158)  评论(0编辑  收藏  举报