Learning Path for Programming Journey. Start Now

Python Programming Language – A Gentle Introduction

Python Programming Languages

Hackr.io.

Spread the Knowledge

As a programmer, you are always looking out for a language that is easy to learn, free, cross-platform, has a great developer community support, and good in-built features that could help you develop your applications quickly. You may wonder if such a language exists. The answer is ‘Yes’, and it is Python.

Why Learn Python Programming Language?

Python is one of the most popular general purpose programming languages. It is among the world’s fastest growing programming languages and is used by software engineers, mathematicians, data analysts, scientists, network engineers, students, and accountants. The features that make Python such a powerful language are:

  • It is an Interpreted, object-oriented, and a high-level programming language. Python is called an interpreted language as its source code is compiled to bytecode which is then interpreted. CPython usually compiles Python code to bytecode before interpreting it.
  • It supports dynamic typing and Dynamic binding. In languages like Java, C and C++ you cannot initialize a string value to an int variable and in such cases, the program will not compile. Python does not know the type of the variable until the code is executed.
  • Python has an easy syntax which enhances readability and reduces the cost of code maintenance. The code looks elegant and simple.
a = 2
b = 3
sum = a + b
print(sum)
  • Python framework has modules and packages, hence facilitates code reusability.
  • Python is open source or freely distributed. You can download it for free and use it in your application. You can also read and modify the source code.
  • No Compilation of the code – The cycle of Edit-test-debug is fast hence a delight to any coder.
  • Supports exception handling. Any code is prone to errors. Python generates exceptions which can be handled hence avoids crashing of programs.
  • Automatic memory management. Memory management in Python involves a private heap(a data structure that represents a queue) containing all Python objects and data structures. On demand, the Python memory manager allocates the heap space for Python objects and other internal buffers. The management of this private heap is ensured internally by the Python memory manager.

You can do a lot with Python. Here is a list of applications in the modern world:

Web Development

As a web developer, you have the option to choose from a wide range of web frameworks while using Python as a server-side programming language. Both Django and Flask are popular among Python programmers. Django is a full-stack web framework for Python to develop complex large web applications, whereas Flask is a lightweight and extensible Python web framework to build simple web applications as it is easy to learn and is more Python based. It is a good start for beginners.

Application giants like Youtube, Spotify, Mozilla, DropBox, Instagram use the Django framework.

Whereas Airbnb, Netflix, Uber, Samsung use the Flask framework.

Machine Learning

As Python is a very accessible language, you have a lot of great libraries on top of it that makes your work easier. A large number of Python libraries which exist, help you to focus on more exciting things than reinventing the wheel. Python is also an excellent wrapper language for working with more efficient C/ C++ implementations of algorithms and CUDA/cuDNN, which is why existing machine learning and deep learning libraries run efficiently in Python. This is also super important for working in the fields of machine learning and AI.

Data Analysis

Python has tools for almost every aspect of scientific computing. Bank of America uses Python to crunch their financial data and Facebook looks upon the Python library Pandas for its data analysis.

While there are many libraries available to perform data analysis in Python, here are a few to get you started:

  • NumPy For scientific computing with Python, NumPy is fundamental. It supports large, multi-dimensional arrays and matrices and includes an assortment of high-level mathematical functions to operate on these arrays.
  • SciPy works with NumPy arrays and provides efficient routines for numerical integration and optimization.
  • Pandas, is also built on top of NumPy, offers data structures and operations for manipulating numerical tables and time series.
  • Matplotlib is a 2D plotting library that can generate data visualizations as histograms, power spectra, bar charts, and scatterplots with just a few lines of code.

Games

Python and Pygame are good languages and framework for rapid game prototyping or for beginners learning how to make simple games. The famous Disney’s multiplayer online role-playing game Toontown Online is written in Python and uses Panda3D for graphics. Battlefield 2 a first-person shooter military simulator video game uses Python for all of its add-ons and a lot of its functionality. Frets on Fire a free, open-source Finnish music video game is written in Python and uses Pygame. Pygame is a Free and Open Source python programming language library for making multimedia applications like games.

Desktop Applications

As a part of Python standard library – Tkinter gives you a possibility to create small, simple GUI applications. The PyQt library is most useful for desktop creation python bindings for the Qt (C++ based) application development framework. The PySide library is the python binding of the cross-platform GUI toolkit Qt.

