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

时间:2023-03-09 20:15:49
Python快速学习02:基本数据类型 & 序列

前言

  系列文章:[传送门]

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

  

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

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

基本数据类型 

变量不需要声明  

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']

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

        堆栈中的数据

列表操作

例子

members = ['jeff','li','mum','dad']
print (len(members))
print (members[1]) members.append('')
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', '']
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(序列)

  #列表及其操作

感谢及资源共享

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

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

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

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