Posts

Showing posts from July, 2024

How to exit a python program

Image
             A start and end are common in any process. As we know earlier to start the python script. In general, we end a python program after displaying an output. To exit a python program, there are three methods. Here, are the three methods…. 1.     Using exit() method. 2.     Using quit method. 3.     Using sys.exit() method. Each one is coded with an example below. 1.Using exit() method: It has the following steps. ·       First open a command prompt in your system. ·       It shows the below screen. ·       Type “py” to enter into python environment. ·       Write your print statement. print(“Hi, welcome to python”) ·       It displays the message “ Hi, welcome to python” on the screen. ·       To exit the python program, jus...

How to execute bash commands in python???

Image
 Bash (Bourne Again shell). It is a shell used in Linux and GNU operating systems. Bash is an enhanced version of Bourne shell. Why bash commands??? Bash commands interact the system. It deals with Datatypes, files, directories and its associated functions. Some of the bash commands are listed below. ls – lists the files and directory. cd- changes the directory. pwd – displays the present working directory. touch – creates a new file. rm -remove files and directory. cp – copying a file. mv – move a file to another location. These are the basic bash commands. How to execute bash commands in python???               This can be done by some built in packages in python. Let us use the package “os”. Print the current working directory: Equivalent to pwd #first, import the os package import os #getcwd() prints the current working directory os.getcwd() #the output is given below T...

How to execute python scripts from command line???

Image
     Python is a popular language nowadays. The python scripts are the python codes created by the developer. These can be created in many ways. But it needs an interpreter in the system, where you run the program. Command   line is an interface between the user and the system. It gets the commands as a text in the command prompt, executes and gives you the output in the screen.  How to enter the command line:               To open the command prompt, click the start button, type “cmd” in search bar. Next, select the command prompt icon. It opens the command prompt. How to execute python scripts from command line: There are many methods to execute the python scripts. They are listed below… 1.        Run the python script directly in command prompt. 2.        Run the python file which contains python code. 1.Run the python...

Binary search program in python

Image
 Searching is the process of finding an element from a group of elements. The group of elements may be an array, list, tuple or dictionary in python. Binary search is one of the efficient algorithm to find the element. Algorithm: ·        First, sort the group elements. ·        let us find the middle index of the group. ·        Check the key with the middle element. If the key value is found, then stop the process and print “key found” message. ·        If the key is not found in the middle element, check the below. o    If the key is greater than the middle element, search the element in the right side. o    Else if the key is less than the middle element, search the element in the left side. ·        Continue this process until the key is found. This algorithm can be implemented by two ways ...