Python Python IDLE and print function....

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
In the discussion, users explore the behavior of the IDLE editor in Python, noting that variables can be displayed by simply typing their names without needing the print() function. This feature is specific to interactive mode, which is accessible in IDLE and when running Python directly in a console. The conversation highlights the difference between the output of print() and the interactive display, explaining that print() uses the string representation from str(), while the interactive mode uses repr(). It is emphasized that while IDLE allows for quick variable inspection, writing scripts is the recommended practice for programming in Python. The discussion also clarifies the distinction between command line interfaces (CLI) and graphical user interfaces (GUI), with IDLE categorized as a GUI that provides an interactive shell for Python. The concept of shells is further elaborated, indicating that both IDLE and command prompts serve as interfaces for executing commands and displaying results.
fog37
Messages
1,566
Reaction score
108
TL;DR
print function in IDLE
Hello,

While using the IDLE editor, I noticed that it is possible to simply type the variable name, list name, dict name and view its content without using the print() function...For example, if the variable a=5, then I would expect print(a) to output 5. And it does. But I can also simply type "a" and obtain the same result...this makes the use of print superfluous...

What is going on? I though we would always need the print function to output something to the screen...

Thanks!
 
Technology news on Phys.org
You might be entering these in a console window.
 
  • Like
Likes fog37
Thanks jedishrfu.

I am sure I am using the IDLE that comes with the Python installation...
 
(1) This only works when running python interactively.
(2) There is a distinction for strings, as shown in the following example.
Python:
>>> foo = 'a\nb'
>>> foo
'a\nb'
>>> print(foo)
a
b
>>>
 
  • Like
Likes fog37
ok. I see. Thanks pasmith. It only works in the IDLE to not use print and still get the same output...
 
It works whenever python is interactive mode. Just typing python at the command line starts python like that, as does running through IDLE. If you run a script, though, you get no output except print commands and similar.
 
pasmith said:
(1) This only works when running python interactively.
(2) There is a distinction for strings, as shown in the following example.
Python:
>>> foo = 'a\nb'
>>> foo
'a\nb'
>>> print(foo)
a
b
>>>

More precisely, print() prints the default "display" or "aesthetic" string representation of its argument, as returned by calling str() on it. By contrast, Python's interactive interpreter evaluates the expression you give it and tries to display it in source form, as returned by the repr() function. For objects that have a source syntax, the idea is that evaluating eval(repr(x)) should return an object with the same type and value as x. In other words, you can see repr() as a partial inverse of the eval() function. For some objects, the two string representations happen to be the same.

Here are some examples:
Python:
>>> 'hello'
'hello'
>>> str('hello')
'hello'
>>> repr('hello')
"'hello'"
>>> print('hello')
hello
>>> print(str('hello'))
hello
>>> print(repr('hello'))
'hello'
>>> from fractions import Fraction
>>> x = Fraction(3, 2)
>>> print(x)
3/2
>>> x
Fraction(3, 2)
>>> str(x)
'3/2'
>>> repr(x)
'Fraction(3, 2)'
>>> eval(str(x))
1.5
>>> eval(repr(x))
Fraction(3, 2)
You can define one or both kinds of string representation for user-defined objects by writing __str__() or __repr__() methods for them.
 
Last edited:
  • Like
Likes fog37
fog37 said:
ok. I see. Thanks pasmith. It only works in the IDLE to not use print and still get the same output...
That works because you are in python shell by default when you start IDLE. If you see a screen like this:

1598600739507.png

with >>> on each line, it means you are in a shell. In that case, you can view the value of variables without using print function.

IDLE gives you an option to save the commands you have executed in the shell, which is generally lost if you use command prompt/terminal. But that is not the way you should write programs in Python. It is always better to use a script for writing programs.

To create a script, go to File -> New File.

1598600971143.png
 
  • Like
Likes fog37
Thank you everyone.

I guess we are talking about user interfaces (UI). The terms "console", "terminal" and "prompt" are all synonyms of user interface. Is that correct?

There are two types of user interface:
  1. CLI: A user interface can be a command line interface (CLI), i.e. text-based interface with a black screen, white, text, no other functionality. The Windows prompt is CLI.
  2. GUI: we can have graphical user interface with more functionalities, menus, etc. Python IDLE is a GUI user interface.
Both CLI and GUI interfaces are shells (i.e. interpreters), correct? To me, a shell is the program that controls the user interface. So IDLE is a shell. There is also the Windows shell that opens up when we type the command cmd.

Being interactive mode means that we are running one or multiple commands directly in the console/user interface and not from a saved Python file...

Let me know if I have anything wrong.

Thank you!
 
  • #10
fog37 said:
CLI: A user interface can be a command line interface (CLI), i.e. text-based interface with a black screen, white, text, no other functionality. The Windows prompt is CLI.

Not by this description. When you open "Command Prompt" in Windows, the Windows desktop doesn't disappear; the command prompt appears in a GUI window on the Windows desktop, just like any other GUI program. In other words, the Windows "Command Prompt" program is just a GUI program whose main window displays an interactive session with the Windows command interpreter, cmd.exe. (Note that Windows now also has PowerShell, which displays an interactive session with a different command interpreter, powershell.exe.) IDLE is the same sort of thing, except that it displays an interactive session with the Python interpreter.

A true CLI in this sense would be, for example, MS-DOS, where literally all you have is a C:> prompt on a bare black screen. (You can also get this in Linux by not booting X Windows. You used to be able to get it in Windows by booting into DOS mode, but I don't think that's been possible since at least Windows XP, if not earlier.)

fog37 said:
GUI: we can have graphical user interface with more functionalities, menus, etc. Python IDLE is a GUI user interface.

So is any program that runs on Windows today. See above.
fog37 said:
a shell is the program that controls the user interface

The term "shell" usually means an interactive session with a command interpreter--something that interprets commands you type, executes them, and prints the results. So the Windows command prompt, Windows PowerShell, and IDLE are all shells in this sense, just with different command interpreters.
 
  • Like
Likes Wrichik Basu and fog37

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K