[Simple Steps] How to Clear Python Interpreter Console Screen?
Do you use the Python interpreter console for practicing Python code?
You might have seen, there is no direct way or command to clear Python interpreter console.
So you need a system call to clear the Python interpreter console screen.
For window system,
cls clear the console. For the Linux system,
clear command works.
We can use these system calls to clear screen in Python. It’s simple.
To call system calls on the Python console, it requires the
os library to be imported. This
os library comes inbuilt with python installation. There is no need to explicitly install.
Command to clear Python Interpreter Console on Window:
import os clear = lambda: os.system('cls') #on Windows System clear()
Command to clear Python Interpreter Console on Linux:
import os clear = lambda: os.system('clear') #on Linux System clear()
Don’t Just Copy Paste the Code:
Why am I saying that?
If you copy-paste the code, I am pretty sure you will forget this code and then tomorrow again you will do a Google Search.
The best way is to understand the code written above. It is pretty simple to remember and write code if you know how it has implemented.
What is Lambda Function here?
Above code is all about lambda function.
The “lambda” keyword in Python is used to define anonymous functions. And these functions are also called as lambda functions.
Learn more about lambda function. You can write many such codes with no more hustle. It is extremely useful.
These commands are tested on Python 3.4 version. It will work for both Python 2.x and 3.x versions.
You can run a Python program without using the Python interpreter console. Install free text editor for your system (Linux/Windows/Mac). And now you can run the Python program from Windows’s command prompt or Linux’s terminal.
I am a Python developer. I usually keep Python interpreter console opened. Sometimes test command runs over it and creates cluttered print output on python console. These backlog print command needs to be cleared. So use this trick to clear screen in Python.
Hope you find this quick commands and guide useful for your Python learning. Is there anything I can help you with? Feel free to write in the comment section below.