Getting Started with Python
In previous chapter we have learned how to install and setup the python environment in different platforms.
This chapter will explain how to write and execute python program.
Now there are various ways to start Python.
1. Python Shell ( REPL) / Interactive Mode
A shell is usually an "interactive shell", usually termed a REPL which stands for "Read - Evaluate - Print - Loop" Most interpreted languages offer a REPL interface.
It allows us to execute python code from the shell and shows output immediately.
How to start python shell ?
To start python shell
ON windows open the command prompt and type python and press enter, it will start python shell session.
Microsoft Windows [Version 10.0.17134.48] (c) 2018 Microsoft Corporation. All rights reserved. C:\Users\prowessapps>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> this denotes you are in python shell
ON Linux / Mac open the terminal and type python3 and press enter, it will start python shell session.
prowessapps@ubuntu:~$ python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> this denotes you are in python shell
To quit the python command line ( shell ) interface type the command exit():
Let's understand how to use python shell for coding
You can use python shell to run your code without creating any external source file.
So, just look at the code snippet and understand :
# To print use print( ) function >>> print("Hello World") Hello World OUTPUT # To evaluate an expression >>> 2 + 3 5 OUTPUT >>> a = 2 Variable Declaration >>> b = 3 >>> c = a + b >>> print (c) 5 >>> print("SUM =",c) print output with concatenation variable SUM = 5
2. Script ModeThis mode is used to execute Python program written in a file. Such a file is called a python script.
Python scripts have the extension .py, meaning that the filename ends with .py.
Create the python script:
Open the text editor.
Type the following code and save the file with name "first.py"
You can save your file at any location in your system, for this example file is saved at Desktop.
# SAVE FILE : first.py print("Hello World") a = 10 b = 20 c = a + b print("SUM =",c)
Run the script :
Open the terminal ( Command Prompt) and change the working directory to that location where you have saved your source file.
In this example case, file location is Desktop
Microsoft Windows [Version 10.0.17134.48] (c) 2018 Microsoft Corporation. All rights reserved. C:\Users\prowessApps>cd Desktop C:\Users\prowessapps\Desktop>
For running script type the command
On Linux / Mac
C:\Users\prowessapps\Desktop>python first.py Hello World SUM = 30
3. Using IDLE (Integrated development and learning environment)
IDLE is Python’s Integrated Development and Learning Environment, which has been bundled with the default implementation of the language.
To open IDLE type idle in search box of all program menu as you search any program like word, excel etc.. and open it.
IDLE by default opens in shell mode.
In IDLE you can direct create a python script file and run it.
To do this follow the following step :
STEP 1: click on file menu option and choose 'New File'
STEP 2: write your python code and save it.
STEP 3: press 'F5' to run script.
4. PyCharm IDE
Another way to create and run python code is by using PyCharm IDE
PyCharm is an Integrated Development Environment (IDE) used in computer programming, specifically for the Python language. It is developed by the Czech company JetBrains.
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at email@example.com
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at firstname.lastname@example.org