Python
Python
2020]
Python is one of the powerful, high-level, easy to learn programming language that
provides a huge number of applications. Some of its features, such as being object-oriented
and open source, having numerous IDE’s, etc. make it one of the most in-demand
programming languages of the present IT industry.
According to TIOBE index, as of January 2020, Python is one of the popular programming
languages. By looking at the popularity of this programming language, many IT
professionals, both beginners as well as experienced alike, are willing to build their career
as a Python developer.
Image source
Many people have daunting questions like:
• How one can start to learn Python?
• What are the fundamental concepts you need to know to learn Python?
With an aim to help similar concerns, Simpliv is presenting this blog to discuss about the
various fundamental concepts of Python programming and take you along to start writing
Python programs on your own.
Before proceeding further, at this point, we would like to suggest that you read blog (first
blog in this series) on introduction to Python programming language.
Without further ado, let us quickly look at the topics we will be covering in this blog:
Step 2:
Select Downloads and download python.exe file for Windows.
Step 3:
Once the installer is downloaded, run the Python installer. Check on Install launcher for all
users.
Step 4:
Select Customize installation. Check on all settings Document, pip, tcl/tk, python test suite,
py launcher, for all users. Click on Next.
Step 5:
This takes you to Advanced Options available while installing Python. Here, select
the Install for all users and Add Python to environment variables check boxes.
Step 6:
Once installation is done, you can see that the installation is successful.
Step 7:
Add Environment variables to run Python in Windows.
In the Start menu, search for “advanced system settings”. Select “View advanced system
settings”. In the “System Properties” window, click on the “Advanced” tab and then click on
the “Environment Variables” button.
Locate the Python installation directory on your system. If you followed the steps exactly as
above, Python will be installed in below locations:
• C:\Program Files (x86)\Python37-32: for 32-bit installation
• C:\Program Files\Python37-32: for 64-bit installation.
Step 8: Verify Python Installation
You have now successfully installed Python 3.8.1 on Windows. You can verify if the Python
installation is successful through the command line.
Search for the command prompt and type “python”. You can see that Python 3.8.1 is
successfully installed.
>>>
Type the following text at the Python prompt and press Enter –
print (“Hello world”)
Now you will get the output as:
Hello world
In this program, we are using built-in print() function to print the string Hello world as
the output. String is a sequence of characters and in Python they are enclosed inside single
quotes (‘’) , double quotes(“ ”) or in triple quotes(“”” “””).
If you want to print a list of five colors, you can write the program in the Python prompt as
follows:
Red
Blue
Yellow
Orange
Saffron
• Functions
• Classes
• Modules
• Packages
• Lists.
1. Functions:
In Python programming language a function is referred to as a collection of statements
grouped under a name. You can use it whenever you there is a need to execute all those
statements at a time. You can call the function as many times as you want it in your
program. Function may return a value.
2. Classes:
Python is an Objected Oriented programming language. This language supports classes and
objects. A class is an abstract data type. An object is a real world entity and an instance of a
class.
3. Modules:
A collection of related classes and functions can be called as a module. These are some of
the different types of modules:
• mathematical calculations
• string calculations
• web programming, etc.
4. Packages: Python package is a collection of related modules. Developers can either
import a package or can develop their own.
5. Lists: Lists can be a collection of values. They are declared in the CSV (Comma Separated
Values) format and enclosed in square brackets.
Python identifiers:
Python identifier is a name that is used to identify a variable, function, class, module or
other object. It helps to differentiate one entity from another.
Some of the rules for writing identifiers are as follows:
Did You Know?: “Python is a case-sensitive language. In Python myname and Myname
are not the same.”
• The first character of the variable should be an alphabet or underscore (_)
• Identifiers can be a combination of letters. They can be written in lower case (a to
z) or upper case (A to Z) or in digits (0 to 9) or an underscore(_). For example:
Names like myFirstprogram, var_1, display_this_on_screen etc.
• Identifiers must not contain any white space or special characters (!, @, #, %, ^, &, *
)
• Keywords cannot be used as identifiers
• Identifiers can be of any length.
Python Reserved words (Python keywords)
Python keywords are the reserved words. These keywords are used to convey a special
meaning to the compiler/interpreter. Keywords cannot be used as a variable name,
function name or any other identifier.
Did You Know?: “Python 3.7 has 33 keywords and the number can vary slightly in
coming days.”
All the keywords in Python are lowercase except True, False and None.
The following table gives the list of keywords used in Python.
True False None and As
assert break Continue for Lambda
try from Nonlocal while Del
global not With elif If
or yield Assert else Import
pass break Except in Raise
finally is Return
The above table provides a list of keywords. However, the list may get vary according to
different Python versions. You can type the following in the prompt to get the list of all the
available keywords in the current version of Python being used.
import keyword
print (keyword.kwlist)
Now you will get the output of all the available keywords in your Python version.
Indentation
Some of the programming languages such as Java, C, etc. use braces { } to define the block of
code. But Python uses indentation.
The number of spaces is variable, but it should be noted that all the statements within the
block must be indented the same amount. The usage of indentation makes the Python code
look neat and clean.
Generally, four whitespaces are used for indentation and that is preferred over using tabs.
The following code is an example of indentation:
Program:
if True;
print “True”;
else;
print “False”
Quotation in Python
To denote string literals Python accepts single (‘), double (‘’), triple (‘’’) quotes as long as
the same type of quote starts and ends the string.
The below example shows how to use quotations in Python.
single_quotation ‘word’
double_quotation “Python is a powerful programming
language”
triple _quotation “””Python is a powerful programming
language. It is open-source, and object
oriented programming language”””
Comments in Python
Comments play a very important role while writing a program. It describes the program to
the user so that they can easily understand the program.
In Python hash (#) is used to start writing a comment. The interpreter does not interpret
the comment.
Python supports two types of comments. They are:
1. Single Line Comment.
2. Multi-Line Comment.
1. Single Line Comment:
Single Line Comment starts with ?#?
The following program has single line comment.
Hello world
Here you can note that only the output Hello world is displayed on the console as the
output and the above comment written with # tag is not displayed.
2. Multi-Line Comment:
Developers can write multiple lines of comments to their code. It can be done in two ways.
They are:
• By using # tag before start of each line of comment
• By using triple comment at the start and at the end of the comment.
We will see a program example for these types below.
Thank You for previewing this eBook
You can read the full version of this eBook in different formats:
To download this full book, simply select the format you desire below