Chap 1
Chap 1
INTRODUCTION TO PYTHON
History, Features and Applications of python:
Python is a widely-used general-purpose, high-level programming language. It was initially
designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It
was mainly developed for emphasis on code readability, and its syntax allows programmers
to express concepts in fewer lines of code.
In the late 1980s, history was about to be written. It was that time when working on Python
started. Soon after that, Guido Van Rossum began doing its application-based work in
December of 1989 at Centrum Wiskunde & Informatica (CWI) which is situated in
the Netherlands. It was started firstly as a hobby project because he was looking for an
interesting project to keep him occupied during Christmas. The programming language in
which Python is said to have succeeded is ABC Programming Language, which had
interfacing with the Amoeba Operating System and had the feature of exception handling. He
had already helped to create ABC earlier in his career and he had seen some issues with ABC
but liked most of the features. After that what he did was really very clever. He had taken the
syntax of ABC, and some of its good features. It came with a lot of complaints too, so he
fixed those issues completely and had created a good scripting language that had removed all
the flaws. The inspiration for the name came from BBC’s TV Show – ‘Monty Python’s
Flying Circus’, as he was a big fan of the TV show and also, he wanted a short, unique and
slightly mysterious name for his invention and hence he named it Python! He was the
“Benevolent dictator for life” (BDFL) until he stepped down from the position as the leader
on 12th July 2018. For quite some time he used to work for Google, but currently, he is
working at Dropbox.
The language was finally released in 1991. When it was released, it used a lot fewer codes to
express the concepts, when we compare it with Java, C++ & C. Its design philosophy was
quite good too. Its main objective is to provide code readability and advanced developer
productivity. When it was released, it had more than enough capability to provide classes
with inheritance, several core data types exception handling and functions.
Features of python:
2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple example, the hello
world program you simply type print("Hello World"). It will take only one line to execute,
while Java or C takes multiple lines.
3) Interpreted Language
Python is an interpreted language; it means the Python program is executed one line at a time.
The advantage of being interpreted language, it makes debugging easy and portable.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh,
etc. So, we can say that Python is a portable language. It enables programmers to develop the
software for several competing platforms by writing a program only once.
Python is freely available for everyone. It has a large community across the world that is
dedicatedly working towards make new python modules and functions. Anyone can contribute
to the Python community. The open-source means, "Anyone can download its source code
without paying any penny."
6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects come into
existence. It supports inheritance, polymorphism, and encapsulation, etc. The object-oriented
procedure helps to programmer to write reusable code and develop applications in less code.
7) Extensible
It implies that other languages such as C/C++ can be used to compile the code and thus it can
be used further in our Python code. It converts the program into byte code, and any platform
can use that byte code.
It provides a vast range of libraries for the various fields such as machine learning, web
developer, and also for the scripting. There are various machine learning libraries, such as
Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are the popular
framework for Python web development.
Graphical User Interface is used for the developing Desktop application. PyQT5, Tkinter, Kivy
are the libraries which are used for developing the web application.
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code line
by line like C,C++ Java. It makes easy to debug the code.
11) Embeddable
The code of the other programming language can use in the Python source code. We can use
Python source code in another programming language as well. It can embed other language
into our code.
In Python, we don't need to specify the data-type of the variable. When we assign some value
to the variable, it automatically allocates the memory to the variable at run time. Suppose we
are assigned integer value 15 to x, then we don't need to write int x = 15. Just write x = 15.
Applications of python:
Web Development.
Game Development.
Machine Learning and Artificial Intelligence.
Data Science and Data Visualization.
Desktop GUI.
Web Scraping Applications.
Business Applications.
Audio and Video Applications.
Identifiers:
Identifiers are the name given to variables, classes, methods, etc. For example,
language=python
Here, language is a variable (an identifier) which holds the value 'Python'.
We cannot use keywords as variable names as they are reserved names that are built-in to
Python.
example,
continue=’python’
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
+,-,*,/,%,**(exponent),//(floor division)
=,+=,-=,==,*=,/=,**=,//=
==,<,>,<=,>=,!=
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
is not- Returns True if both variables are not the same object
Python Membership Operators
in- Returns True if a sequence with the specified value is present in the object
not in- Returns True if a sequence with the specified value is not present in the object.
Operator Precedence
() parenthesis
** Exponentiation
*, /, //, %
| bitwise or
^ bitwise xor
== != > >= < <= is is not in not in comparison,identity and membership operators
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Indentation: indentation is a way of telling a Python interpreter that the group of statements
belongs to a particular block of code.
#This is a comment
print("Hello, World!")
Console (also called Shell) is basically a command line interpreter that takes input from the
user i.e one command at a time and interprets it. If it is error free then it runs the command
and gives required output otherwise shows the error message. A Python Console looks like
this.
Accepting Input from Console User enters the values in the Console and that value is then
used in the program as it was required. To take input from the user we make use of a built-
in function input().
# input
input1 = input()
# output
print(input1)
1. Typecasting the input to Integer: There might be conditions when you might
require integer input from the user/Console, the following code takes two
input(integer/float) from the console and typecasts them to an integer then prints the
sum.
# input
num1 = int(input())
num2 = int(input())
# printing the sum in integer
print(num1 + num2)
2. Typecasting the input to Float: To convert the input to float the following code
will work out.
# input
num1 = float(input())
num2 = float(input())
# printing the sum in float
print(num1 + num2)
3. Typecasting the input to String: All kinds of input can be converted to string type
whether they are float or integer. We make use of keyword str for typecasting.
we can also take input string by just writing input() function by default it makes the input
string
# input
string = str(input())
# output
print(string)
# Or by default
string_default = input()
# output
print(string_default)
# string variable
a = "5.9"
# typecast to float
n = float(a)
print(n)
print(type(n))
Multiple interrelated modules are stored in a library. And whenever we need to use a
module, we import it from its library. In Python, it’s a very simple job to do due to its easy
syntax. We just need to use import.
Control flow statements: control statements are the statements which controls the flow of
execution.
if statement
The if statement is the most simple decision-making statement. It is used to decide whether
a certain statement or block of statements will be executed or not.
Syntax:
if condition:
# Statements to execute if
# condition is true
if-else: if a condition is true it will execute a block of statements and if the condition is
false it won’t. if the condition is false, we can use the else statement.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Nested-If Statement:
Nested if statements mean an if statement inside another if statement. Yes, Python allows us
to nest if statements within if statements. i.e., we can place an if statement inside another if
statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
If-elif-else ladder: The if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed, and the
rest of the ladder is bypassed. If none of the conditions is true, then the final else statement
will be executed.
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
For Loop: are used for sequential traversal. For example: traversing a list or string or array
etc.
Syntax:
for iterator_var in sequence:
statements(s)
Program to illustrate
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)
break statement: used to skip parts of the current loop or break out of the loop completely.
for i in range(5):
if i == 3:
break
print(i)
Continue statement: The continue keyword is used to end the current iteration in a for
loop (or a while loop), and continues to the next iteration.
for i in range(9):
if i == 3:
continue
print(i)