简体   繁体   English

没有定义的字母变量在Python中有效

[英]letter variable without being defined works in Python

My code is this: 我的代码是这样的:

word = input('enter a word:')
for letter in word:
        print( letter)

Output: 输出:

enter a word:tree
t
r
e
e

Is letter an in-built variable? letter是内置变量吗?

You don't need to declare variables in Python. 您无需在Python中声明变量。 The variable is defined in the for-loop directly. 该变量直接在for循环中定义。 Python has only very few keywords . Python只有很少的关键字 It is also not a built-in constant . 它也不是内置常量

I recommend going through a Python tutorial . 我建议阅读Python教程 You might also want to try exercism . 您可能还想尝试运动

Variable Annotations 可变注释

The following stuff is only relevant for Python 3.6+. 以下内容仅与Python 3.6+相关。 No matter which Python version you use, you can ignore it. 无论您使用哪个Python版本,都可以忽略它。 If you are a very early beginner, you probably should ignore it. 如果您是初学者,则可能应该忽略它。

You can use variable annotations to "declare" a variable. 您可以使用变量注释来“声明”变量。 PEP 526 introduces them. PEP 526介绍了它们。 They look like this: 他们看起来像这样:

foo: str
int: bar

Python is a language which doesn't require to declare variables beforehand, like Pascal or any versions of C. In the moment you use a new variable, it is considered as being declared. Python是不需要预先声明变量的语言,例如Pascal或C的任何版本。当您使用新变量时,它被视为已声明。 In your case, letter is declared in the for loop. 在您的情况下, letterfor循环中声明。

"Variables declaration" in Python is implicit, in fact we better use name binding . Python中的"Variables declaration"是隐式的,实际上我们最好使用name binding There are many ways of name binding in Python, for is one of them. Python中的name binding方法有很多, for是其中一种。

For more details, you can have a look at binding-of-names . 有关更多详细信息,您可以查看名称绑定

There is one very important implicit fact: Python will pre-compute all names before executing code of one certain scope. 有一个非常重要的隐性事实: Python将在执行某一范围的代码之前预先计算所有名称。

def test_1():
    b = a + 1

def test_2():
    b = a + 1
    for a in range(3):
        print(a)

# NameError: name 'a' is not defined
test_1()

# UnboundLocalError: local variable 'a' referenced before assignment
test_2()

In test_1 , when executing b = a + 1 , the error is name 'a' is not defined . test_1 ,当执行b = a + 1 ,错误是name 'a' is not defined

In test_2 , when executing b = a + 1 , the error is local variable 'a' referenced before assignment . test_2 ,当执行b = a + 1 ,错误是local variable 'a' referenced before assignment That is to say, in test_2 , when executing b = a + 1 , Python already knew that a is a local variable, it just has not been bound to an object, so the error is UnboundLocalError . 也就是说,在test_2 ,当执行b = a + 1 ,Python已经知道a是局部变量,它尚未绑定到对象,因此错误是UnboundLocalError

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:为没有空格的变量分配字母和数字 - Python: Assign letter and number to a variable without spaces 没有定义变量sidea? Python编程 - variable sidea is not being defined? Python programming python - 如何在不分隔每个字母的情况下加入每个输入? - How to join every input without every letter being separated in Python? Python 中的变量在由 Tkinter 按钮定义后未写入文件 - Variable in Python not being written to file after being defined by a Tkinter button 在python中“没有使用上面定义的Redeclared变量”? - “Redeclared variable defined above without usage” in python? 在Python中,为什么lambda表达式可以引用正在定义的变量而不是列表? - In Python, why can a lambda expression refer to the variable being defined but not a list? Python:使用具有在1个函数中定义的值的变量,并在另一个函数中使用 - Python: Using a variable with a value defined in 1 function, being used in another function Python / Spyder - 未定义变量错误,当变量被定义并显示在环境中时。 代码即使出现错误也能工作 - Python / Spyder - Undefined variable error, when variable is defined and shown in the environment. Code works even with the error print(input()+ input())如何在python中工作? 没有变量分配? - How print(input() + input()) works in python ? Without variable assignment? 尽管在init函数中定义了变量,但仍未在类中定义 - variable not being defined in a class despite being defined in it's init function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM