Python数据类型

简介: Python的逻辑运算符数字运算符: + - / %关系运算符:a==b, a>b, a=1 True

Python的逻辑运算符
数字运算符: + - / %
关系运算符:a==b, a>b, a<b, a!=b
赋值运算符:a=b(把b的值赋给a), +=, a+=b, -=,
=, /=
逻辑运算符: and, or, not, (if a==b and a!=10:)

1、 整型 int

a=10
print(a)

2、 布尔值bool
布尔值分为两种,一种是 True,一种是 False

>=1 True
<=0 False

3、 float 浮点值

a=3.141592653
m = round(a,2) # 保留两位小数
print(m)

round(float,ndigits)
float代表数字,ndigits代表精度
大的规则是四舍五入

4、字符串 str
'abc' "abc" '''abc'''

string = 'abcadefgahiagh'
print(string)

5、find
查找字符串,如果找到就返回字符串开始的下标,如果没有找到则返回-1

print(string[0]) # 打印下标为0的值
print(string[3]) # 打印下标为3的值
print(string[:]) # 打印所有值
result = string.find('def')
print(result)
# 3 返回下标3

6、 replace 替换

print(string.replace("a","AAA"))

7、split 分隔符

# join(可迭代对象) 一般为list字符串
newlist = string.strip().split("a")
print(newlist)
print(" ### ".strip().join(newlist))

8、 strip()
去除字符串前后的空字符

string.strip()

print("My string is : %s" % string)
print("My string is : {0}".format(string)) # 推荐使用,效率最高

print("hello" + "world")

9、列表
列表是有一系列特定顺序排列的元素组成的,
可以把字符串、数字、字典等任何对象加入到列表中,
其中的元素之间没有任何关系,列表也是自带下标的,默认从0开始

l = [1,2,3.1415926,'a','b','c',True,{"name":"zyy"}]
print(l)
# [1, 2, 3.1415926, 'a', 'b', 'c', True, {'name': 'zyy'}]

10、字典方法
字典有哪些常用方法呢

l.append("hello")
print(l)
# [1, 2, 3.1415926, 'a', 'b', 'c', True, {'name': 'zyy'}, 'hello']

11、 pop
删除元素 ,默认删除最后一个元素

l.pop()
print(l)
# [1, 2, 3.1415926, 'a', 'b', 'c', True, {'name': 'zyy'}]
l.pop(2) # 删除下标为2的元素
print(l)
# [1, 2, 'a', 'b', 'c', True, {'name': 'zyy'}]

12、remove
删除元素,直接删除元素,remove(value)

# index(value) 查找元素对应的下标
print(l.index("a"))
# 2

m = [1,34,234,54,543,5,533,4,5432]
print(m)
# [1, 34, 234, 54, 543, 5, 533, 4, 5432]
m.sort()     #
print(m)
# [1, 4, 5, 34, 54, 234, 533, 543, 5432]
m.reverse()
print(m)
# [5432, 543, 533, 234, 54, 34, 5, 4, 1]

13、 insert
插入新的元素 insert(index, value)

m.insert(3,"hello")
print(m)
目录
相关文章
|
26天前
|
存储 索引 Python
python数据类型
【4月更文挑战第1天】,Python有数字(整型、浮点型、复数)、布尔、字符串等基本类型,及列表、元组、字典、集合等复合类型。列表是可变有序集合,元组是不可变有序集合,字典是键值对无序集合,集合是无序唯一元素集合。还有特殊类型NoneType,仅包含值None。
26 1
python数据类型
|
7月前
|
存储 开发者 Python
Python 内置数据类型详解
## 内置数据类型 在编程中,数据类型是一个重要的概念。 变量可以存储不同类型的数据,不同类型可以执行不同的操作。 Python默认内置了以下这些数据类型,分为以下几类:
84 0
|
1月前
|
索引 Python
Python的内置数据类型有哪些?
Python的内置数据类型有哪些?
17 0
|
7月前
|
存储 Python 容器
Python数据类型
Python数据类型
58 0
|
8月前
|
存储 索引 Python
python中数据类型的详解
python中数据类型的详解
72 2
|
8月前
|
人工智能 索引 Python
python七种数据类型
python七种数据类型
|
9月前
|
存储 数据安全/隐私保护 Python
Python 的数据类型
Python 的数据类型
|
11月前
|
存储 Python
Python内置数据类型
Python内置数据类型
|
Python
Python常见的数据类型
Python常见的数据类型
111 0