Python compared to other languages

If you know a few other languages, this section may be of interest to you. Here is a quick comparison of Python with other languages.

Java and C++

Python is expected to run slower than Java and C++ but the programs are 3 to 5 times shorter and the time taken to develop the application is less. One of the reasons we have already seen above is that Python is a dynamic binding language. Python has a syntax that uses simple functions and variables.

Perl

Python and Perl come from a similar background basically Unix scripting. Perl emphasizes support for common application-oriented tasks, such as extracting information from a text file, report printing, text file conversions into other formats. Python emphasizes support for common programming methodologies such as data structure design and object-oriented programming and encourages programmers to write readable (and thus maintainable) code by providing an elegant syntax.

Tcl

Like Python, Tcl is used as an application extension language, as well as a stand-alone programming language. However, Tcl is weak on data structures and executes typical code much slower than Python. Tcl also lacks features needed for writing large programs hence a large application using Tcl usually contains extensions written in C or C++ that are specific to that application, and an equivalent application can often be written purely in Python language.

Smalltalk

Like Smalltalk, Python has dynamic typing and binding, and everything in Python is an object.

Smalltalk’s standard library of collection data types is more superior, while Python’s library has more facilities for dealing with Internet and WWW realities such as email, HTML and FTP.

Now, let’s see the nuts and bolts of Python

How to install Python

Python installation is pretty simple. You can install it on any operating system such as Windows, Mac OS X, Linux (Ubuntu)

Installation of Python on Windows

Goto https://www.python.org/downloads/. Click on Download Python 3.7.3 (you may see a different version number as it depends on the latest release)

Once the file python3.7.3.exe is downloaded you can run the exe file to install Python. The installation includes IDLE, pip and the documentation. IDLE is an integrated development environment(IDE) for Python, which has been bundled with the default implementation of the language. IDLE is a graphical user interface (GUI) which has a number of features to develop your programs.

Python can be installed on Linux/Unix, Mac OS X too. Click on https://www.python.org/download to install on Linux/Unix, Mac OS X and to install on other OS click on https://www.python.org/download/other/ some examples of other OS are AIX, IBM i, iOS, OS /390, z/OS, Solaris, VMS and HP-UX.

You could also install Pycharm an IDE for Python developed by JetBrains, it claims to work better than any other IDE for Python. Pycharm helps developers to write neat and maintainable code, and it provides all the tools needed for productive Python development. You can download Pycharm for Linux/Unix, Mac OS X and Windows from https://www.jetbrains.com/pycharm/download/#section=windows.

Now that you have the required IDE setup, you can start writing your first program.

If you are using Pycharm then follow the steps given below:

  • Click “Create New Project” in the PyCharm welcome screen.
  • Give a valid project name
  • Create a new python file, so right click on the folder name and select New -> Python File
  • Write the code:
# this program prints Hello World on the screen
Print(‘Hello World’)
  • Save the file as HelloWorld.py
  • Run the file HelloWorld.py
  • The output will be seen on the screen as – Hello World

Your first Python program is ready. Now let us understand the fundamental features of the language.

The Python Language – Feature set

The Python language has 8 basic features that will help you write your own program applications in Python.

  • Comments in Python
  • Keywords and Identifiers
  • Variables, Constants, and Literals
  • Data Types
  • Flow control
  • Functions
  • Classes and Objects
  • Exception Handling

Comments

When we open any program written in a language, understanding the logic of the program is difficult. Comments are statements in any program which are not executed i.e. it does not alter the output result however they play a very important role as it improves readability of the code. Comments should be written in plain English language for any user to read and understand.

There are two ways in which you can comment in Python:

Single line comment: as shown below

# this line is a sample python comment. I am adding two numbers in the program below

