How to Write Python program- First Step
We all know that python is programming language but what is “Program”?
A program is a sequence of instructions that specifies how to perform a Computation. The Computation might be mathematical or working with text.
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.
You can download the Python idle directly from the link mentioned below:
Download Link: https://www.python.org/downloads/
What is Python shell :
Python Shell is used to execute a single Python command and get the result. Python Shell waits for the input command from the user. As soon as the user enters the command, it executes it and displays the result.
Python shell can be used in two ways :
- Interactive mode
- Script mode
1) Interactive Mode: Interactive Mode as the name suggests, allows us to interact with OS. For working in the interactive mode, we have to first download and start Python on our computer.
What we see is a welcome message of Python interpreter with revision details and the Python prompt, i.e., „>>>‟. his is a primary prompt indicating that the interpreter is expecting a python command.
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.
Let’s start with typing print “How are you” after the prompt.
>>>print “How are you?”
Output:
How are you?
2)Script Mode: Script mode allow us create and edit python source file.In script mode, we type Python program in a file and then use the interpreter to execute the content from the file. 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
Note: For every updation of script file, we need to repeat step 3 & step 4
If we write in script mode, it will be written in the following way:
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()
Output Will be:
8
Alternatively we can execute the script directly by choosing the RUN option.
Important Points need to know before start writing programme in python:
- If we want to repeat prior command in interactive window, you can use “UP ARROW‟ 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.
- ^D (Ctrl+D) or quit () is used to leave the interpreter.
- ^F6 will restart the shell.
- To leave the help mode and return back to interactive mode, quit command can be used.
- Type Credits after the prompt and what we get is information about the organization involved in Python development. Similarly, Copyright and Licenses command can be used to know more about Python. Help command provides help on Python.
- While writing in Python, remember Python is case sensitive. That means x & X are different in Python.
- Result produced by Interpreter in both the modes, viz., Interactive and script mode is exactly same.
- Python, in interactive mode, is good enough to learn, experiment or explore, but its only drawback is that we cannot save the statements for further use and we have to retype all the statements to re-run them.
For more detail on the above topic ” How to Write Python program- First Step ” you may also write to us @:
To check about an earlier topic on Python click below:
“https://www.soluversity.in/what-is-python-and-why-so-popular“
To get the PDF copy on above topic : “How to Write Python program- First Step” Write us…