python学习第一天基本数据类型[Python基础]
1.Number
(1) 整数:int
(2)浮点数:float(无单精度和双精度之分)
计算注意点:
2/2=1.0
type(2/2)》》》
<class"float">
1+1=2
1+1.0 = 2.0
如果想要除法得到整数:
python;gutter:true;">type(2//2)》》》<class "int">
1//2
》》》0
单斜杠是除法,自动转为浮点型数据,双斜杠表示整除,只会保留整数部分
关于进制,同其他 语言相同:
二进制:0b10 代表十进制的2
八进制:0b10 代表十进制的8
十六进制:0bff 代表十进制的255
进制转换法:
bin(x): 把其他进制数字转化为二进制
int(x):把其他进制数字转化为十进制
hex(x):把其他进制数字转化为十六进制
oct(x):把其他进制数字转化为八进制
复数(Complex):如36j
2.bool 布尔值
bool也是Number的一种
与javascript不同的是, bool([])的结果是false, 而javascript中Boolean([])为true
同时:所有的空值都是转化为false,如0, 空对象, ‘’,[]
另外一个可以转为false的是None
3.单引号与双引号和三引号
单引号和双引号表示字符串,三引号字符串中可以用回车来表示换行
"""hello worldhello world
hello world"""
当然也可以使用三个单引号。
输出结果是: "hello world
hello world
hello world"
"""hello worldhello world
hello world
"""
以上代码的输出结果是:
"hello worldhello world
hello world
"
也就是说引号并不解析转义字符串。
我们可以通过print去打印,此时转义字符会被解析
在print时, 为了不让部分字符转义,我们可以在字符串前加r
>>> print(r"c:orthword
orthEast
")c:
orthword
orthEast
>>> print("c:orthword
orthEast
")c:
orthword
orthEast
加入r或者R后,这个字符串就不是普通字符串了,而是一个原始字符串。
4.字符串运算
与javascript相同,字符串可以通过方括号来获取第n位的字符":
"helloworld
"[0] == "h"
但是与javascript不同的是,在python中,可以有负序号:
"helloworld
"[-1] == "d" //True"helloworld
"[-13] //>>> "helloworld
"[-13] //>>>Traceback (most recent call last): //>>> File "<pyshell#0>", line 1, in <module>//>>> "hello
world"[-13]
//>>>IndexError: string index out of range
//超出范围报错
而在JavaScript中,
"helloworld
"[-1] //undefined
字符串截取与拼接:
"hello world"[5:7] //" w""hello world"[0:-2] //"hello wor"
"hello world"[-1:-2] // ""
"hello world"[-3:-2] //"r"
"hello world"[-3:] //"rld"
"hello world"[:-3] //"hello wo"
"sdfd" * 3 //"sdfdsdfdsdfd"
5.列表-- list
列表访问
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"] //["新月打击", "苍白之瀑", "月之降临", "月神冲刺"] >>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][0]//"新月打击">>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][1]
//"苍白之瀑"
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][:1]
//["新月打击"]
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][:3]
//["新月打击", "苍白之瀑", "月之降临"]
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][1:]
//["苍白之瀑", "月之降临", "月神冲刺"]
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][-1]
//"月神冲刺">>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][-1, -2]
//[]
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"][-2: -1]
//["月之降临"]
列表拼接:
>>> ["新月打击", "苍白之瀑", "月之降临", "月神冲刺"] + ["月之降临"] //["新月打击", "苍白之瀑", "月之降临", "月神冲刺", "月之降临"][
"新月打击", "苍白之瀑", "月之降临", "月神冲刺"]*2//["新月打击", "苍白之瀑", "月之降临", "月神冲刺", "新月打击", "苍白之瀑", "月之降临", "月神冲刺"]
6.元组:---tuple
(1,2,‘T’, True)
获取元组的某一项或某几项都是与数组一样的,包括拼接,*(乘法)等
type((1,2,3)) //tuple type((1)) //int --- 元组中只有一个元素时,就会获取到该元素的类型type((True))
//<class"bool">type(([
1,2,]))//<class"list">type((
1,)) //<class"tuple">type(())
//<class"tuple">type([
1]) //<class"list">
7.序列
在python中,list和tuple不叫组,而是叫做序列,str也是序列
字符串,数组,元组的截取叫做切片
"hello world"[0:8:2] //"hlow""hello world"[0:8:3]
//"hlw"
由以上代码可以总结, 对于str【x:y:z】从第x+1个起(从序号为x的开始),每隔z-1个获取一个值,一直到第y个为止,最后讲这些获取的拼接在一起。
判断在序列中是否存在:
>>> "a"in"asfsdaf"True
>>> "3"in"345dfdfdf"True
>>> 3 in (123, 4, 5)False
>>> 3 in (123, 3, 5)True
3 in"233dddd"//报错,3是int类型 ,不能在字符串中寻找 3 in ["3", 4] //False 类型不同
3 notin [1,2,3]//False
获取序列的长度:
>>> len([1,2,3,4,5,6]) 6>>> len((1, "3", "a", True))
4
>>> len("d1fdf")
5
取最值:
>>> max((1,2,3,50,800,2)) 800>>> max([1,2,3,50,800,2])
800
>>> max("hello world")
"w"
>>> min("helloworld")
"d"
>>>min("helloworld345")
"3"
>>> max(["3", "cd"])
"cd"
>>> max([3, "cd"])
//Traceback (most recent call last):
// File "<pyshell#13>", line 1, in <module>
// max([3, "cd"])
//TypeError: ">"not supported between instances of "str"and"int" //类型不同不可比较
获取字符的ASCII 码
>>> ord("cc")Traceback (most recent call last):
File
"<pyshell#0>", line 1, in <module>ord(
"cc")TypeError: ord() expected a character, but string of length
2 found >>> ord("c") 99
8.集合:{}
set 无序的
type({1,23,5,6,8,9}) //<class"set">{
1,3,5,48,5,9,1,1,5,64,85} //{64, 1, 3, 5, 9, 48, 85} 重复的会被剔除
集合支持的操作: len(), in
>>> 15 notin {1,3,5,48,5,9,1,1,5,64,85}True
>>> 3 in {1,3,5,48,5,9,1,1,5,64,85}True
元素剔除:
-:求两个集合的差集
>>> {1,2,3,4,5,6} - {3, 4}{
1, 2, 5, 6} >>> {1,2,3,4,5,6} - {3, 4,7,8,9}{
1, 2, 5, 6}
&: 求两个集合的公共部分(交集)
>>> {1,2,3,4,5,6} & {3, 4}{
3, 4}>>> {1,2,3,4,5,6} & {3, 4,7,8,9}{
3, 4}
| : 并集
>>> {1,2,3,4,5,6} | {3, 4,7,8,9} {
1, 2, 3, 4, 5, 6, 7, 8, 9}
类型:
type({}) //<class"dict">type(set())
//<class"set">set({
1,3,5,4})//{1, 3, 4, 5}type({
123,132,}) //<class"set">
9.字典:dict
type({1:1,2:2})//<class"dict">
查询:
{"Q": "新月打击", "W": "苍白之瀑","E": "月之降临", "R": "月神冲刺"}["Q"]//"新月打击" {"Q": "新月打击", "Q": "苍白之瀑","E": "月之降临", "R": "月神冲刺"}["Q"] //"苍白之瀑" ---- 注意如有相同的key,则取最后一个,这是为啥呢? >>> {"Q": "新月打击", "Q": "苍白之瀑","E": "月之降临", "R": "月神冲刺"} //{"Q": "苍白之瀑", "E": "月之降临", "R": "月神冲刺"} >>> {1: "新月打击", "1": "苍白之瀑","E": {1:1}, "R": "月神冲刺"} //{1: "新月打击", "1": "苍白之瀑", "E": {1: 1}, "R": "月神冲刺"}
不可变类型才能当做key:
>>> {[1,2]: "新月打击", "1": "苍白之瀑", "E": {1: 1}, "R": "月神冲刺"}Traceback (most recent call last): File
"<pyshell#2>", line 1, in <module>{[
1,2]: "新月打击", "1": "苍白之瀑", "E": {1: 1}, "R": "月神冲刺"} TypeError: unhashable type: "list">>> {True: "新月打击", "1": "苍白之瀑", "E": {1: 1}, "R": "月神冲刺"} {True: "新月打击", "1": "苍白之瀑", "E": {1: 1}, "R": "月神冲刺"}
life is short, i use python
以上是 python学习第一天基本数据类型[Python基础] 的全部内容, 来源链接: utcz.com/z/530815.html