Python快速学习02:基本数据类型 & 序列

it2022-05-09  33

前言

  系列文章:[传送门]

  也就每点一点点的开始咯,“还有两年时间,两年可以学很多东西的”

  

  Python ['paɪθən]  n. 巨蛇,大蟒

    

 

基本数据类型 

变量不需要声明  

a=10 # int 整数 a=1.3 # float 浮点数 a=True # 真值 (True/False) a='Hello!' # 字符串

 

例子

a=10 print (a) print (type(a)) a =1.3 print (a,type(a))

#内置函数type(), 用以查询变量的类型。

 

会有下面的输出

10 <class 'int'> 1.3 <class 'float'>

#print的另一个用法,也就是print后跟多个输出,以逗号分隔。

 

sequence 序列

  sequence(序列)是一组有顺序的对象的集合

  python中最基本的数据结构,每一个元素被分配一个需要——元素的位置,亦称“索引”,首个索引为0,第二个为1,后面依此类推。python包含六种内建的序列类型:列表(list)、元组(tuple)、字符串、Unicode字符串、buffer对象和xrange对象。

  tuple和list的主要区别在于,一旦建立,tuple的各个元素不可再变更,而list的各个元素可以再变更

 

例子

s1 = (2,1.3,'love',5.6,9,12,False) s2 = [True , 5, 'smile'] print (s1,type(s1)) print (s1,type(s2))

 

会有下面的输出

(2, 1.3, 'love', 5.6, 9, 12, False) <class 'tuple'> (2, 1.3, 'love', 5.6, 9, 12, False) <class 'list'>

#s1是一个tuple

#s2是一个list

 

Python列表 list 

创建一个列表

  解释器会在内存中创建类似数组的数据结构来存储,数据项自下而上

members = ['jeff','li','mum','dad']

    

        堆栈中的数据

 

列表操作

例子

members = ['jeff','li','mum','dad'] print (len(members)) print (members[1]) members.append('33') print ('members(append):',members) members.pop() members.extend(['qq','ll']) print ('members(extend):',members) members.remove('ll') print ('members(remove):',members) members.insert(0, 'xx') print ('members(insert):',members)

#len() 列表大小

#append  pop  remove  insert 

 

会有下面的输出

4 li members(append): ['jeff', 'li', 'mum', 'dad', '33'] members(extend): ['jeff', 'li', 'mum', 'dad', 'qq', 'll'] members(remove): ['jeff', 'li', 'mum', 'dad', 'qq'] members(insert): ['xx', 'jeff', 'li', 'mum', 'dad', 'qq']

 

其他 for , if 等操作会用到列表,以后讲到。

 

总结

  #变量不需要声明,不需要删除,可以直接回收适用。

  #sequence(序列)

  #列表及其操作

 

感谢及资源共享

    

    路上走来一步一个脚印,希望大家和我一起。

    感谢读者!很喜欢你们给我的支持。如果支持,点个赞。

    知识来源: http://book.douban.com/doulist/3870144/

 

转载于:https://www.cnblogs.com/Alandre/p/3643737.html


最新回复(0)