Python Logo2
Blog

How To Write My First Python Program

Before I start writing my first Python program, let’s build a foundation for understanding its importance!

Can you imagine, there are at least 50,000 professional skills in the world. The challenge is figuring out which of those skills to learn to keep up with industry demands and technology trends.

Personally, I think the following five domains would top the list of skills one should learn:

Artificial Intelligence
Machine Learning
Data Science
Mobile Application Development
Web Development (Read book WordPress Made Super Simple)

It goes without saying that in addition to theoretical knowledge, you will need to develop practical skills to master any of these domains. Here comes the importance of learning programming languages. However, with the pace at which technology evolves at all times, it is prudent that we explore a programming language that is both powerful and here to stay. Furthermore, the language should be easy to learn because skills such as data science, artificial intelligence or machine learning are not only developed by software developers, but also by mathematicians, statisticians, and scientists. The only software language that meets all of these requirements is PYTHON.

Here I will introduce Python !!

Python Logo

INTRODUCTION

Python is a general-purpose, high-level programming language created by Guido van Rossum in 1991 and developed by the Python Software Foundation. It is the fastest growing programming language in the world.

Read: The Incredible Growth of Python

Python is used by major companies like Google, Facebook, Dropbox, IBM, Instagram, Uber, Quora, and many others, which makes it credible and popular.

Python is developed with an emphasis on code readability and simple syntax. It is a “batteries included” language due to its extensive standard library. Programmers can write programs in fewer lines of code and use modules from the standard library to simplify their tasks. Unlike other languages ​​that use a lot of semicolons, parentheses, or braces, Python relies on new lines to complete the command and indentation using blanks to define scope.

Next, the comparison of a basic program to generate “Hello World” in different languages:

Python:

print(“Hello World”)

Java:

class HelloWorld
{
public static void main(String args[])
{
System.out.println(“Hello, World”);
}
}

C:

#include <stdio.h>
int main()
{
printf(“Hello world\n”);
return 0;
}

Python is a multipurpose language. It is used for various tasks, such as web development and data science, and is considered the preferred language for machine learning and prototyping algorithms for artificial intelligence.

There are two main versions of Python: Python 2 and Python 3. Python 2.7 was the last version of the 2.x series. The latest version of Python 3.7.4 was released on July 8, 2019. According to PEP-0373, the end-of-life (EOL) date of Python 2.7 has been moved to 2020 to allay the concerns of those users who have not yet can migrate to Python 3.

I would encourage any curious student to start learning ‘Python’. There are many job opportunities on the market for Python developers in data analytics, artificial intelligence, machine learning, applications, or web development. These are emerging and evolving technologies that would rule the world in the future. You can also accept freelance jobs or work as online / offline instructors. The supply of Python developers in the market is very low and therefore it is the best time to update your knowledge in Python.

According to payscale, the average salary for a python developer is INR 740K.

Lastly, I always encourage people to obtain professional certifications based on their skills, as it will not only validate their competence but also make the job search easier. For Python, you can take certification exams at three proficiency levels: Entry, Associate, and Professional at OpenEDG Python Institute.

References:

Python Software Foundation
Python versions
PEP-0373

INSTALLING PYTHON

Installing Python - Anaconda

Getting ready to embark on our Python journey! Installing Python is the first step!!

I will use ‘Anaconda Distribution‘ to create our Python programming environment. I find Anaconda very easy to use, mostly because it is free, open source and easy to deploy.

Anaconda distribution comes with 1,500+ Python/R data science packages, Conda open source package and environment management system, and Anaconda Navigator. It is suitable to run on Linux, Windows and Mac OS  machines.

The credibility of Anaconda Distribution can be assessed from the fact that it has over 15 million users worldwide successfully using it for python programming, data science, machine learning applications, data processing, predictive analytics, etc

[stextbox id="info"]A software distribution is a collection of packages while a package manager is a tool that automates installation, update and removal of packages[/stextbox]

Let’s go ahead installing Python on Windows!!

Installation Steps:

Login to Anaconda Site.

https://www.anaconda.com/distribution/

Chose the OS where you want to install Anaconda (Windows in my case)

Anaconda Installation

Most of the machines re with 64-Bit. So, select the 64-Bit installer package of latest Python Version.

Installing Python

Once download is complete, install the package. You will see the installation wizard. Click ‘Next

Installing Python

Accept License Agreement.

Installing Python

Select an install for ‘Just Me

Installing Python

Select a destination folder to install Anaconda. I prefer to keep the default folder selected by the installer. Click ‘Next‘.

Installing Python

Choose “Add Anaconda to my PATH environment variable“. Ignore the red warnings!! This is your first Python installation, so its better to set your PATH environment. Click ‘Install

Installing Python

Installation will take some time. Be patient.

Installing Python

On successful installation, ‘Thanks for installing Anaconda3!‘ dialog box will appear:

Installing Python

Verify you installation by going to your Windows Start menu, search for Anaconda Navigator. Open it.

Installing Python

[stextbox id="info" caption="Additional Info"]In case you are running out of disk space on your computer, you may like to install Miniconda. Miniconda has the conda package manager and Python, and will be good to go for your initial Python programming. 
Link to install Miniconda:
https://docs.conda.io/en/latest/miniconda.html[/stextbox]

Congratulations!! You are just ready to code you first Python program. 

Reference:
Anaconda Wiki
Conda Doumentation

PYTHON PROGRAMMING TOOLS

Python Programming Tools

