Getting Started With Python
Everything you need to start your first Python program
Join the DZone community and get the full member experience.Join For Free
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.
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.
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.)
Open command prompt and type, "python --version" to check the version of Python installed on the system.
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.
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.
Now, run use the Python command followed by the script name (with its extension) and see the output in the command prompt.
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.
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.
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.
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.
The output of the application will be visible in the terminal, as well as in the debug console.
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.
You can also execute the python statements directly just as we did in Python Shell Command prompt.
To create a new script in IDLE, go to the file menu, and click on the new file button.
Type the Python code in it and save it with a .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.
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.
In order to demonstrate, I have added some code in PythonApp.py file. Click on run.
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.
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.
Running the Jupyter Notebook
Open the Anaconda Navigator, and click on the launch button in the Jupyter Notebook tile.
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.
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
I hope this article will help you in getting started with Python.
Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.