Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
2014-12-22 23:02
1451 查看
Python的变量和数据类型 1 、python的变量是不需要事先定义数据类型的,可以动态的改变 2、 Python当中一切皆对象,变量也是一个对象,有自己的属性和方法 我们可以通过 来查看变量的类型:变量名.__class__ 调用变量的方法:变量名.方法() #!/bin/env python
#coding:utf-8
#type 打印出数据的类型
print type(1)
print type(1.0)
print type("helloworld")
#虚数 如12j
a = 12j + 1
print a
print type(a)
# 除法和求余数
print "5 / 3 = %d" % ( 5 / 3)
print "5 %% 3 = %d" %( 5 % 3)
#进制数字打印
print "%d,%o,%x" %(10,10,10)
#查看变量的类型
a = "hello world"
print a.__class__
#调用变量的方法
print a.split()
Tuple(元组) #!/bin/env python
#coding:utf-8
#除了字符串和数值之外,Python还提供了另外4中重要的基本类型:
#元组、列表、集合和字典。
#元组(tuple) :不可更改的数据序列
a = ("first","second","third")
#输出元组
print (a)
#打印元组的长度
print ("a len : %d" % len(a))
#遍历元组
print ("%s %s %s" % (a[0],a[1],a[2]))
#元组中的一个元素被另一个元组引用
b = (a,"four")
print (b)
print("%s %s %s %s" % (b[0][0],b[0][1],b[0][2],b[1]))
# 元组可以包含各种类型的数据,但是在创建之后,就不能再改变
#元组是不可变的。(字符串在创建之后也是不可变的,那些看起来改变
#他们的操作实际上创建了新的字符串)
#下面的书写将会报错
a[1] = 3
列表 ---可更改数据的值
字典--
#!/bin/env python
#coding:utf-8
#字典---以名称索引的分组数据
dict = {}#空字典
print (dict)
dict["a"] = "1"
dict["b"] = "2"
print (dict)
#从字典里获得它所有的键
key=dict.keys()
print (list(key))
#打印键和值
print ("key[%s] : value[%s]" % (key[0],dict.get(key[0])))
#从字典里获得它所有的值
value=dict.values()
print (list(value))
#字典的工作原理是每个键是不同的(不可以有完全相同的两个键)
#但是可以有多个重复的值
输入如下:
序列相关的一些操作
#coding:utf-8
#type 打印出数据的类型
print type(1)
print type(1.0)
print type("helloworld")
#虚数 如12j
a = 12j + 1
print a
print type(a)
# 除法和求余数
print "5 / 3 = %d" % ( 5 / 3)
print "5 %% 3 = %d" %( 5 % 3)
#进制数字打印
print "%d,%o,%x" %(10,10,10)
#查看变量的类型
a = "hello world"
print a.__class__
#调用变量的方法
print a.split()
Tuple(元组) #!/bin/env python
#coding:utf-8
#除了字符串和数值之外,Python还提供了另外4中重要的基本类型:
#元组、列表、集合和字典。
#元组(tuple) :不可更改的数据序列
a = ("first","second","third")
#输出元组
print (a)
#打印元组的长度
print ("a len : %d" % len(a))
#遍历元组
print ("%s %s %s" % (a[0],a[1],a[2]))
#元组中的一个元素被另一个元组引用
b = (a,"four")
print (b)
print("%s %s %s %s" % (b[0][0],b[0][1],b[0][2],b[1]))
# 元组可以包含各种类型的数据,但是在创建之后,就不能再改变
#元组是不可变的。(字符串在创建之后也是不可变的,那些看起来改变
#他们的操作实际上创建了新的字符串)
#下面的书写将会报错
a[1] = 3
列表 ---可更改数据的值
#!/bin/env python #coding:utf-8 #列表---可更改数据的内容 list = ["coffee","tea","toast"] #list 长度 print ("list len is %d" % (len(list))) #打印list数值 print ("%s,%s,%s" % (list[0],list[1],list[2])) #修改list[2]的数值 list[len(list) - 1] = "sausages" print ("list[2] is %s" % (list[2])) #list 追加 list.append("waffles") print ("list[%d] is %s" % (len(list) - 1,list[len(list) - 1])) #list 一次性追加多个元素 list.extend(["juice","decaf"]) print(list)
字典--
#!/bin/env python
#coding:utf-8
#字典---以名称索引的分组数据
dict = {}#空字典
print (dict)
dict["a"] = "1"
dict["b"] = "2"
print (dict)
#从字典里获得它所有的键
key=dict.keys()
print (list(key))
#打印键和值
print ("key[%s] : value[%s]" % (key[0],dict.get(key[0])))
#从字典里获得它所有的值
value=dict.values()
print (list(value))
#字典的工作原理是每个键是不同的(不可以有完全相同的两个键)
#但是可以有多个重复的值
集合
#!/bin/env python #coding:utf-8 #集合 是不包括重复数据的数据集合 list = ['a','b','a','a','b','b'] print ("list is %s" % (list)) print ("set is %s " % (set(list)))
输入如下:
list is ['a', 'b', 'a', 'a', 'b', 'b'] set is set(['a', 'b'])
序列相关的一些操作
#!/bin/env python #coding:utf-8 #序列的其他共有属性 #字典代表一组数据,但它不是序列,因为它没有衣蛾从头至尾的特定顺序 #1、引用最后一个元素 last_names = ["Dogglass","Jefferson","Williams","Frank"] print ( "last_names len is %d" % (len(last_names))) #python 提供了一个捷径,可以通过使用数值-1访问一个序列的最后一个元素 len = len(last_names) print ("last_names[%d] = %s" % ( len -1,last_names[len -1])) print ("last_names[%d] = %s" % ( -1,last_names[-1])) print ("last_names[%d] = %s" % ( len -2,last_names[len -2])) print ("last_names[%d] = %s" % ( -2,last_names[-2])) #2、序列的范围 #序列分片 slice_me = ("the","next","time","we","meet","drinks","are","on","me") print (slice_me[5:9]) slice_me_list = ["the","next","time","we","meet","drinks","are","on","me"] print (slice_me_list[5:9]) slice_this_string="the next time we meet drinks are on me" print (slice_this_string[5:9]) #通过附加序列增长列表 #append 方法是将一个序列附加到另一个序列的末端 slice_me = ("the","next","time","we","meet","drinks","are","on","me") apartment = [] apartment.append(slice_me) print ("apartment.append(slice_me) is %s" % (apartment)) #extend 方法将给定序列中的么个元素插入到调用它的列表中 apartment=[] apartment.extend(slice_me) print ("apartment.extend(slice_me) is %s" % (apartment)) #使用列表临时存储数据 #为了防止列表变得笨重,可以使用pop方法在处理完列表的一个数据之后 #将其引用从列表中删除。当删除引用之后,它原来在列表的位置将被后续元素 #填上,列表较少的元素个数等于已经弹出的元素个数。 todays_temperatures = [23,32,33,31] todays_temperatures.append(29) print ("berfore todays_temperatures is :%s" % (todays_temperatures)) print ("todays_temperatures.pop(1) is %d" % (todays_temperatures.pop(1))) print ("end todays_temperatures.pop(1) is :%s" % (todays_temperatures))输出如下:
last_names len is 4 last_names[3] = Frank last_names[-1] = Frank last_names[2] = Williams last_names[-2] = Williams ('drinks', 'are', 'on', 'me') ['drinks', 'are', 'on', 'me'] ext apartment.append(slice_me) is [('the', 'next', 'time', 'we', 'meet', 'drinks', 'are', 'on', 'me')] apartment.extend(slice_me) is ['the', 'next', 'time', 'we', 'meet', 'drinks', 'are', 'on', 'me'] berfore todays_temperatures is :[23, 32, 33, 31, 29] todays_temperatures.pop(1) is 32 end todays_temperatures.pop(1) is :[23, 33, 31, 29]
相关文章推荐
- Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
- python入门:基础,列表、元组、字典及集合类型
- Python 学习笔记 -- 变量、元组、列表、字典和集合
- Python变量类型(l整型,长整形,浮点型,复数,列表,元组,字典)学习
- Python变量类型(l整型,长整形,浮点型,复数,列表,元组,字典)学习
- python学习笔记2—python文件类型、变量、数值、字符串、元组、列表、字典
- Day2 Python基础学习——字符串、列表、元组、字典、集合
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- 2.python基础之—列表,元组,字典,集合,字符串的使用方法
- Python3组合数据类型(元组、列表、集合、字典)语法
- python学习六:异常处理、map/reduce/filter内置函数、python集合(set)类型、元组转列表
- 【python学习笔记】Python数据结构(列表,元组,字典,集合)
- Python3.5入门学习记录-列表、元组、字典
- Python之几种重要的基本类型:元组,列表,字典,字符串,集合
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python 学习笔记(3)数据类型2 (元组(tuple),集合,字典)
- python学习-1-列表,元组,字典,集合,文件操作
- Python基础-列表_元组_字典_集合
- python学习-1-列表,元组,字典,集合,文件操作
- Python学习笔记(一):基础语法、变量类型、运算符(快速入门篇)