Where will you write your Python code? We will need an environment where we can write, edit or delete our code.
So, what are the Python Programming Tools available? Let’s explore!!!

There are 3 main types of programming environments to run Python programs.

Text Editors

They are general editors that support various file types (.js, .py, .txt, .html). In order to have added functionalities for your Python development, you can add various python-specific plugins and add-ons.

There are many text editors in the market that supports Python. Few notable editors are:

Sublime Text 
Vim
Atom 

To let you know, I use Sublime Text for my Python programming.

Notebooks

Notebook is an interactive environment for coding. It is good for learners as you can see input and output next to each other. It supports markdown notes, plots and rich media like videos. For data scientists, notebook is very useful for python coding.

I will be using Jupyter Notebook for most of my Python tutorials. Jupyter is the most popular notebook for Python. It comes integrated with Anaconda Distribution.

Excerpts from Jupyter official site:

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

Jupyter supports over 40 programming languages, including Python, R, Julia and Scala.

Jupyter official site: https://jupyter.org/

Full IDEs

For professional developers working in large software organization, IDE is the way to go for Python programming. IDE encompass all programming tasks in one application like text editor, compiler, debugger and automation tools.

Pycharm and Spyder are the most popular IDEs for Python.

So, my friends, you can use any development tools for Python. It is purely your choice. 

INSTALLING SUBLIME TEXT EDITOR

Installing Sublime Text Editor

Sublime Text is a editor suitable for Python Programming. You can add additional functions by installing plugins, typically community-built and maintained under free-software licenses.

Installing Sublime Text Editor

Sublime Text Installation

Login to Sublime Text Site: https://www.sublimetext.com/

Click on ‘DOWNLOAD FOR WINDOWS

Installing Sublime Text Editor

Once the installer is downloaded, the wizard will open. Keep the default installation folder. Go to ‘Next

Installing Sublime Text Editor

Click ‘Next

Now, you are ready to install Sublime Text package. Select ‘Install

Installing Sublime Text Editor

Post successful installation, you will get a confirmation window. Click ‘Finish

Installing Sublime Text Editor

You can go to start menu to validate if Sublime Text is installed on your computer.

Installing Sublime Text Editor

Congrats friends!! We are well on track…..

MY FIRST PYTHON PROGRAM

First Python Program

Most of you will recall the ‘Hello, World!‘ program. This is usually the first program taught to us while learning any new programming language. Well !! Keeping the tradition alive, lets create ‘My First Python Program’ – Hello World.

I will run my first program using two environment. Sublime Text Editor and Jupyter Notebook.

‘Hello, World!’ using Sublime Text Editor

By this time, you all must have Sublime Text Editor installed on your machine.

Open your ‘Sublime Text Editor‘ from your start menu.

Type print (‘Hello, World!’) in the editor.

My First Python Program

Now, save this program into a file with .py extension in your computer.

My First Python Program

Open your command prompt window.
In order to run your python code, go to the directory where you have saved your file (test.py).
Type the command: python test.py

Output Hello, World! will be printed.

My First Python Program

Note:
You can also directly write Python code from your command line.
Just type python and press ‘enter’.
You will get >>> prompt.

My First Python Program

Now, type print (‘Hello, World!’) and press ‘enter’ key.
You can see the output Hello, World!

My First Python Program

Type exit() to come out of the >>> prompt.

My First Python Program

This method is not an effective way of coding as you can type only one line at a time. You may just know it for academic interest.

‘Hello, World!’ using Jupyter Notebook

Here again, I assume that you have installed Anaconda Navigator in your computer.

Go to your Window Start Menu and look for Anaconda Navigator. Open it.

You will see Jupyter Notebook. Launch the Jupyter application.

My First Python Program

Jupyter Notebook will open in your localhost browser. Please keep in mind that the application opens in your local machine and do not require any internet connection.

My First Python Program

If you want to collect all your Python programs in a dedicated directory, then you have to create a local directory in the path where you have installed Anaconda Distribution. (Of course you can use any other path of your choice)

For me it is C:\Users\<Name>\. I have created the directory Python_Programs.
C:\Users\<Name>\Python_Programs

When you refresh your Jupyter application page, you can see the new directory.

My First Python Program

Now double-click the directory (Python_Programs). Then on top-right hand side button ‘New‘, select ‘Python 3′ from the drop-down menu.

My First Python Program

You will see a cell where you need to write your code. The beauty of Jupyter is that you can execute the code line by line and review the output.

Write your code in the cell.

My First Python Program

You can execute this line of code by hitting the ‘Run‘ button or by pressing ‘Shift-Enter

My First Python Program

Press the ‘Save‘ button.

My First Python Program

Now, the file is by default named ‘Untitled‘. Just click on ‘Untitled‘. A pop-up will open where you can set the name of your file.

My First Python Program

Set the filename and click ‘Rename

My First Python Program

The new filename appears in place of ‘Untitled

My First Python Program

You can go to you local machine and check the directory for the new file.

My First Python Program

[stextbox id="info"]Please note that Python files created in Jupyter Notebook have .ipynb extension. Jupyter notebooks were formerly known as IPython notebooks, which is where the "ipynb" extension came into picture. You can only open .ipynb file using Jupyter application. However, you can convert ipynb files into .py, .pdf etc. to use in other applications.[/stextbox]

Phew!! Friends, you’ve just coded your first Python program. The journey has just begun !!

As I said before, the opportunities are limitless with Python. Now that you’ve just tried out a basic Python program, get ready and start delving into this beautiful language! Let me know your experiences.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!