数据类型初识

 

1.数字

整型 int    

32位机器取值范围 -2**31~2**31-1

64位机器取值范围 -2**64~2**64-1

长整型 long

于机器内存有限,实际使用不可能无限大。

注意:

自动江整数数据转换为长整数

python3没有长整型

 

 

浮点型 float

浮点数用来处理实数,即带有小数点的数字。

3.23和52.3E-4是浮点数的例子。E表示10的幂

 

复数 complex

复数由实数部分和虚数部分组成,一般形式为x+yj,其中x、y是复数的实数部分,j是虚数。eg:(2+3j)

 

 

2.布尔值

真 或 假  ——>1 或 0 ——> Ture 或 False

 

 

3.字符串

字符串格式化输出: 

name = “ww”

print "i am %s " % name

#输出: i am ww

 

python 字符串格式化符号:

%c:格式化字符及其ASCII码

%s:格式化字符串

%d:格式化整数

%u:格式化无符号整型

%o:格式化无符号八进制数

%x:格式化无符号十六进制数

%X:格式化无符号十六进制数(大写)

%f:格式化浮点数字,可指定小数点后的精度

%e:用科学计数法格式化浮点数

%E:作用同%e,用科学计数法格式化浮点数

%g:%f和%e的简写

%G:%F 和 %E 的简写

%p:用十六进制数格式化变量的地址

 

格式化操作符辅助指令: 

*:定义宽度或者小数点精度

-:用做左对齐

+:在正数前面显示加号( + )

<sp>:在正数前面显示空格

#:在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')

0:显示的数字前面填充'0'而不是默认的空格

%:'%%'输出一个单一的'%'

(var):映射变量(字典参数)

m.n.:m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

 

python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。

三引号的语法是一对连续的单引号或者双引号(通常都是成对的用)

 

a = '''hello world
  
author:ww'''
print(a)
#输出
hello world

author:ww

 

  

Python字符串运算符:

a = 'Hello' b = 'World'

+:字符串连接 

print(a+b)#HelloWorld

*:重复输出字符串 

print(a*2)#HelloHello

[]:通过索引获取字符串中字符 

print(a[1])#e

[:]:截取字符串中的一部分

print(a[1:4])#ell

in:成员运算符 - 如果字符串中包含给定的字符返回 

print('H' in a)#True

not in:成员运算符 - 如果字符串中不包含给定的字符返回

print('W' not in a)#Ture

r/R:原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。

print(r'\n')#\n 

print(R'\n')#\n

 

字符串常用内建函数详解+代码举例:

print('------------capitalize-------------')
#str.capitalize():将字符串的第一个字母变成大写,其他字母变小写

print('A,b'.capitalize())
print(' A,b'.capitalize())
print('\nA,b'.capitalize())
print('\rA,b'.capitalize())
print('\tA,b'.capitalize())
print('1,A,b'.capitalize())
# 输出结果:
#-----------------
'''
A,b
a,b

a,b
a,b
    a,b
1,a,b
'''
#-----------------


print('------------count-------------')
#str.count(sub, beg, end):返回 sub 在 string 里面出现的次数,sub -- 搜索的子字符串,start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。end -- 字符串中结束搜索的位置。默认为字符串的最后一个位置。

print('ww ww ww'.count('w'))
print('ww ww ww'.count('w',0,3))
print('ww ww ww'.count('w',0,4))
print('ww ww ww'.count('w',2,4))
print('ww ww ww'.count(' '))
print('ww\nww\nww'.count('\n'))
# 输出结果:
#-----------------
'''
6
2
3
1
2
2
'''
#-----------------

print('------------center-------------')
#str.center(width, fillchar):返回一个原字符串居中,并使用空格填充至长度 width 的新字符串,width -- 返回字符串的总长度,fillchar -- 填充字符。

