Python数据类型
- 数值 int float complex
- bool
- 字符串 str
- 列表 list
- 元组 tuple
- 字典
bool型
True(1) False(0)
运算符:and or not
bool():查看bool值状态
False:表示一种无的状态,以下值都是False
print(bool(''))
print(bool([]))
print(bool({}))
print(bool(()))
print(bool(None))
print(bool(0))
字符串 str
转义字符/
“格式化工具: {}”.format(“format”)
len():返回字符串的长度
id():返回内存地址
type():返回数据类型
切片操作:
string[:10] # 取前10个字符
string[0:10:2] # 取前10个字符, 每隔一个取一个
string[-10:] # 取后10个字符
string[::-1] # 反转字符
常用方法:
len(line)
string.capitalize() # 首字母大写
string.istitle() # 是否是首字母大写的形式
string.center(20, '%') # 字符串居中, 旁边用%填充
string.count('o')
string.endswith("how")
string.startswith("you")
string.find("how") # 不存在时返回-1
string.index("a") # 不存在时抛出异常
string.upper()
string.isupper()
string.lower()
string.islower()
string.strip() # 类似java的trim()
string.swapcase() # 大小写翻转
列表 list
类似:java:List
切片操作:
序列:
- list是一种容器型的序列
- str是一种扁平型的序列
元组 tuple
类似:java:Array
tuple1.index(2)
tuple1.count(2)
字典 dict
类似:java:Map
zip():将2个列表合成一个列表或字典
//kotlin版
fun main() {
val list1 = listOf("星期一", "星期二", "星期三", "星期四", "星期五", "星期六")
val list2 = listOf(1, 2, 3, 4, 5)
val list = list1.zip(list2)
val map = mapOf(*list.toTypedArray())
println(list)
println(map)
}
fromkeys():根据一个列表生成dict
根据value值找对应的key值
//kotlin版
val dict = mapOf("one" to 1,"two" to 2,"three" to 3,"four" to 4,"一" to 1)
println(dict.filterValues { it == 1 }.keys)
代码部分:
import math
"""
数字型
"""
print("---------------- 数字型 -------------")
number = 60
number += 40
number -= 50
number *= 2
number /= 2
# math 模块
print("----- 乘方 ----")
a = 2
b = 3
print(math.pow(a, b))
print(a ** b)
print('---- 天花板数 和 地板数 -----')
print(math.ceil(math.pi))
print(math.floor(math.pi))
print("----- 角度转化 ----")
print(math.radians(180))
print("----- 三角函数 ----")
print(math.sin(30))
"""
bool型
"""
print("---------------- bool型 -------------")
a = True
b = False
# False:表示一种无的状态,以下值都是False
print(bool(''))
print(bool([]))
print(bool({}))
print(bool(()))
print(bool(None))
print(bool(0))
print("-----")
print(True and False)
print(True or False)
print(True and '')
print(False and '')
print('-----')
a = list(range(5))
b = 4
c = (b in a)
print(type(c)) # <class 'bool'>
"""
字符串
"""
print("---------------- 字符串 -------------")
print("张三说: \"我不是法外狂徒\"")
print("格式化工具: {}".format("format"))
print("zhangsan " + "李四")
print("zhangsan " * 3)
string = "how are you?"
# 常用API
print(len(string))
print(string[:10])
print(string[0:12:2])
print(string[-10:])
print(string[::-1])
print(string.capitalize())
print(string.istitle())
print(string.center(20, '%'))
print(string.count('o'))
print(string.endswith("how"))
print(string.startswith("you"))
print(print(string.find("how")))
print(string.index("a"))
print(string.upper())
print(string.isupper())
print(string.lower())
print(string.islower())
print(string.strip())
print(string.swapcase())
"""
列表
"""
print("---------------- 列表 -------------")
list1 = [1, 2, 3]
list2 = list()
list1[0] = 'one'
print(list1)
list1.append('hello')
print(list1)
list1.clear()
list1.copy()
"""
元组
"""
print("---------------- 元组 tuple -------------")
tuple1 = (1, 2, 3, 4, 5)
print(tuple1.index(2))
print(tuple1.count(2))
a, b = 10, 20
print(a)
print(b)
"""
字典 dict
"""
print("---------------- 字典 dict -------------")
dict1 = {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
"一": 1
}
print(dict1['two'])
# 根据value找key
ones = [key for key, value in dict1.items() if value == 1]
print(ones)
# 将2个列表和成一个列表
list1 = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
list2 = [1, 2, 3, 4, 5]
dict1 = dict(zip(list1, list2))
list3 = list(zip(list1, list2))
print(dict1)
print(list3)
student = ["zhao", "qian", "sun", "li", "wang"]
dict1 = dict.fromkeys(student, 10)
print(dict1)
print(dict1.get("zhang", 10))
print(dict1.keys())
print(dict1.values())
print(dict1.items())
dict1["zhang"] = 20
dict1.setdefault("zhang", 1000)
print(dict1)
dict1.pop("zhang")
print(dict1)
"""
输出:
---------------- 数字型 -------------
----- 乘方 ----
8.0
8
---- 天花板数 和 地板数 -----
4
3
----- 角度转化 ----
3.141592653589793
----- 三角函数 ----
-0.9880316240928618
---------------- bool型 -------------
False
False
False
False
False
False
-----
False
True
False
-----
<class 'bool'>
---------------- 字符串 -------------
张三说: "我不是法外狂徒"
格式化工具: format
zhangsan 李四
zhangsan zhangsan zhangsan
12
how are yo
hwaeyu
w are you?
?uoy era woh
How are you?
False
%%%%how are you?%%%%
2
False
False
0
None
4
HOW ARE YOU?
False
how are you?
True
how are you?
HOW ARE YOU?
---------------- 列表 -------------
['one', 2, 3]
['one', 2, 3, 'hello']
---------------- 元组 tuple -------------
1
1
10
20
---------------- 字典 dict -------------
2
['one', '一']
{'星期一': 1, '星期二': 2, '星期三': 3, '星期四': 4, '星期五': 5}
[('星期一', 1), ('星期二', 2), ('星期三', 3), ('星期四', 4), ('星期五', 5)]
{'zhao': 10, 'qian': 10, 'sun': 10, 'li': 10, 'wang': 10}
10
dict_keys(['zhao', 'qian', 'sun', 'li', 'wang'])
dict_values([10, 10, 10, 10, 10])
dict_items([('zhao', 10), ('qian', 10), ('sun', 10), ('li', 10), ('wang', 10)])
{'zhao': 10, 'qian': 10, 'sun': 10, 'li': 10, 'wang': 10, 'zhang': 20}
{'zhao': 10, 'qian': 10, 'sun': 10, 'li': 10, 'wang': 10}
"""