How to clear screen in Python?
How to clear screen in python
There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values (this happens generally when we don't initialize the value of our variable to zero) or the output might give a result in the data type we don't expect from it.
So, whenever you encounter a situation like this you feel that you should once again come to a clear working space and start everything from the beginning.
In this article, we will discuss how we can clear our screen in python when we are stuck in problems as described at the beginning.
We have two ways of implementing the same-
- Using a shortcut key to clear the screen.
- Using a python script to clear screen
Let us have a look at the first method-
The first way to clear the screen in Python is using the shortcut key ‘Ctrl + I’.
The second way is that we can define a cls function and call it when required-
def cls(): print(“\n”*100)
The above cls() function can be called anywhere in our program as per our need.
Python also provides various features from different modules that can make our work much easier. Let us see how do they work-
To clear the commands in Windows and Linux we will use-
For Windows
import os os.system(“CLS”)
For Linux
import os os.system(“clear”)
Now let us have a look at how the same can be implemented using Python script
# import os module from the os import system, name # For displaying output for a specific time import sleep from time import sleep # definition of clear function def clear(): # for windows if name == 'nt': _ = system('cls') # for mac and linux(the os.name is 'posix') else: _ = system('clear') # printing the message in the output print('Tutorials and Example\n'*5) # specifying sleep time after the output is displayed sleep(10) # calling the clear function clear()
INPUT-

OUTPUT-

Let us understand each part of the above program here-
- os module– The os module in Python helps in accessing and manipulating the files and directories present in our system.
- system package- The sys module in Python provides different kinds of functions and variables that can manipulate the Python runtime environment
- time module- The time module provides functions that can be used with time-related programs.
- sleep – The sleep function suspends the execution of a particular thread for a certain time.
- using underscore- The underscore variable is used because the last output is stored in it.
Now we will look at some Python programs so that you have a good hands-on language.
p=int(input("Enter the principal: ")) r=int(input("Enter the rate: ")) t=int(input("Enter the time: ")) si=p*r*t/100 print("The value for simple interest is {}".format(si))
- Python program to calculate the volume of a cylinder.
pi=3.14 r=int(input("Enter the value of radius: ")) h=int(input("Enter the value of height: ")) vol=pi*pow(r,2)*h print("Volume of cylinder is {}".format(vol))
- Python program to perform addition, subtraction, division, and multiplication.
num1=float(input("Enter number1: ")) num2=float(input("Enter number2: ")) add=num1+num2 subt=num1-num2 mult=num1*num2 div=num1/num2 print("Addition of two float numbers is {}".format(add)) print("Subtraction of two float numbers is {}".format(subt)) print("Multiplication of two float numbers is {}".format(mult)) print("Division of two float numbers is {}".format(div))
- Python program to calculate the area of a circle.
pi=3.14 r=int(input("Enter the value of radius: ")) area=pi*r*r print("Area of circle is {}".format(area))
- Python program to find the greatest of three numbers.
a=int(input("Enter the value of 1st number: ")) b=int(input("Enter the value of 2nd number: ")) c=int(input("Enter the value of 3rd number: ")) if(a>b and a>c): print("{} is greatest".format(a)) elif(b>a and b>c): print("{} is greatest".format(b)) else: print("{} is greatest".format(c))
- Python program to understand type conversion.
x=input("Enter the first number: ") y=input("Enter the second number: ") print(x+y) #result is addition of two strings x=int(input("Enter the first number: ")) y=int(input("Enter the second number: ")) print(x+y) #result is addition of two numbers
- Python program to calculate distance between two points.
x1=int(input("Enter the x-coordinate of first point: ")) y1=int(input("Enter the y-coordinate of first point: ")) x2=int(input("Enter the x-coordinate of second point: ")) y2=int(input("Enter the x-coordinate of second point: ")) dist=((x2-x1)**2+(y2-y1)**2)**0.5 print("The calculated distance is {}".format(dist))