Python IDLE and print function...

  • Thread starter fog37
  • Start date
  • #1
1,055
61

Summary:

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!
 

Answers and Replies

  • #2
12,089
5,768
You might be entering these in a console window.
 
  • Like
Likes fog37
  • #3
1,055
61
Thanks jedishrfu.

I am sure I am using the IDLE that comes with the Python installation...
 
  • #4
pasmith
Homework Helper
1,857
513
(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
  • #5
1,055
61
ok. I see. Thanks pasmith. It only works in the IDLE to not use print and still get the same output...
 
  • #6
Ibix
Science Advisor
Insights Author
6,920
5,832
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.
 
  • #7
wle
316
139
(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
  • #8
1,522
1,380
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
  • #9
1,055
61
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
PeterDonis
Mentor
Insights Author
2019 Award
31,131
10,050
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.)

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.
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

Related Threads on Python IDLE and print function...

Replies
4
Views
25K
Replies
10
Views
2K
  • Last Post
Replies
2
Views
22K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
9
Views
1K
Replies
6
Views
784
Replies
19
Views
1K
  • Last Post
Replies
1
Views
3K
Top