X = 6
Y = 10
Z = x + y
Print(“”# Hello World”)
print(z)

However ‘#’ inside a program statement is not a comment. Output will be:

# Hello World
16

Multiline comment

For a multi-line comment in Python, you need to use triple single quotes at the beginning and at the end of the comment, as shown below.

‘’’
This is a sample multi
Line comment Python will ignore these
Lines.
‘’’
print(“Hello World”)

Keywords and Identifiers

Keywords are reserved words in the Python language. So, you cannot use keywords when you would like to name your variables, classes, functions and so on. These keywords define the language syntax, flow and structure. Below is the list of keywords in Python.

Identifiers are names given to the variables, functions, and classes that you define. There are certain rules that you need to remember when you name identifiers.

  • Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. Names like displayNamesClass, intSalary_1, _myName all are valid identifiers.
  • An identifier cannot start with a digit. 1Salary is invalid, but Salary1 is valid.
  • Keywords mentioned above cannot be used as identifiers.
  • You cannot use special symbols such as !, @, #, $, % etc. while naming identifiers.
  • Python is a case-sensitive language hence employeeName and EMPLOYEEname are not the same.

Variables, Constants and Literals

Variables are used to store data which can be later used and changed in the program if needed.

empName = “Jason”
empNo = 19160
The = operator is used to assign a value to the variable
print(empName)
Will show the output as - Jason
empName = “Susie”
print(empName)

Will show the output as – Susie

Since Python is a dynamic typed language you do not have to worry about the data type of the variable when you declare it. When the code is executed the type of the variable will be identified based on the value in it.

Constants are types of variables that cannot be changed. You could create a config.py file and store your constants in there. These can be used in your code wherever necessary.

For example:

config.py file will contain constants such as:
COMPANYNAME=DATAINC
COMPANYLOC=SAN FRANCISCO

To use the config,py constants in your code you need to do the following

import config
# this is the config.py file that you have included in your program because you have to
# access the constants which are in the file.
print(config.COMPANYNAME)
print(config.COMPANYLOC)

When you run the program the output will be:

DATAINC
SAN FRANCISCO

Literals are the data that is assigned to a variable or a constant. Python has the following literals. String, Numeric, Boolean, Special Literal called None and collection literals.

Here is an example of few types of literals.

String: “Delhi”

Numeric:100, -46.89 (float), 3.14j (3.14j is an imaginary literal which yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it)

Boolean: True or False. A Boolean literal has only 2 values

Data Types

In Python data types are identified based on the values the variables contain. Python is an object oriented language hence variables are considered as objects and data types are classes. Since Python is a dynamic-typed language you do not need to declare the variable with their type before using them.

Some of the important data types are as follows:

Numbers:

int, float and complex are data types that represent Numbers.

a = 5
b = 8.77
c = 2+3j

String: is a sequence of Unicode characters. You can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ”’ or “””.

The data type of string in Python is – str

S = “This is an example of a string”

Boolean: If the value in a variable is either True or False, Python considers the data type of the variable as Boolean

If (number % 2) = 0
noEven = True # noEven is of Boolean type
else
noEven = False

List: List data type is an ordered sequence of values. All values in a list need not be of the same data type. Lists are changeable (mutable). The value in a list can be modified. Lists are used extensively.

List

Tuple: Tuples are also similar to lists, they are an ordered sequence of values. The values in a tuple are not changeable (immutable). They are faster than lists as they do not change dynamically.

Set: A set is an unordered and unindexed collection of items. The output of the set will show unique values.

a = {5,2,3,1,4}

Dictionary: Dictionary is an unordered collection of key and value pairs. A dictionary is accessed by the keys. The keys can be of any data type.

Sampledict = {1:”Sam”, 2:”Jack”, “name”:”Jill”}
You can also convert one data type to the other and it is called type conversion.

Flow Control

 

    • If, if …else, if …elif…else – Flow control is a part of decision making in programming. It helps you to run a particular piece of code only when a condition is satisfied.

 

Flow Control Here are some sample if conditions # Program checks if the number is positive or negative # And displays an appropriate message num = 3 if num >= 0: print(“Positive or Zero”) else: print(“Negative number”) You can extend the same program to include elif as follows # In this program, # we check if the number is positive or # negative or zero and # display an appropriate message

num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

You can use nested ifs i.e. you can have an – if…elif…else statement inside another if…elif…else statement.

Loops

  • A loop is a sequence of instructions that is continually repeated until a condition is reached. There are three types of loops in Python. .

for loop: Here is an example of a for loop

# Program to print values stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4]
# iterate over the list and print the values one by one
for val in numbers:
print(val)

while loop: a while loop is similar to a for loop, however in a for loop you know the number of times you are going to iterate. A while loop executes as long as a condition holds true.

This program prints all numbers from 1 to 9

num = 1
# loop will repeat itself as long as
# num < 10 remains true
while num < 10:
   print(num)
#incrementing the value of num
   num = num + 1

break and continue are used in loops to alter the flow in the loop. Break is used to exit out of the loop for a particular condition hence it follows an if condition. A continue is used to skip a set of instructions and move on to the next iteration.

Example of break and continue:

# program to display only odd numbers

for num in [20, 11, 9, 66, 4, 89, 44]:
# Skipping the iteration when number is even
if num%2 == 0:
continue
# This statement will be skipped for all even numbers
  print(num)
# program to display all the elements before number 88
for num in [11, 9, 88, 10, 90, 3, 19]:
  print(num)
if(num==88):
  print("The number 88 is found")
  print("Terminating the loop")

break

  • pass: An interesting feature in Python. ‘pass’ is a placeholder. If you would like to use a function, but you are not ready with the code for the function you can use ‘pass’. Here the Python interpreter does not ignore ‘pass’ however it assumes that it has to do nothing as of now.
# pass is just a placeholder for
# functionality to be added later.
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass # do nothing as of now

Functions

A function is a sequence of steps or a block of code that performs a particular task. It usually accepts an input parameter, performs a process and returns a result. A function can be called from another function or from the main program. Functions are very important in coding.

Advantages of using a function in a program are:

  • Improves readability of code
  • Functions can be reused any number of times
  • The same Function can be used in any number of programs
  • It makes the code modular hence you could avoid errors

There are two types of functions in Python:

  • Built-in functions: These functions are predefined in Python and you need to just use them. You do not have to define the function you just need to call the function wherever it is required.
  • User defined functions: The functions that you create in your code for a specific process are user-defined functions.

Sample function in Python:

def multiply_nos(num1, num2) # this is the definition of your function with 2 input parameters
return num1 * num2 # function returns the product of 2 numbers
# now you are calling the function in your program
product = multiply_nos(5,6)
print(product)

The output on the screen will show you the number 30

Class and Objects

Python is an object-oriented programming language (OOP). Python satisfies the four principles of OOP encapsulation, abstraction, inheritance, and polymorphism. You can create classes, objects with attributes and methods.

Class and Objects

Class: A class is a blueprint of an object. You can imagine a class as a skeleton with certain attributes and methods. Attributes are the properties of the class and the methods are functions that are specific to the class.

Object: When you create an instance of the class with specific features it is an object. The example here will help you understand it better.

# this is a class
class box:
figuretype = “3D” # this is a class attribute
def boxdimension(self, length, breadth, height)
# these are instance attributes and boxdimension a class method
print (length* breadth * height)
#now you can create an instance of this class
   objsquare = box() # objsquare is an object
   objsquare.boxdimension(10,20,30)
# you are passing these three numbers and the volume of
# the box will be shown as the output

Classes like functions are good to use as it enhances modularity and the code is reused. Classes can be used when you need to represent a collection of attributes and methods that will be used repeatedly in other places in your application.

Exception Handling

Errors detected during execution are called exceptions. Exceptions can be handled in Python. There are various types of exceptions that can be handled in your program. Few examples of exception are ValueError, KeyboardInterrupt, OSError, ZeroDivisionError and so on. Here is a sample code for exception handling

def this_fails():
x = 1/0
try:
this _fails()
except ZeroDivisionError as err:
print(‘Handling run time error error name is :’, err)

The output of this program will look like this

Handling run time error name is: division by zero

You can define your own exceptions by creating a new exception class. Exceptions should typically be derived from the Exception class, either directly or indirectly.

File Handling

File handling is all about opening a file, reading, writing into it and closing a file.

For example to open a text file you could do it with an in-built function ‘open’ in Python
f = open("test.txt") # open file in current directory
f = open("C:/Python33/README.txt") # specifying full path
f.close()

You can close a file using the close function. Files could be opened in various modes like read-only, write-only and so on.

File Handling

Python Doesn’t End Here!

What you read through so far is only the tip of the iceberg. There is a lot more to Python programming. If you are keen to explore and learn further, you could get deep sights on Advanced Topics such as Python Iterators, Co-routines, Decorators, Generators and a lot more.

You Might be Interested In:

Related Posts

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

*

One Comment, RSS

  1. Avatar

    Dinesh Vishnoi April 16, 2019 @ 12:54 pm

    i read your blog this is very helpful for me, i also read this type of blog on IIP Academy.