1. 程式人生 > >python之路--day1--輸入與輸出&&數據類型

python之路--day1--輸入與輸出&&數據類型

test 整體 lis 相同 msg app 字符類型 身份證 []

輸入與輸出

輸出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

python之路--day1--輸入與輸出&&數據類型