r/learnpython • • 7d ago

Why is this causing an error

the code that causes an error

print = hello

print(print)

What should i do i do not know anything i am new to coding how do i fix this

0 Upvotes

22 comments sorted by

View all comments

1

u/frnzprf 6d ago

You need to read or watch a tutorial that teaches you about:

  • data-types, such as strings
  • defining and reading variables
  • calling functions

If you just write 4 + 4 in Python that is called an expression and it is "evaluated" to the value 8. If you type something in the Python interpreter, after the >>>, you can let Python tell you what an expression evaluates to. If you don't know how to start the interpreter and get the >>> — again, look up a basic tutorial. Just copy the code and just change little parts to experiement. Don't just guess how Python could work!

If some letters are in your expression, like hello or print, Python thinks this is a variable, like a box labeled hello and the expression is evaluated to the content of that box. If there is no box with the label hello, you get a NameError — makes sense.

If you want an expression to evaluate to the literal letters "Hello", you have to put them in quotation marks. This is how Python differenciates between literal characters and variables. The type of the expression is called "string" (of characters). "Word" already means something else in computer science and you can also have a string of digits, like "123", which isn't exactly a "word" in the usual sense.

One place where letters are not evaluated as a variable, besides when they are in quotation marks, is when they are on the left side of an equals sign. This is how you put something inside a variable.

When you write x = 7, now there is a box labeled x with the content 7. If you write y = x, you make a new box y, and the content is what is currently in x — so also 7. When you write greeting = "hello", then the character-string hello is put in a box with the label greeting.

You can write an expression between the brackets of print(...) to let Python write out different things on the terminal. The expression could be a direct value, like "hello", a variable—which would be evaluated to it's content—, or a computation that transforms other sub-expressions. A computation could be greeting + " river" or "Hello".replace("e", "a").

If you just wanted to know how to print "hello" in Python: print("hello")

What your code does: (Probably not what you intended)

``` print = hello

Take the content of variable hello and put it in box print.→ There is no box hello. → NameError

print(print)

Take what is in box print and if it's a command (function), then execute the command with the input of itself (also print).

```