python集合类型变量_Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)...

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

#列表---可更改数据的内容

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值