How to exit a python program

           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. 1.    Using exit() method.
  2. 2.    Using quit method.
  3. 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, just type exit() and press the ‘enter’ key.

·      It exits the environment.

Note: If you want to print a message when exiting, you can add the message in exit().

For example,

exit(“you exited the environment”).


2.Using quit method:

This method uses the keyword ‘quit’. You can use this keyword even in print statement.

#Python program to use ‘quit’ to exit a program.

#get a input ‘a’

a = int(input(“enter a number”))

if a>5 :

   print(quit)

else :

  print(a)

This program checks the given number is greater than 5. If yes, it quits the program. Otherwise, it prints the number.


3.Use sys.exit() method:

          This method uses a built in module “sys”.

#Python program using sys.exit() function to exit a program

import sys

id = int(input(" Enter the id"))

if id > 20 :

    sys.exit("you exceeded the limit")

else :

    print("You are in the limit. your id is:",id)#First, import the sys module.

When executing this program, you get the below output.

These are simple methods to exit a python program. Happy coding!!!!



How to execute bash commands in python???

 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

To change directory: Equivalent to cd

              It uses a function called “chdir()”. Let us built the code.

#import the os package.

import os

#use the chdir() function to change the directory. Include your URL.

os.chdir(“c:\raji”)

#print the changed directory.

print(os.getcwd())

#output to change directory


To list the files in current directory: Equivalent to ls

              Here, a function listdir() is used. To print the files in the directory, use the below code..

#code to display the files in the directory.

os.listdir()

#here is the output

These are the basic bash commands can be executed in python.

How to execute python scripts from command line???

    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 script directly in command Prompt:

   The steps to follow is given below.    

  •    First, open the command prompt and type the keyword “py”.
  •    Type the python code line by line in the command prompt.

    For example,  Creating a welcome message in python.

    The code is given below….

     #Python code to print the welcome message

      print(“Welcome to the world of python”)

  • just type the code and press “enter” button. It gives the below output.

2.Run the python file which contains python code:

  For this method, follow the below steps.

  • ·       open notepad and save the file as “welcome.py”.
  • ·       Add the code to the file.

#Python code to display welcome message

print("This is an example of python file")

print("Welcome to the world of python")

  • ·       save the file and run in the command prompt by typing this.

·                               py welcome.py

·       It gives you the output.

These are the ways to run the python scripts in run time.

Binary search program in python

 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 in terms of programming.

# Python code to implement iterative Binary Search.

# To find the location of k in given array bs. where min refers minimum value. max refers maximum value.

def binarySearch(bs,min, max, k):

    while min <= max:

       mid = min + (max - min) // 2

        # Check if k is present at mid

        if bs[mid] == k:

            return mid

        # If k is greater, ignore left half

        elif bs[mid] < k:

            min = mid + 1

        # If k is smaller, ignore right half

        else:

            max = mid - 1

    # The element was not present

    return -1

if __name__ == '__main__':

    bs = [24, 35, 42, 50, 60]

    k = int(input(" enter the key value"))

    # Function call

    output = binarySearch(bs, 0, len(bs)-1, k)

    if output != -1:

        print("Element is present at index", output)

    else:

        print("Element is not present in array")

This is the python program to search an element in an array. When you execute this code,it shows the following output.