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)
#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))
#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]
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中元组/列表/字典/集合
转自:https://blog.csdn.net/lobo_seeworld/article/details/79404566
- Python入门学习:1.变量和简单的数据类型
python入门学习:1.变量和简单的数据类型 关键点:变量.字符串.数字 1.1 变量的命名和使用1.2 字符串1.3 数字1.4 注释 1.1 变量的命名和使用 变量,顾名思义是一个可变的量, ...
- python入门学习:7.函数
python入门学习:7.函数 关键点:函数 7.1 定义函数7.2 传递实参7.3 返回值7.4 传递列表7.5 传递任意数量的实参7.6 将函数存储在模块中 7.1 定义函数 使用关键字def ...
- python入门学习:3.操作列表
python入门学习:3.操作列表 关键点:列表 3.1 遍历整个列表3.2 创建数值列表3.3 使用列表3.4 元组 3.1 遍历整个列表 循环这种概念很重要,因为它是计算机自动完成重复工作的常 ...
- day04 python入门(变量,基本数据类型)
python入门学习 来自egon的学习套路 在每次遇到一个新事物的时候,要学三步: xxx是什么? 为什么要有xxx? 大前提:python中所有出现的语法都是为了让计算机能够具有人的某一个功能 ...
- python入门学习:9.文件和异常
python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件 首先创建一个pi_ ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- python入门学习:4.if语句
python入门学习:4.if语句 关键点:判断 4.1 一个简单的测试4.2 条件测试4.3 if语句 4.1 一个简单的测试 if语句基本格式如下,注意不要漏了冒号 1if 条件 :2 ...
- python入门学习:2.列表简介
python入门学习:2.列表简介 关键点:列表 2.1 列表是什么2.2 修改.添加和删除元素2.3 组织列表 2.1 列表是什么 列表,是由一系列按特定顺序排列的元素组成.你可以创建包含字母表 ...
随机推荐
- 小学生都能学会的python(函数)
小学生都能学会的python(函数) 神马是函数 函数: 对功能或者动作的封装 函数的定义 def 函数名(形参列表): 函数体(return) ret = 函数名(实参列表) 函数的返回值 retu ...
- 推荐几款常用的Eclipse插件
Eclipse 应该说是老牌也是最常用的Java开发工具,尽管这几年 InstelliJ IDEA 的发展势头很强劲,身边使用和推崇的人也大有人在,但个人而言还是觉有些不太习惯.这里也介绍几款自己常用 ...
- .conf、.bak是什么格式
1..conf 是config的简写,也就是配置文件,多用于存取硬件驱动程序的安装配置信息.内容一般是一些硬件的版本号呀,支持什么样的系统等信息.本质上来说就是TXT文件,里面的格式没有统一标准,各个 ...
- linux 文件的权限说明
1.开头 d:表示问文件夹 ( directory ) -:表示普通二进制文件 ( 压缩包.文件 等等 ) l:表示软连接文件 ( link 硬链接或软链接 ) 2.权限 ( 3 个为一组 ) r:读 ...
- Springboot 应用启动分析
https://blog.csdn.net/hengyunabc/article/details/50120001#comments 一,spring boot quick start 在spring ...
- WinServer-IIS-woff字体不显示问题
ASP.NET mvc发布到IIS之后,访问网站的时候,发现woff字体没有加载 百度发现很多博客上的教程是这样的,在IIS管理器中的MIME选项中添加类型 但是重新使用IIS发布后,新添加的字体就会 ...
- 数据库-mongodb-聚合与map reduce
分组统计:group() 简单聚合:aggregate() 强大统计:mapReduce() Group函数: 1.不支持集群.分片,无法分布式计算 2.需要手写聚合函数的业务逻辑 curr指当前行, ...
- Javaee 应用分层架构
应用分层的优点:修改方便,仅修改有问题的那层以及其相邻几层即可,层数越多,其相应的资源分配也会更加平均 缺点:耗费时间,速度慢,调用占用大量堆栈. JAVAEE的分层: 4层分法:1.客户层:运行在客 ...
- Git-删除本地文件夹的repository(本地仓库)
安装git软件后.有些文件夹里会出现带有?的图标,右键菜单上会有"Git-Sync"或者"Git-Commit"等命令:正常的应该是"Git-Clon ...
- CoreData 从入门到精通(四)并发操作
通常情况下,CoreData 的增删改查操作都在主线程上执行,那么对数据库的操作就会影响到 UI 操作,这在操作的数据量比较小的时候,执行的速度很快,我们也不会察觉到对 UI 的影响,但是当数据量特别 ...