What is '$' in python? [duplicate]

2020-04-03 08:36发布

As a beginner in Python, I'm reading a book written by Bill Lubanovic

I found something weird.

In that book, After saving simple code in test1.py, which is

print("This standalone program works!")

it says python can run it by typing in

$ python test1.py

However, whenever I try to use that, syntax error happens.

Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.

标签: python shell
3条回答
SAY GOODBYE
2楼-- · 2020-04-03 09:25

It means you need to type everything but the $ in the terminal.

python test1.py

It's just a convention though. Authors also use > python test1.py and other notations.

I don't know which version of his book you're reading, but he mentions it in this version.

In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.

查看更多
戒情不戒烟
3楼-- · 2020-04-03 09:31

Question was answered in following stackoverflow post: What does the $ mean when running commands?

What does the $ mean when running commands?

As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python. Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)

So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

查看更多
神经病院院长
4楼-- · 2020-04-03 09:36

You are not supposed to enter the $.

The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.

Your terminal will probably use a different prompt, e.g.

[user@localhost ~]$

Or, if you are using a Windows terminal you might see :

C:\>

or

C:\WINDOWS>
查看更多
登录 后发表回答