print('HelloWorld'.center(20))
print('HelloWorld'.center(1))
print('HelloWorld'.center(20,'\n'))
print('ww'.center(9,'*'))
print('www'.center(8,'*'))
#无法使左右字符数相等时候,字符串字符数为奇数时左侧字符会比右侧少 1,字符串字符数为偶数时左侧字符会比右侧多 1
# 输出结果:
#-----------------
'''
    HelloWorld     
HelloWorld





HelloWorld





****ww***
**www***
'''
#-----------------

print('------------endswith-------------')
#str.endswith(suffix, start, end):方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。suffix -- 该参数可以是一个字符串或者是一个元素,start -- 字符串中的开始位置,end -- 字符中结束位置

print('Hello World ww'.endswith('w'))
print('Hello World ww '.endswith('w'))
print('Hello World wW'.endswith('w'))
print('Hello World wW'.endswith('W',0,7))
# 输出结果:
#-----------------
'''
True
False
False
True
'''
#-----------------

print('------------expandtabs-------------')
#str.expandtabs(tabsize=8):该方法返回字符串中的 tab 符号('\t')转为空格后生成的新字符串。tabsize -- 指定转换字符串中的 tab 符号('\t')转为空格的字符数,比如tabsize=8,意思就是将字符串中’\t’转为8个空格。

print('Hello\tWorld'.expandtabs(1))
print('Hello\tWorld'.expandtabs(2))
print('Hello\tWorld'.expandtabs(3))
print('Hello\tWorld'.expandtabs(4))
print('Hello\tWorld'.expandtabs(5))
print('Hello\tWorld'.expandtabs(6))
print('Hello\tWorld'.expandtabs(7))
print('Hello\tWorld'.expandtabs(8))
print('Hello\tWorld'.expandtabs(9))
print('Hello\tWorld'.expandtabs(10))

#参数是长度,从头开始数,数到第一个\t正好为一个长度,不足则补空格,
#如果还有\t,接着数到第二个\t仍然为一个长度,以此类推知道最后一个\t结束

#以上面的例子
#第一个, 长度是1,那么从头开始数,H、e、l、l、o分别为一个长度那个,\t单独为一个1长度,补足一个空格
#第二个, 长度是2,那么重头开始数,He、ll为分别为一个长度,o差一个长度才是2,所以补足一个空格
#第三个, 长度是3,那么重头开始数,Hel为分别为一个长度,lo差一个长度才是3,所以补足一个空格
#第四个, 长度是4,那么重头开始数,Hell为分别为一个长度,l差三个长度才是4,所以补足三个空格
#第五个, 长度是5,那么重头开始数,Hello为分别为一个长度,那么\t单独为一个5长度,补足五个空格
#第六个, 长度是6,那么重头开始数,Hello差一个长度为一个6长度,那么\t补足一个空格
#以此类推

print('---Hello\tWorld\tH\nelloWorld\t'.expandtabs(10))
print('***Hello\tWorld\tHello\rWorld\t'.expandtabs(10))

#如果有\n,则\n后的字符为起点开始下一轮\t的长度起点
#\r:前面的都没了
# 输出结果:
#-----------------
'''
Hello World
Hello World
Hello World
Hello   World
Hello     World
Hello World
Hello  World
Hello   World
Hello    World
Hello     World
---Hello  World     H
elloWorld 
World     
'''
#-----------------

print('------------find-------------')
#str.find(str, beg, end):方法检测字符串中是否包含子字符串 str,如果包含子字符串返回开始的索引值,否则返回-1。str -- 指定检索的字符串,beg -- 开始索引,默认为0,end -- 结束索引,默认为字符串的长度

print('ww ww ww w'.find("w"))
print('ww ww ww w'.find("w",1))
print('ww ww ww w'.find("w",3,3))
print('ww ww ww w'.find("w",50))
# 输出结果:
#-----------------
'''
0
1
-1
-1
'''
#-----------------

print('------------format-------------')
#str.format():字符串格式化,基本语法是通过 {} 和 : 来代替以前的 % 。函数可以接受不限个参数,位置可以不按顺序

