python有趣的代码
时间: 2023-10-11 15:07:29 浏览: 15
当然,以下是一些我认为有趣的Python代码:
1. 反转字符串
s = "Hello World"
print(s[::-1])
2. 打印乘法表
for i in range(1, 11):
for j in range(1, 11):
print(i * j, end="\t")
print()
3. 生成斐波那契数列
def fibonacci(n):
if n <= 1:
return n
else:
return (fibonacci(n-1) + fibonacci(n-2))
for i in range(10):
print(fibonacci(i))
4. 判断回文数
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("racecar"))
print(is_palindrome("hello"))
希望这些代码能够帮助到你!
相关问题
python有趣代码大全
当然,Python有很多有趣的代码示例。下面是一些有趣的Python代码示例:
1. 打印心形图案:
```python
print('\n'.join([''.join([('Love'[(x-y) % len('Love')]
if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ')
for x in range(-30, 30)]) for y in range(15, -15, -1)]))
```
2. 模拟掷骰子:
```python
import random
def roll_dice():
dice = random.randint(1, 6)
print(f"你掷得了 {dice} 点!")
roll_dice()
```
3. 绘制彩色螺旋:
```python
import turtle
colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
turtle.bgcolor('black')
turtle.speed(0)
for x in range(360):
turtle.pencolor(colors[x % 6])
turtle.width(x / 100 + 1)
turtle.forward(x)
turtle.left(59)
```
4. 生成随机密码:
```python
import random
import string
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
print("生成的随机密码是:", password)
generate_password(8)
```
这些代码示例只是其中的一小部分,您可以根据自己的兴趣和需求来探索更多有趣的Python代码。希望对您有帮助!
python有趣小代码
以下是一些有趣的Python小代码:
1. 生成随机密码:可以使用Python的random和string模块来生成随机密码。
2. 猜数字游戏:编写一个程序,让用户猜一个随机生成的数字,直到猜中为止。
3. 打字游戏:编写一个程序,让用户在规定时间内尽可能多地输入单词或句子。
4. 爬虫:使用Python的requests和beautifulsoup模块来爬取网站上的数据。
5. 自动发送邮件:使用Python的smtplib模块来编写一个程序,可以自动发送邮件。
6. 自动化测试:使用Python的selenium模块来编写一个程序,可以自动化测试网站。
7. 图片转字符画:使用Python的pillow模块来将图片转换为字符画。
8. 自动化下载:使用Python的urllib模块来编写一个程序,可以自动下载网站上的文件。
9. 自动化填表:使用Python的selenium模块来编写一个程序,可以自动填写网站上的表格。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)