python中else的研究

print('--------single else:if break--------')
for l in [1, 9, 0]:
    if l % 3 == 0:
        print("if")
        break
else:
    print("else")
    print(l)

print('--------single else:no if--------')
for m in [1, 9, 0]:
    print("for")
    break
else:
    print("else")
    print(m)

print('------single else:no break--------')
for n in [1, 9, 0]:
    if n % 3 == 0:
        print("if")
else:
    print("else")
    print(n)

print('------single else:no if break--------')
for o in [1, 9, 0]:
    print("for")
else:
    print("else")
    print(o)

print('------double else:no break--------')
for i in [1, 9, 0]:
    if i % 3 == 0:
        print("if")
    else:
        print("inner else")
else:
    print("outer else")
    print(i)

print('--------double else:if break--------')
for j in [1, 9, 0]:
    if j % 3 == 0:
        print("if")
        break
    else:
        print("inner else")
else:
    print("outer else")
    print(j)

print('--------double else:else break--------')
for k in [1, 9, 0]:
    if k % 3 == 0:
        print("if")
    else:
        print("inner else")
        break
else:
    print("outer else")
    print(k)

print('------double else:break if --------')
for p in [1, 9, 0]:
    if p % 3 == 0:
        if p == 2:
            print("if")
            break
        else:
            print("inner else")
else:
    print("outer else")
    print(p)

print('------double else:break anyway --------')
for q in [1, 9, 0]:
    if q % 3 == 0:
        if q == 2:
            print("if")
        else:
            print("inner else")
            break
else:
    print("outer else")
    print(q)

打印结果如下

--------single else:if break--------
if
--------single else:no if--------
for
------single else:no break--------
if
if
else
0
------single else:no if break--------
for
for
for
else
0
------double else:no break--------
inner else
if
if
outer else
0
--------double else:if break--------
inner else
if
--------double else:else break--------
inner else
------double else:break if --------
inner else
inner else
outer else
0
------double else:break anyway --------
inner else

Process finished with exit code 0

答案是如果触发了breakelse不执行

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,没有明确提到Python在QQ音乐热度排行国内外研究方面的应用情况。但是,Python在数据分析和可视化方面的应用非常广泛,可以通过Python对QQ音乐的数据进行分析和可视化,以了解QQ音乐热度排行的国内外研究状况。 以下是Python对QQ音乐数据进行分析和可视化的一些例子: 1. 使用Python爬虫获取QQ音乐热度排行榜单数据,并使用Matplotlib库进行可视化展示。 ```python import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt # 获取QQ音乐热度排行榜单数据 url = 'https://y.qq.com/n/yqq/toplist/4.html' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') song_list = soup.select('.songlist__songname_txt') # 统计歌曲类型出现的次数 song_type = {} for song in song_list: type = song.get('title').split('-')[0] if type in song_type: song_type[type] += 1 else: song_type[type] = 1 # 可视化展示 plt.bar(song_type.keys(), song_type.values()) plt.title('QQ音乐热度排行榜单歌曲类型分布') plt.xlabel('歌曲类型') plt.ylabel('出现次数') plt.show() ``` 2. 使用Python爬虫获取QQ音乐热度排行榜单数据,并使用Pandas库进行数据分析和可视化展示。 ```python import requests import pandas as pd import matplotlib.pyplot as plt # 获取QQ音乐热度排行榜单数据 url = 'https://y.qq.com/n/yqq/toplist/4.html' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') song_list = soup.select('.songlist__songname_txt') # 将数据存储到DataFrame data = [] for song in song_list: name = song.get('title').split('-')[1] singer = song.get('title').split('-')[0] hot = song.parent.select('.songlist__score')[0].get_text() data.append([name, singer, hot]) df = pd.DataFrame(data, columns=['歌曲名', '歌手', '热度']) # 对数据进行分析和可视化展示 df['热度'] = df['热度'].astype(int) df.groupby('歌手')['热度'].sum().sort_values(ascending=False).head(10).plot(kind='bar') plt.title('QQ音乐热度排行榜单歌手热度排名') plt.xlabel('歌手') plt.ylabel('热度') plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值