print("{} {}".format("hello", "world"))# 不设置指定位置,按默认顺序
print("{0} {1}".format("hello", "world"))# 设置指定位置
print(' {name}  {year}'.format(name='ww',year=23))
print(' {name}  {year}'.format(year=23,name='ww'))
print("name:{0[0]}, age {0[1]}".format(['ww', '24']))#0[1]  这个0相当于['ww', '24']列表,[1]按下标取数据
print(' {name}  {year}'.format_map({'name':'alex','year':12}))
# 输出结果:
#-----------------
'''
hello world
hello world
ww  23
ww  23
name:ww, age 24
alex  12
'''
#-----------------

print('------------isalnum-------------')
#str.isalnum():检测字符串是否由字母和数字组成。如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

print('helloworld'.isalnum())# 字符中没有空格
print('hello world'.isalnum())# 字符有空格
# 输出结果:
#-----------------
'''
True
False
'''
#-----------------

print('------------isalpha-------------')
#str.isalpha():检测字符串是否只由字母组成。如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False

print('HelloWorld'.isalpha())
print('Hello  World'.isalpha())# 字符有空格
print('你好!世界'.isalpha())
print('你好世界'.isalpha())
print('你好世界'.encode('utf-8').isalpha())
# 输出结果:
#-----------------
'''
True
False
False
True
False
'''
#-----------------

print('------------isdecimal-------------')
#str.isdecimal():检查字符串是否只包含十进制字符,这种方法只存在于unicode对象

print("hi2019".isdecimal())
print("2019".isdecimal())
# 输出结果:
#-----------------
'''
False
True
'''
#-----------------

print('------------isdigit-------------')
#str.isdigit():检测字符串是否只由数字组成

print("hi2019".isdigit())
print("2019".isdigit())
print('IV'.isdigit())
# 输出结果:
#-----------------
'''
False
True
False
'''
#-----------------

print('------------isnumeric-------------')
#str.isnumeric():检测字符串是否只由数字组成。这种方法是只针对unicode对象

print("hi2019".isnumeric())
print("2019".isnumeric())
# 输出结果:
#-----------------
'''
False
True
'''
#-----------------

'''
isdigit、isdecimal、isnumeric的区别:

isdigit():

True: Unicode数字,byte数字(单字节),全角数字

(双字节)

False: 汉字数字,罗马数字,小数

Error: 无 

isdecimal() :

True: Unicode数字,全角数字(双字节) 

False: 罗马数字,汉字数字,小数

Error: byte数字(单字节) 

isnumeric() :

True: Unicode数字,全角数字(双字节),罗马数

字,汉字数字 

False: 小数 

Error: byte数字(单字节)
'''

print('------------isidentifier-------------')
#str. isidentifier():判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法

print("if".isidentifier() )
print("def".isidentifier() )
print("class".isidentifier() )
print("_a".isidentifier() )
print("中国123a".isidentifier() )
print("123".isidentifier() )
print("3a".isidentifier() )
print("".isidentifier() )
# 输出结果:
#-----------------
'''
True
True
True
True
True
False
False
False
'''
#-----------------

print('------------istitle-------------')
#str.istitle():如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.

print('Helloworld'.istitle())
print('HelloWorld'.istitle())
print('Hello World'.istitle())
print('Hello world'.istitle())
print('Hello WoRld'.istitle())
# 输出结果:
#-----------------
'''
True
False
True
False
False
'''
#-----------------

print('------------isprintable-------------')
#str. isprintable():如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 False。

print("Hello\tWorld".isprintable())  # 制表符 False
print("Hello\nWorld".isprintable())  # 换行符 False
print('HelloWorld'.isprintable())
print('Hello World'.isprintable())
print('~'.isprintable())
print(''.isprintable())
# 输出结果:
#-----------------
'''
False
False
True
True
True
True
'''
#-----------------

