1

I wrote a program of two lines in python. At first I tested it in the python shell. Here it is:

>>>state=True
>>>type(state)
<class 'bool'>

The output was as I expected in the shell. And then I wrote these instructions in a file named main.py.

#---------------main.py----------------#
state=True
type(state)

Then I executed this program using linux terminal as root user. The output was nothing

[manjaro ~]# python main.py
[manjaro ~]# 

I expected that the output would be as it was in the shell. As a beginner In python I don't know why there was no output. Please help me to understand why there was no output.

3
  • 1
    Possible duplicate of function print in python shell Commented Apr 29, 2018 at 12:49
  • You can take a look at the answers in the dupe - together with KAsramds answer this should clairfy things. if not, ask by comment Commented Apr 29, 2018 at 12:49
  • The interactive shell is a Read–Eval–Print Loop. Printing is automatic in the shell. You may need to use the print() function in a Python script to display output. Advise you read the Python Tutorial to learn the basics of the language. Commented Apr 29, 2018 at 14:45

1 Answer 1

4

What you see is the raw representation of the object which is returned by __repr__ method of the respective object. It's what is called when you type an object in Python's interactive shell. When you're in a file you need to print the result using print function that triggers the __str__ method.

state=True
print(type(state))
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain more about repr method and str please. I want to learn more about them from you. because unlike some awkard articles, You explained them clearly and neatly. Can you please tell more about them Please sir I request you! hope some help from you
@user9218974 Well, __str__ and __repr__ are not such complicated methods. The documentation has already explained their functionality clearly docs.python.org/3/reference/datamodel.html#object.__str__. Please check the documentation and let me know if you had any further questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.