DEV Community

Mevin Ngugi
Mevin Ngugi

Posted on

ULTIMATE INTRODUCTION TO PYTHON

Python has become the fastest growing programming language due to its wide range of applications and usage, such as data science, web development, robotics and/or IoT. Getting started is very easy. This is especially true for those without a technical background. The syntax is similar to writing simple and clear sentences in English. Let's take the print statement as an example.

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

Printing is a way to instruct the Python computer to print, much like pressing Print in Word or any other word processing program. Well, in this case, you don’t click but write code to be executed. Cool right?

In this and future articles, I will be using Python3 as it is widely used and supported by most python frameworks and libraries. How widely used you ask? As of 2021, Python has grown to be the second most popular language after JavaScript from research that combined telemetry from over 4M+ repositories. Python must be on to something.

A Look Back at Python

Before we dive into setting up our environment, let's take a minute or two to better understand how Python came to be and where it's going.

Python was developed by Guido van Rossum in December 1989. Named after the BBC comedy series Monty Python's Flying Circus, which started as a hobby project to keep him busy over the Christmas holidays, 29 years later it has emerged as one of the most popular programming languages ​​in the world. While Python remains popular, even more so in the field of data science, there are some challenges that still need to be addressed. For example, we are currently living in a mobile first world, and Python is yet to catch up on. However, Python is gaining an ever-diverse user and usage adoption and it being used as a bridge between technologies. Needless to say, developers out there like you and me are pushing the limits to what Python can do every single day. And to you trail blazers I say, keep on keeping on.

Installation

As with any other programming language, the first step to learn is how to install it.

Windows User:

  • Go to Python's official website.
  • Click on the download button (As of this write up the latest version was Python 3.10.2)
  • Go to the path where the package is downloaded and double-click the installer.
  • Check the box indicating to "Add Python 3.x to PATH" and then click on "Install Now".
  • Once done you'll get a prompt that "Setup was successful". Check again if python is configured properly using the above command.
  • To confirm if Python is installed and configured properly, use the command python3 --version.

Mac User:

  • First install xcode from the app store.
  • If you want to install Xcode using the terminal, then use the following command:
xcode-select –install
Enter fullscreen mode Exit fullscreen mode
  • After that, we will use the brew package manager to install Python. To install and configure brew, use the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Enter fullscreen mode Exit fullscreen mode
  • Once brew setup is done, use the following command to update any outdated packages:
brew update
Enter fullscreen mode Exit fullscreen mode
  • Use the following command to install Python:
brew install python3
Enter fullscreen mode Exit fullscreen mode
  • To confirm if Python is installed and configured properly, use the command python3 --version.

Linux User:

To install Python using apt, use the following command:

sudo apt install python3
Enter fullscreen mode Exit fullscreen mode
  • To install the Python using yum, use the following command:
sudo yum install python3
Enter fullscreen mode Exit fullscreen mode

To confirm if Python is installed and configured properly, use the command

python3 --version.
Enter fullscreen mode Exit fullscreen mode

Python Shell

Python provides an interactive shell that is used to run a single Python command to get the output.

Open the command prompt and type "python" or "py" to start the Python shell. You will get the following result.

C:\Users\Mevin>py
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter fullscreen mode Exit fullscreen mode

Now you can type a single statement to get the result. For example, calculate a simple statement like 4 * 7, press enter and the result will be displayed.

C:\Users\Mevin>py
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 4*7
28
>>> 9/4
2.25
>>>
Enter fullscreen mode Exit fullscreen mode

The Python shell executes only one statement at a time. To exit or quit the Python shell, you need to type the exit() or quit() command and press Enter, after which you will see the three greater-than signs (>>>) disappear, and that’s how you’ll know you’ve come out of the shell.

Commenting

Comments help us understand why a particular piece of code was written and what it returns or does. A comment in python is represented by the # symbol. Everything after the # is not executed.

Imagine you are a developer working on next big project. The project has more than a thousand lines of code. To understand how everything works you'll need to go line by line and read through all the code. It also helps other developers looking at your code understand the solution you are designing with ease.

For example:

# My first hello world program. 
Print(Hello World!”)
Enter fullscreen mode Exit fullscreen mode

To note, comments are complete ang grammatically correct English sentences. Starting with a capital letter and a period at the end.

Print

Other than debugging tools from the editor, the thing which helps developers solve problems most often is a print statement. The print statement is one of the most underrated pieces of syntax in all of programming. So how does it help in debugging an issue? Well, consider that you have a module, and you want to check the flow of execution to understand or debug it. There are two options. Either you can use a debugger or add a print statement.

It's not always possible to use a debugger. For example, if you are using the Python shell, then a debugger is not available. In such a scenario, print helps us. Another scenario is when your application is running. You can add a print statement that will display in the logs of your application and monitor them in runtime. Python provides an inbuilt print method with the following syntax:

print("My first print statement!") 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)