Ads Top

Basics of Python Programming Language


To write and run Python program, we need to have Python interpreter installed in our computer. IDLE (GUI integrated) is the standard, most popular Python development environment. IDLE is an acronym of Integrated Development Environment. It lets edit, run, browse and debug Python Programs from a single interface. This environment makes it easy to write programs.
Python shell can be used in two ways, viz., interactive mode and script mode. Where Interactive Mode, as the name suggests, allows us to interact with OS; script mode let us create and edit python source file. Now, we will first start with interactive mode. Here, we type a Python statement and the interpreter displays the result(s) immediately.

Interactive Mode

For working in the interactive mode, we will start Python on our computer.

Python Interpreter
What we see is a welcome message of Python interpreter with revision details and the Python prompt, i.e., „>>>?. This is a primary prompt indicating that the interpreter is expecting a python command. There is secondary prompt also which is „…? indicating that interpreter is waiting for additional input to complete the current statement.
Interpreter uses prompt to indicate that it is ready for instruction. Therefore, we can say, if there is prompt on screen, it means IDLE is working in interactive mode. We type Python expression / statement / command after the prompt and Python immediately responds with the output of it.
Let?s start with typing print “How are you” after the prompt.

>>>print (How are you?) 

How are you?

While writing in Python, remember Python is case sensitive. That means x & X are different in Python.
^D (Ctrl+D) or quit () is used to leave the interpreter.
^F6 will restart the shell.

Note: If we want to repeat prior command in interactive window, you can use ↑ key
to scroll backward through commands history and ↓ key to scroll forward. Use Enter
key to select it. Using these keys, your prior commands will be recalled and displayed,
and we may edit or rerun them also.

Script Mode

In script mode, we type Python program in a file and then use the interpreter to execute the content from the file. Working in interactive mode is convenient for beginners and for testing small pieces of code, as we can test them immediately. But for coding more than few lines, we should always save our code so that we may modify and reuse the
code.
To create and run a Python script, we will use following steps in IDLE, if the script
mode is not made available by default with IDLE environment.
1. File>Open OR File>New Window (for creating a new script file)
2. Write the Python code as function i.e. script
3. Save it (^S)
4. Execute it in interactive mode- by using RUN option (^F5)
Otherwise (if script mode is available) start from Step 2
Step 1: File> New Window

Step 2:
def test():
x=2
y=6
z = x+y
print z

Step 3: Use File > Save or File > Save As - option for saving the file (By convention all Python program files have names which end with .py)

Step 4: For execution, press ^F5, and we will go to Python prompt (in other window)

>>>test()

8

2 comments:

  1. Amazing article. You have explained everything in an easy way.
    Python in an easy way

    ReplyDelete
  2. Excellent post. You have shared some wonderful tips. I completely agree with you that it is important for any blogger to help their visitors. Once your visitors find value in your content, they will come back for more How to Run Python Program In Script Mode



    ReplyDelete

Powered by Blogger.