返回 导航

Python

hangge.com

Python - 基本数据类型详解1(整型、浮点型、布尔型、字符串)

作者:hangge | 2022-04-29 09:10

一、整型(int)

1,基本用法

Python 3 里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long
注意Python 整数的取值范围是无限的,不管多大或者多小的数字,Python 都能轻松处理。当所用数值超过计算机自身的计算能力时,Python 会自动转用高精度计算(大数计算)。
a = 10
b = 4

print(a + b) #加法 14
print(a - b) #减法 6
print(a * b) #乘法 40
print(a / b) #除法 2.5
print(a // b) #取整除法 2
print(a % b) #取余 2
print(a ** b) #幂(乘方) 10000


2,整数的不同进制

Python 中,除了可以使用十进制形式外,还可以使用其它多种进制形式来表示整数:
a = 10 #十进制
b = 0b1010 #二进制
c = 0o12 #八进制
d = 0xa #十六进制

print(a, b, c, d)

3,数字分隔符

为了提高数字的可读性,Python 允许使用下划线 _ 作为数字的分隔符,下划线不会影响数字本身的值:
a = 123_456_789
b = 1_2345_6789
print(a, b)


二、浮点型(float)

1,基本用法

浮点数就是除整数以外的其他数字,包括正浮点数、负浮点数:
a = 10.2
b = 2

print(a + b) #加法 12.2
print(a - b) #减法 8.2
print(a * b) #乘法 20.4
print(a / b) #除法 5.1
print(a // b) #取整除法 5.0
print(a % b) #取余 0.1999999999999993
print(a ** b) #幂(乘方) 104.03999999999999

2,指数表示形式

Python 也支持使用指数形式表示一个浮点数:
a = 22.1E1  # 22.1 x 10^1
b = 44E-2  # 44 x 10^-2
print(a, b)


三、布尔型(bool)

1,基本用法

布尔值只有两种状态 True(正确的)False(错误的),Python 3 中,boolint 的子类。
is_top = True
is_close = False

#打印类型
print(type(is_top), type(is_close))

#如果is_top为true
if is_top:
    print('top is true')

#如果is_close为false
if not is_close:
    print('close is false')

2,布尔运算符

下面是 andornot 这三个布尔运算符的使用样例:
注意:布尔运算的优先级低于表达式,not a == b 相当于 not (a == b), 若 a == not b 就会有语法错误
a = True
b = False

print("a and b:", a and b)
print("a or b:", a or b)
print("not a:", not a)

3,类型转换

Python 中所有数据类型都可以转换成布尔值,其中 0None[]{} 会转为 False,其他均为 True
print(bool(0), bool(None), bool([]), bool({}))  #均为False
print(bool(1), bool(-3), bool([13]), bool({'hangge'}))  #均为True

四、字符串(string)

1,字符串定义

(1)定义一个字符串由如下4种方式:
str1 = 'welcome to hangge.com'
str2 = "welcome to hangge.com"
str3 = '''welcome to hangge.com'''
str4 = """welcome to hangge.com"""

print(str1)
print(str2)
print(str3)
print(str4)

(2)前面两种方式如果字符串内部包含引号的话需要反斜杠 \ 转义;后面两种方式除了首尾位置外,内部如果包含引号的话可以不需要转义(如果加了反斜杠 \ 也没关系)
str1 = 'welcome \'to\' hangge.com'
str2 = "welcome \"to\" hangge.com"
str3 = '''welcome 'to' hangge.com'''
str4 = """welcome "to" hangge.com"""

print(str1)
print(str2)
print(str3)
print(str4)

2,字符串截取

字符串截取的语法格式为:变量[头下标:尾下标]
str = 'hangge.com'
print (str[0:-1])    # 输出第一个到倒数第二个的所有字符  hangge.co
print (str[0])       # 输出字符串第一个字符  h
print (str[2:5])     # 输出从第三个开始到第五个的字符  ngg
print (str[2:])      # 输出从第三个开始的后的所有字符  ngge.com

3,字符串拼接

(1)加号 + 可以进行字符串的连接:
str = 'welecome ' + 'to ' + 'hangge.com'

(2)乘号 * 可以重复字符串:
str = 'welecome ' * 3
print (str)    # welecome welecome welecome 

4,判断是否包含某字符串

使用 in not in 可以判断字符是否存在:
str = "abcde"

print("ab" in str)  # True
print("ac" in str)  # False

print("ab" not in str)  # False
print("ac" not in str)  # True

5,常用方法

    Python 中字符串对象提供了很多内置方法来操作字符串,在 Python 中字符串是不可变的,所以字符串所有相关的方法都不会改变原有字符串,都是返回一个新的字符串。
str = "welcome to hangge.com"
print(str)
print("\n--- 字符串长度 ---")
print(len(str))

print("\n--- 从左侧开始查找某个子串(可指定开始结束位置) ---")
print(str.find('hello'))  # -1
print(str.find('com'))  # 3
print(str.find('com', 10))  # 18
print(str.find('com', 10, 21))  # 18

print("\n--- 从右侧开始查找某个子串(可指定开始结束位置) ---")
print(str.rfind('hello'))  # -1
print(str.rfind('com'))  # 3
print(str.rfind('com', 10))  # 18
print(str.rfind('com', 10, 21))  # 18

print("\n--- 查找子串出现的个数(可指定开始结束位置) ---")
print(str.count('hello'))  # 0
print(str.count('com'))  # 2
print(str.count('com', 10))  # 1
print(str.count('com', 10, 21))  # 1

print("\n--- 字符串替换(可指定替换次数) ---")
print(str.replace('com', 'XXX'))  # welXXXe to hangge.XXX
print(str.replace('com', 'XXX', 1))  # welXXXe to hangge.com

print("\n--- 分割字符串转为列表(可指定分割字符出现次数) ---")
print(str.split(' '))  # ['welcome', 'to', 'hangge.com']
print(str.split(' ', 1))  # ['welcome', 'to hangge.com']

print("\n--- 根据指定分隔符将字符串拆分为包含三个元素的元组 ---")
print(str.partition(' '))  # ('welcome', ' ', 'to hangge.com')

print("\n--- 将列表转为字符串 ---")
print("-".join(['welcome', 'to', 'hangge.com']))  # welcome-to-hangge.com

print("\n--- 将字符串第一个字符转为大写 ---")
print(str.capitalize())  # Welcome to hangge.com

print("\n--- 将字符串中每个单词首字母大写 ---")
print(str.title())  # Welcome to Hangge.com

print("\n--- 将字符串所有字符转为大写 ---")
print(str.upper())  # WELCOME TO HANGGE.COM

print("\n--- 将字符串所有字符转为小写 ---")
print(str.lower())  # welcome to hangge.com

print("\n--- 删除字符串两侧空格 ---")
print(" hangge.com ".strip())  #hangge.com

print("\n--- 删除字符串左侧空格 ---")
print(" hangge.com ".lstrip())  #hangge.com 

print("\n--- 删除字符串右侧空格 ---")
print(" hangge.com ".rstrip())  # hangge.com

print("\n--- 左对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---")
print("hangge.com".ljust(20))  # hangge.com********
print("hangge.com".ljust(20, '*'))  # hangge.com********

print("\n--- 右对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---")
print("hangge.com".rjust(20))  #          hangge.com
print("hangge.com".rjust(20, '*'))  # ********hangge.com

print("\n--- 居中对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---")
print("hangge.com".center(20))  #      hangge.com     
print("hangge.com".center(20, '*'))  # *****hangge.com*****

print("\n--- 判断是否是以指定字符串开头 ---")
print(str.startswith("welcome"))  # True

print("\n--- 判断是否是以指定字符串结尾 ---")
print(str.endswith("welcome"))  # False

print("\n--- 判断字符串是否只包含数字 ---")
print("123".isdigit())  # True
print("123.456".isdigit())  # False
print("hangge".isdigit())  # False

print("\n--- 判断字符串是否至少有一个字符并且所有字符都是字母或数字 ---")
print("123hangge".isalnum())  # True
print("123hangge.com".isalnum())  # False

print("\n--- 判断字符串是否至少有一个字符并且所有字符都是字母或中文字 ---")
print("航歌hangge".isalpha())  # True
print("航歌hangge.com".isalpha())  # False

print("\n--- 判断字符串是否只包含空白 ---")
print("    ".isspace())  # True
print("   hangge".isspace())  # False

print("\n--- 将字符串按照指定方式编码 ---")
print("航歌".encode("utf-8"))  # b'\xe8\x88\xaa\xe6\xad\x8c'

print("\n--- 将字符串按照指定方式解码 ---")
print(b"\xe8\x88\xaa\xe6\xad\x8c".decode("utf-8"))  # 航歌

welcome to hangge.com

--- 字符串长度 ---
21

--- 从左侧开始查找某个子串(可指定开始结束位置) ---
-1
3
18
18

--- 从右侧开始查找某个子串(可指定开始结束位置) ---
-1
18
18
18

--- 查找子串出现的个数(可指定开始结束位置) ---
0
2
1
1

--- 字符串替换(可指定替换次数) ---
welXXXe to hangge.XXX
welXXXe to hangge.com

--- 分割字符串转为列表(可指定分割字符出现次数) ---
['welcome', 'to', 'hangge.com']
['welcome', 'to hangge.com']

--- 根据指定分隔符将字符串拆分为包含三个元素的元组 ---
('welcome', ' ', 'to hangge.com')

--- 将列表转为字符串 ---
welcome-to-hangge.com

--- 将字符串第一个字符转为大写 ---
Welcome to hangge.com

--- 将字符串中每个单词首字母大写 ---
Welcome To Hangge.Com

--- 将字符串所有字符转为大写 ---
WELCOME TO HANGGE.COM

--- 将字符串所有字符转为小写 ---
welcome to hangge.com

--- 删除字符串两侧空格 ---
hangge.com

--- 删除字符串左侧空格 ---
hangge.com 

--- 删除字符串右侧空格 ---
 hangge.com

--- 左对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---
hangge.com          
hangge.com**********

--- 右对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---
          hangge.com
**********hangge.com

--- 居中对齐使用指定字符串填充至指定长度(未指定填充字符默认为空格) ---
     hangge.com     
*****hangge.com*****

--- 判断是否是以指定字符串开头 ---
True

--- 判断是否是以指定字符串结尾 ---
False

--- 判断字符串是否只包含数字 ---
True
False
False

--- 判断字符串是否至少有一个字符并且所有字符都是字母或数字 ---
True
False

--- 判断字符串是否至少有一个字符并且所有字符都是字母或中文字 ---
True
False

--- 判断字符串是否只包含空白 ---
True
False

--- 将字符串按照指定方式编码 ---
b'\xe8\x88\xaa\xe6\xad\x8c'

--- 将字符串按照指定方式解码 ---
航歌
评论

全部评论(0)

回到顶部