大家好!今天给大家带来十个超级简单的Python代码,它们不仅易于理解,而且可以直接运行。这些小代码能够帮助你快速上手Python编程,让你的项目更加丰富多彩。快来一起看看吧!🔍
🌈1️⃣ 打印“Hello, World!”
```python
print("Hello, World!")
```
🌈2️⃣ 计算两个数字之和
```python
num1 = 5
num2 = 10
sum = num1 + num2
print(sum)
```
🌈3️⃣ 检查一个数是否为偶数
```python
num = 4
if num % 2 == 0:
print(f"{num} 是偶数")
else:
print(f"{num} 是奇数")
```
🌈4️⃣ 创建一个列表并打印其中的元素
```python
my_list = ["apple", "banana", "cherry"]
for item in my_list:
print(item)
```
🌈5️⃣ 字符串反转
```python
text = "hello"
reversed_text = text[::-1]
print(reversed_text)
```
🌈6️⃣ 计算字符串长度
```python
text = "Python is fun!"
print(len(text))
```
🌈7️⃣ 判断一个年份是否为闰年
```python
year = 2020
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(f"{year} 是闰年")
else:
print(f"{year} 不是闰年")
```
🌈8️⃣ 列表排序
```python
numbers = [5, 2, 8, 1, 9]
numbers.sort()
print(numbers)
```
🌈9️⃣ 字典遍历
```python
my_dict = {"name": "Alice", "age": 25}
for key, value in my_dict.items():
print(f"{key}: {value}")
```
🔟🔟 查找列表中的最大值
```python
numbers = [5, 2, 8, 1, 9]
max_value = max(numbers)
print(max_value)
```
希望这些简单的代码能够帮助你更好地理解和使用Python!如果你有任何问题或建议,欢迎在评论区留言。我们一起交流学习,共同进步!📚👩💻👨💻