用python写一个随机数列表,将其中的奇数和输出
时间: 2023-05-23 12:00:48 浏览: 60
好的,以下是用 Python 写一个随机数列表,将其中的奇数和输出的代码:
```python
import random
# 生成随机数列表
nums = [random.randint(1, 100) for _ in range(10)]
# 将奇数取出并输出
odds = [num for num in nums if num % 2 == 1]
print("随机数列表中的奇数有:", odds)
```
希望这个代码能够帮到您,如果您有更多问题或需求,请随时提出。
相关问题
用python写一个随机数列表,将其中的所有奇数和输出
可以这样写:
```python
import random
# 生成随机数列表
num_list = [random.randint(1, 100) for _ in range(10)]
# 将所有奇数找出来并输出
odd_list = [num for num in num_list if num % 2 == 1]
print(odd_list)
```
输出结果类似于:
```
[13, 27, 21]
```
用python生成十个随机数求奇数和
Sure, I can help you with that! Here's some Python code to generate 10 random numbers and calculate the sum of the odd numbers:
```
import random
# generate 10 random numbers
numbers = [random.randint(1, 100) for _ in range(10)]
# calculate the sum of the odd numbers
odd_sum = sum(num for num in numbers if num % 2 != 0)
print("The 10 random numbers are:", numbers)
print("The sum of the odd numbers is:", odd_sum)
```
I hope that helps! Let me know if you have any other questions.