数据类型

一、字符
1、字符串:
单引号(‘),双引号("),三引号('")是等价的
2、print函数:在屏幕上打印文本
转义字符:\:
\n:换行
串联两个字符串:当需要把两个或多个字符串连在一起时:
1>用加号+:“John" + "Everyman" ---->JohnEveryman
2>不用加号+:“John" "Everyman" ---->JohnEveryman
>>>“John" +“ ” + "Everyman" ---->John Everyman
>>>“John" +“.” + "Everyman" ---->John.Everyman
3>用Print函数:print("John",“Everyman") ---->John Everyman
4>使用格式说明符(占位符):
>>>“John Q.% s%" % ("Public") ---->John Q. public
>>>“John s%s%" % ("Every","Man") ---->John EveryMan
s%格式说明符,对应于特定的数据类型;%符号表明其后面的所有值会被插入到字符串中相应的格式说明符处
>>>"%s %10s %s" % ("John","woman","man") ---->John woman man (第2个是长度为10的字符串,空格在左边)
>>>"%s %-10s %s" % ("John","woman","man") ---->John woman man (第2个是长度为10的字符串,空格在右边)
二、数值类型
整型,浮点型,虚数
确定数值的类型:type函数
>>>type(10) ----> <type 'int'>
>>>type(10.0) ----> <type 'float'>
>>>type(10j+1) ----> <type 'complex'>
字符串中包含不同的数字:
>>>"input words with %%d like this: %d" % 10 ----> input words with %d like this: 10
>>>"a normal float with %%f: %f" % 0.1234 ----> a normal float with %f: 0.123400
>>>"controlling the number of decimal place shown: %.02f" % 1.123456 ----> controlling the number of decimal place shown: 1.12
在字符串中将%转义:连续使用2个%%
基本算术运算:+,-,*,/,%(取余)
%f格式说明符:
>>>print("%3f" % (321.2345678)) ----> 321.235678
>>>print("%.3f" % (321.2345678)) ----> 321.235
>>>print("%0.3f" % (321.2345678)) ----> 321.235
三、变量
1>给名称赋值
>>>first_string="this is one"
>>>second_string="this is two"
>>>first_number=4
>>>second_number=5
>>>print("the variables are %s,%s,%d,%d" % (first_string,second_string,first_number,second_number))
----> the variables are this is one,this is two,4,5
>>>print(first_string) ----> this is one
>>>print("first_string") ----> first_string
2>更改已赋值的名称
>>>probe="the file"
>>>probe=probe + "saved’
>>>print(probe) ----> the file saved
>>>number=1
>>>number=number+2
>>>print(number) ----> 3
>>>one=1
>>>two=one>>>print(two) ---->1
特殊的名称:and,as,break,class,def,del,is,not,none,exec,pass,with,or........,不能用于变量
不能以数字,多数非字母字符如逗号(,),加减(+-),斜杠(\)等开头
id(变量名1),id(变量名2) ----查看内存地址
--------------------------------------------------------------------------
内置类型:
元组,列表,集合,字典
1、元组————不可更改的数据序列
元组用括号( )创建
元组是值的序列,其中每个值都可以被单独访问。
元组创建和使用:
元组包含对数据的引用,如对数值和字符串的引用
filler=("string","filled","by a","tuple")
print("A %s %s %s %s" % ("string","filled","by a","tuple")) ----> A string filled by a tuple
print(filler) ----> ('string', 'filled', 'by a', 'tuple')
访问元组中的单个值:
print("the first element of filler is %s" % filler[0]) ----> the first element of filler is string
查看元组中包含的元素的个数:
print("%d" % len(filler)) ----> 4
通过一个元组访问另一个元组:
a=("one","two","three","four")
b=(a,"first","second","third")
print("%s" % b[1]) ----> first
print("%s" % b[0][0]) ----> one
print("%s" % b[0][2]) ----> three
注:先遵循元组b中第1个元素的引用,之后遵循第2层元组中的每个值的引用。
若要创建只有一个元素的元组,必须在该元素后加个逗号(,),如a=("one",),否则就成了一个字符串
元组可以包含各种类型的数据,但在创建后不能改变;元组是不可变的,元组中的元素不可被修改
------------------------------
2、列表————可更改的数据序列
列表用方括号[ ]创建,可以访问列表中的单个元素:
a=["one","two","three","four"]
count=0
print("first element is %s" % a[count]) ----> first element is one
列表在创建后可以修改,还可以向列表中添加元素
---append只能每次向列表末端添加1个元素
---extend可一次性向列表末端添加多个元素
a.append("five")
count=4
print("the five element is %s" %a[count]) ----> the five element is five
列表长度也可通过len函数确定
------------------------------
3、字典————以名称索引的分组数据
字典类似于元组和列表,是包含一组数据的另一种容器。字典是用选择的名称索引,这些名称可以使字母,数值,字符串,符号等;字典是一组数据,但不是序列,没有特定的顺序。
字典用方括号{ }创建
filler_specials={} --创建一个空字典
filler_specials["a"]="one" --print(filler_specials) ----> {'a': 'one'}
filler_specials["b"]="two"
filler_specials["c"]="three"
print("%s" % filler_specials["a"]) ----> one
通过名称的引用,对其实例化:将索引的名称放在方括号内,将通过该索引引用的值放在等号右边。字典中的索引的名称叫键,对应的值叫值。
创建一个完全指定的字典:filler_specials={"a" : "one","b" : "two","c" : "three"}
print(filler_specials) ----> {'a': 'one', 'b': 'two', 'c': 'three'}
若键是字符串,需要用引号,若是数值,直接使用即可。
从字典中获取键:
filler_specials={"a" : "one","b" : "two","c" : "three"}
filler_key=filler_specials.keys()
filler_value=filler_specials.values()
print(list(filler_key)) ----> ['a', 'b', 'c']
print(list(filler_value)) ----> ['one', 'two', 'three']
filler_specials.get("a") ----> 'one'
注:字典中不可以有两个相同的键。
像列表一样处理字符串————分片
last_names=["Douglass","Jefferson","Willmass"]
print("%s" % last_names[0]) ----> Douglass
print("%s" % last_names[0][2]) ----> u
by_letter={}
by_letter[last_names[0][0]]=last_names[0]
by_letter[last_names[1][0]]=last_names[1]
by_letter[last_names[2][0]]=last_names[2]
--------------------------------------------
4、特殊类型
特殊的内置值---None,True,False
Node:只是一个名称,除了它本身以外没有其它任何对象可与之匹配,如当函数没有可以返回的值,即当函数没有响应的动作时,将返回None;
True和False用数值0和1表示
序列其它属性
1>引用最后一个元素:
last_names=["Douglass","Jefferson","Willmass"]
len(last_names) ----> 3
last_element=len(last_names)-1
print("%s" % last_names[last_element]) ----> Willmass
print("%s" % last_names[-1]) ----> Willmass
print("%s" % last_names[-2]) ----> Jefferson
2>序列的范围
获取序列的一部分,从中提取一个片段,创建可以单独使用的一个副本————分片
将序列分片:
multi=("one","two","three","four","five","six","serven","eight","nine")
sliced_tuple=multi[5:9]
print(sliced_tuple) ----> ('six', 'serven', 'eight', 'nine')
multi=["one","two","three","four","five","six","serven","eight","nine"]
sliced_tuple=multi[5:9]
print(sliced_tuple) ---- ['six', 'serven', 'eight', 'nine']
multi="one two three four five six serven eight nine"
sliced_tuple=multi[5:9]
print(sliced_tuple) ----> wo t
multi="one-two-three-four-five-six-serven-eight-nine"
sliced_tuple=multi[5:9]
print(sliced_tuple) ----> wo-t
3>通过附加序列增长列表:
a=["one","two","three","four"]
b=("first","second","third")
a.extend(b)
print(a) ----> ['one', 'two', 'three', 'four', 'first', 'second', 'third']
集合
集合只有键,而没有相应的值;集合是不包括重复数据的数据集
集合有可变集合和不可变集合:可变集合可以增加,删除或改变其元素;不可变集合被设定后不能被更改。
删除重复的元素:
alpha=["a","b","b","c","d","e","f","f","g"]
print(alpha) ----> ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'f', 'g']
aplha2=set(alpha)
print(aplha2) ----> {'f', 'g', 'e', 'a', 'd', 'b', 'c'}
posted @ 2018-08-06 21:13  Sky-wings  阅读(243)  评论(0编辑  收藏  举报