Getting Started With Python

DZone 's Guide to

Getting Started With Python

Everything you need to start your first Python program

· Web Dev Zone ·
Free Resource

In this article, we will install Python and create the first “Hello World” application using Python.

What Is Python?

Python is a high-level, general-purpose, interpreted, Cross-Platform, Case Sensitive, and Object-Oriented programming Language. Python was created by Guido van Rossum and first released in 1991. It uses an Object-Oriented approach which makes the programmer write clear, logical code for projects. Python has become one of the most popular programming languages used in the world. It is easy to learn because its syntax is similar to the English language. It uses a new line to complete commands and indentation to indicate a block of code. Python is derived from C, C++, Algol-68, Unix Shell, and other scripting languages, etc. The latest version of the Python is Python 3.

Many large companies use Python, including Google, YouTube, Netflix, NASA, etc. It is open-source and available at free of cost. Python is used in Web Development, GUI Application, Scientific and Numeric Computing, Console based applications, Enterprise Application, Data Science, and Machine Learning.

Environment Setup

Python can be installed in multiple ways in a system.

You can directly download the python from https://www.python.org/ official website (as shown below). I am downloading Python for Windows. Python is also available for Mac OS and Linux.

Downloading Python 3

Downloading Python 3

On clicking download, an .exe package of size 25 MB is downloaded. Click on it to start the installation. Make sure to add Python 3.7 to the Path variable. (You can also do it manually as well.)

Adding Python 3.7 to PATH

Adding Python 3.7 to PATH

Open command prompt and type, "python --version" to check the version of Python installed on the system.

checking version installed

Checking version of Python installed

Run the Python command line interpreter by typing, "Python" in command prompt and pressing the enter key. Use the print function, which prints the given object on the Standard output device (Screen) or to the text stream file. For this demonstration, I am passing “Hello World” in the print function.

Passing "Hello World" in print function

Passing "Hello World" in print function

Next, we are going to create a python (.py) file. Open any text editor available in the system. For example, In Windows, open Notepad. Write the simple print method and save it as MyFirstApp.py.

saving file as MyFirstApp.py

Saving the file as MyFirstApp.py

Now, run use the Python command followed by the script name (with its extension) and see the output in the command prompt.

output in command prompt

Output in terminal

Setting Up Visual Studio Code for Python:

Go here to download Visual Studio Code. Becuase I am running on Windows, I will download setup for Windows. Once downloaded, run the installer.

Running installer

Running installer

After successful installation, launch VS Code, click on the extension icon, and search extension for Python. Click on install. The extension will help us in providing intelligence, code formatting, debugging, etc.

Python extension for VSC

Python extension for VSC

Now, open a folder where you want to save your Python code. Then click on add new file, give it a meaningful name, and write the Python code in it. I have written a simple code for this demonstration.

First example

First example

Click on the debug icon and then start debugging. As I have created a single Python file, I selected the currently active Python file as debug configuration. In case your application did not start when you clicked the debug configuration option, you need to check the launch.json and settings.json files for the configuration-related issues.

debugging the active python file

Debugging the active Python file

The output of the application will be visible in the terminal, as well as in the debug console.

Output in terminal

Output in terminal

IDLE for Python Development

Python (in Windows) comes with IDLE (Integrated Development and Learning Environment) which can also be used for learning purposes. Search IDLE in the start button search and launch it.


Launching IDLE

You can also execute the python statements directly just as we did in Python Shell Command prompt.

Executing Python statements directly

To create a new script in IDLE, go to the file menu, and click on the new file button.

Creating new script in Idle

Creating a new script in Idle

Type the Python code in it and save it with a .py extension.

Saving with .py extension

Saving the file with .py extension

Once the file is saved, press F5 to run the module in the Python Shell. On a successful run, IDLE will show output something like below.

idle output

IDLE output

Python in Visual Studio:

Versions of Visual Studio Code past 2017 support the Python Development. During installation of Visual Studio Code, select the Python Development Component. I have already installed Python Development Tool in my Visual Studio 2017 IDE. Let’s create a new Python project (console application), as shown in the image below.

creating Python project

Creating a Python project in Visual Studio

In order to demonstrate, I have added some code in PythonApp.py file. Click on run.

Running PythonApp.py

Anaconda Distribution

You can also install Python by installing open source Anaconda Distribution, which makes it easier to use Python for data science and machine learning on Windows, Linux, and Mac OS. Once downloaded, click on the next button to install it. During installation, you will be asked the location to install and to add path environment variables just like we did during python installation.

installing python 3

Installing Python 3

Anaconda Distribution provides libraries and tools with Python for data science, machine learning, and deep learning out of the box. It includes core Python language, 100+ python library, Spyder, pycharm, and Jupyter Notebook, which are IDEs. Search for Jupyter in the Anaconda Navigator in the start menu and launch it.

Starting Anaconda Navigator

Starting Anaconda Navigator

Running the Jupyter Notebook

Open the Anaconda Navigator, and click on the launch button in the Jupyter Notebook tile.

Anaconda navigator

Anaconda Navigator dashboard

Essentially, Jupyter Notebook is a web (client-server based) application that runs on your localhost on default port 8888. Its UI shows files and folders. You can create a new Jupyter notebook by clicking on the new button and selecting the Python 3 option.

python 3 environment

Creating Python 3 environment

In Jupyter Notebook, you can add cells, and, in each cell, you can write code or markdown cells, as per your need for the documentation purpose. You can execute the code by clicking on the run cell button. The notebook is saved with the .ipynb extension. It can also be exported to HTML and PDF formats

Hello, World Program

Hello, world program

I hope this article will help you in getting started with Python.


python, web dev

Published at DZone with permission of Anoop Kumar Sharma , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}