print('------------isupper-------------')
#str.isupper():如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

print('Hello World'.isupper())
print('HELLO WORLD'.isupper())
print('H1234'.isupper())
print('H 1234!~\n'.isupper())
# 输出结果:
#-----------------
'''
False
True
True
True
'''
#-----------------

print('------------join-------------')
#str.join(sequence):用于将序列中的元素以指定的字符连接生成一个新的字符串,返回通过指定字符连接序列中元素后生成的新字符串。sequence -- 要连接的元素序列

print('+'.join(['1','2','3']))
print('--'.join(('1','2','3')))
print('--\n'.join(('1','2','3')))
# 输出结果:
#-----------------
'''
1+2+3
1--2--3
1--
2--
3
'''
#-----------------

print('------------ljust-------------')
#str.ljust(width, fillchar):返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。width -- 指定字符串长度。fillchar -- 填充字符,默认为空格。

print('Hello World'.ljust(15,'*'))
print('Hello World'.ljust(1,'*'))
print('Hello World  '.ljust(15,'*'))
print('   Hello World'.ljust(15,'*'))
# 输出结果:
#-----------------
'''
Hello World****
Hello World
Hello World  **
  Hello World*
'''
#-----------------

print('------------rjust-------------')
#str.rjust(width, fillchar):返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。width -- 指定填充指定字符后中字符串的总长度.fillchar -- 填充的字符,默认为空格。

print('Hello World'.rjust(15,'*'))
print('Hello World'.rjust(1,'*'))
print('Hello World  '.rjust(15,'*'))
print('   Hello World'.rjust(15,'*'))
# 输出结果:
#-----------------
'''
****Hello World
Hello World
**Hello World  
*   Hello World
'''
#-----------------

print('------------lower-------------')
#str.lower():返回将字符串中所有大写字符转换为小写后生成的字符串。

print('Hello World'.lower())
print('Hello 123.0 World \n'.lower())
# 输出结果:
#-----------------
'''
hello world
hello 123.0 world 
'''
#-----------------

print('------------upper-------------')
#str.upper():返回小写字母转为大写字母的字符串。

print('Hello World 12(*&%¥#%(\n'.upper())
# 输出结果:
#-----------------
'''
HELLO WORLD 12(*&%¥#%(
'''
#-----------------

print('------------lstrip-------------')
#str.lstrip([chars]):返回截掉字符串左边的空格或指定字符后生成的新字符串。chars --指定截取的字符

print(' \nHello World'.lstrip())
print('\nHello World'.lstrip())
print('*** Hello World ***'.lstrip('*'))
print(' *** Hello World ***'.lstrip('*'))
print('\n*** Hello World ***'.lstrip('*'))
# 输出结果:
#-----------------
'''
Hello World
Hello World
Hello World ***
*** Hello World ***

*** Hello World ***
'''
#-----------------

print('------------rstrip-------------')
#str.rstrip([chars]):返回删除 string 字符串末尾的指定字符后生成的新字符串。chars -- 指定删除的字符(默认为空格)

print('Hello World \n'.rstrip())
print('Hello World\n '.rstrip())
print('*** Hello World ***'.rstrip('*'))
print('*** Hello World *** '.rstrip('*'))
print('*** Hello World ***\n'.rstrip('*'))
# 输出结果:
#-----------------
'''
Hello World
Hello World
*** Hello World 
*** Hello World *** 
*** Hello World ***

'''
#-----------------

print('------------strip-------------')
#str.strip([chars]):返回移除字符串头尾指定的字符生成的新字符串。chars -- 移除字符串头尾指定的字符序列。

print(' \n Hello World \n '.strip())
print(' Hello World\n'.strip())
print(' *** Hello World *** '.strip('*'))
print('\n*** Hello World ***\n'.strip('*'))
# 输出结果:
#-----------------
'''
Hello World
Hello World
*** Hello World *** 

*** Hello World ***

'''
#-----------------

