python比较运算符习题
时间: 2023-12-11 10:33:52 浏览: 135
以下是Python比较运算符的习题及答案:
1. 比较两个数字是否相等
```python
a = 5
b = 5
print(a == b) # 输出:True
```
2. 比较两个数字是否不相等
```python
a = 5
b = 6
print(a != b) # 输出:True
```
3. 比较两个数字的大小
```python
a = 5
b = 6
print(a < b) # 输出:True
print(a > b) # 输出:False
```
4. 比较两个字符串是否相等
```python
str1 = "hello"
str2 = "world"
print(str1 == str2) # 输出:False
```
5. 比较两个列表是否相等
```python
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 == list2) # 输出:True
```
6. 比较两个元组是否相等
```python
tuple1 = (1, 2, 3)
tuple2 = (1, 2, 3)
print(tuple1 == tuple2) # 输出:True
```
7. 比较两个集合是否相等
```python
set1 = {1, 2, 3}
set2 = {3, 2, 1}
print(set1 == set2) # 输出:True
```
8. 比较两个字典是否相等
```python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 2, 'a': 1}
print(dict1 == dict2) # 输出:True
```
阅读全文