Python While 循环语法 [英] Python While Loop Syntax

查看:39
本文介绍了Python While 循环语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Solution: 
    def display(self,head):
        current = head
        while current:
            print(current.data,end=' ')
            current = current.next

你好,我在理解上面的 while 循环时遇到了一些困难,AFAIK 你需要有一个 while 循环的条件,所以:

Hello, I am having some difficulties understanding the above while loop, AFAIK you need to have a condition with a while loop, so:

while (stuff) == True:

但是上面的代码有:

while current:

这是否与:

while current == head:

谢谢

推荐答案

while current: 语法字面意思是 while bool(current) == True:.该值将首先转换为 bool,然后与 True 进行比较.在 python 中,转换为 bool 的所有内容都是 True,除非它是 NoneFalse、零或空集合.

The while current: syntax literally means while bool(current) == True:. The value will be converted to bool first and than compared to True. In python everyting converted to bool is True unless it's None, False, zero or an empty collection.

请参阅真值测试部分以供参考.

这篇关于Python While 循环语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