print('------------maketrans-------------')
#str.maketrans(intab, outtab):用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。返回字符串转换后生成的新字符串。intab -- 字符串中要替代的字符组成的字符串、outtab -- 相应的映射字符的字符串。
# 注:两个字符串的长度必须相同,为一一对应的关系。
#str.translate(table, deletechars):根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。table -- 翻译表,翻译表是通过maketrans方法转换而来。deletechars -- 字符串中要过滤的字符列表。(python3中只接受一个参数,deletechars没有这个。)

p = str.maketrans("abcdefli",'12345678')#长度必须一致
print('dsadwpod'.translate(p))#大概意思就是将字符串中的a转为1、b转为2...依次类推
# 输出结果:
#-----------------
'''
4s14wpo4
'''
#-----------------

print('------------replace-------------')
#str.replace(old, new, max):把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次,返回新字符串。old -- 将被替换的子字符串。new -- 新字符串,用于替换old子字符串。max -- 可选字符串, 替换不超过 max 次

print('w w wwww'.replace('w','L',1))
print(' wwwwww'.replace('w','L',3))
print(' ww ww ww w'.replace('ww','LL',4))
print('i am a god i am a god'.replace('am','ww',3))
# 输出结果:
#-----------------
'''
L w wwww
LLLwww
LL LL LL w
i ww a god i ww a god
'''
#-----------------

print('------------rfind-------------')
#str.rfind(str, beg, end):返回字符串最后一次出现的位置,如果没有匹配项则返回-1。str -- 查找的字符串。beg -- 开始查找的位置,默认为 0。end -- 结束查找位置,默认为字符串的长度。

print('ww ww ww w'.rfind('w'))
print('ww ww ww w'.rfind('ww'))
print('ww ww ww w'.rfind('w',0,4))
print('ww ww ww w'.rfind('ww',0,4))
print('ww ww ww w'.rfind('ww',0,5))
# 输出结果:
#-----------------
'''
9
6
3
0
3
'''
#-----------------

print('------------split-------------')
#str.split(str, num):通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串,返回分割后的字符串列表。str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。num -- 分割次数。默认为 -1, 即分隔所有。

print('ww \n\tww \n\tww'.split())
print('ww \n\tww \n\tww'.split(" ",-1))
print('ww \n\tww \n\tww'.split(" ",1))
# 输出结果:
#-----------------
'''
['ww', 'ww', 'ww']
['ww', '\n\tww', '\n\tww']
['ww', '\n\tww \n\tww']
'''
#-----------------

print('------------splitlines-------------')
#str.splitlines([keepends]):按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。keepends -- 在输出结果里是否保留换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符。

print('ab c\n\nde fg\rhi\r\n'.splitlines())
print('ab c\n\nde fg\rhi\r\n'.splitlines(True))
# 输出结果:
#-----------------
'''
['ab c', '', 'de fg', 'hi']
['ab c\n', '\n', 'de fg\r', 'hi\r\n']
'''
#-----------------

print('------------swapcase-------------')
#str.swapcase():返回大小写字母转换后生成的新字符串。

print('*@&*(……¥ Hello World \n\t\r?'.swapcase())
# 输出结果:
#-----------------
'''
*@&*(……¥ hELLO wORLD 
?
'''
#-----------------

print('------------title-------------')
#str.title():返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写。

print('Hello wOrld \nhWLLo World'.title())
# 输出结果:
#-----------------
'''
Hello World 
Hwllo World
'''
#-----------------

print('------------zfill-------------')
#str.zfill(width):返回指定长度的字符串,原字符串右对齐,前面填充0。width -- 指定字符串的长度。原字符串右对齐,前面填充0。

print('\n\t\rwwww'.zfill(10))#\n、\t、\r算一个长度
# 输出结果:
#-----------------
'''
000
wwww
'''
#-----------------

print( '-----------------------------')

暂时整理这么多,下章继续整理元组、字典、列表。