Unit 1
Unit 1
Unit 1
❖ History of Python
✓ It was initially designed by Guido van Rossum in 1991 and developed by Python Software
Foundation.
✓ There are two major Python versions- Python 2 and Python 3.
✓ On 16 October 2000, Python 2.0 was released with many new features.
✓ On 3rd December 2008, Python 3.0 was released with more testing and includes new features.
language language
Examples include Perl, PHP,Python, JavaScript, Examples include C, C++, Java, Python, etc.
etc.
Execution of a script takes less time as scripts are Execution of a program takes more time since
generally short. they are compiled.
❖ Python Features
✓ Python is object-oriented: Structure supports such concepts as polymorphism, operation
overloading and multiple inheritance.
✓ Indentation: Indentation is one of the greatest feature in python
✓ It is free (open source): Downloading python and installing python is free and easy.
✓ It is Powerful: It provides Dynamic typing, Built-in types and tools,Library utilities, and
Automatic memory management
✓ It is Portable: Python runs virtually every major platform used today
✓ It is easy to use and learn: It has no intermediate compile. Python Programs are compiled
automatically to an intermediate form called byte code, which the interpreter then reads.
✓ Interpreted Language: Python is processed at runtime by python Interpreter
✓ Interactive Programming Language:Users can interact with the python interpreter directly for
writing the programs
✓ Straight forward syntax: The formation of python syntax is simple and straight forward which
also makes it popular.
❖ Python Applications
1) Web Applications: We can use Python to develop web applications. It provides libraries to handle
internet protocols such as HTML and XML, JSON, Email processing, request, etc. One of Python
web-framework named Django is used on Instagram. Python provides many useful frameworks, and
these are given below:
2) Desktop GUI Applications:The GUI stands for the Graphical User Interface, which provides a
smooth interaction to any application. Python provides a Tk GUI library to develop a user interface.
4) Software Development: Python is useful for the software development process. It works as a
support language and can be used to build control and management, testing, etc.
5) Scientific and Numeric: Python language is the most suitable language for Artificial intelligence
or machine learning. It consists of many scientific and mathematical libraries, which makes easy to
solve complex calculations.
6) Business Applications: E-commerce and ERP are an example of a business application. This kind
of application requires extensively, scalability and readability, and Python provides all these features.
7) Audio or Video-based Applications : Python is flexible to perform multiple tasks and can be used
to create multimedia applications. Some multimedia applications which are made by using Python
are TimPlayer, cplay, etc.
8) 3D CAD Applications: The CAD (Computer-aided design) is used to design engineering related
architecture. It is used to develop the 3D representation of a part of a system. Python can create a 3D
CAD application
9) Enterprise Applications: Python can be used to create applications that can be used within an
Enterprise or an Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc.
10) Image Processing Application: Python contains many libraries that are used to work with the
image. The image can be manipulated according to our requirements. Examples of image processing
libraries are: OpenCV, Pillow
❖ Installing Python
There are many interpreters available freely to run Python scripts like IDLE (Integrated Development
Environment) which is installed when you install the python software from
http://python.org/downloads/
Steps to be followed and remembered:
Step 1: Select Version of Python to Install.
Step 2: Download Python Executable Installer.
Step 3: Run Executable Installer.
Step 4: Verify Python Was Installed On Windows.
Step 5: Verify Pip Was Installed.
Step 6: Add Python Path to Environment Variables (Optional)
❖ Elements of Python
Python has 4 components
1. IDLE (Python GUI): It is a cross platform GUI Integrated Development Environmental that is
provided with Python for editing and running a Python programs. It is a bundled set of software’s tools
such as Python Shell for Scripting programs and text editor for creating and modifying Python’s
source code, an integrated interactive debugger for finding error, help system etc.
2. Module Docs: This component allows us to access the python documents such as build in modules,
DLLs, libraries, packages etc.
3. Python (Command line): Python can also be access from the command line. Command line mode
provide very less features in comparison to IDLE but it is fast.
4. Python Manual : This component include various documents related to Python such as :
installation cum setup guide, tutorials, Python API , FAQs etc.
❖ Basic Structure of Python program
The typical structure of a python program include 3 parts:
Import statements
// import statements are used to include library files to the python program
Function definitions
//This section include the definitions of various functions written in a Python Program
Program statements
// This section include the set of statements for solving the given problem.
❖ Variables :When we create a program, we often need store values so that it can be used in a
program. We use variables to store data which can be manipulated by the computer program.
✓ Every variable has a name and memory location where it is stored.
✓ It Can be of any size
✓ Variable name Has allowed characters, which are a-z, A-Z, 0-9 and underscore (_)
✓ Variable name should begin with an alphabet or underscore
✓ Variable name should not be a keyword
✓ Variable name should be meaningful and short
✓ Generally, they are written in lower case letters
✓ A type or datatype which specify the nature of variable (what type of values can be stored in
✓ We can check the type of the variable by using type command
>>> type(variable_name)
✓ The operand to the left of the = operator is the name of the variable and the operand to the right of
the = operator is the value stored in the variable.
For example −
a= 100 # An integer assignment
b = 1000.0 # A floating point
c = "Ami Patel" # A string
print (a)
print (b)
print (c)
Output:
100
1000.0
Ami Patel
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously.
For example :
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same
memory location. You can also assign multiple objects to multiple variables.
For example −
a,b,c = 1,2,"abc“
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one
string object with the value "abc" is assigned to the variable c.
❖ Data types
1. Number: Number data type stores Numerical Values. These are of three different types:
a) Integer & Long
b) Float/floating point
c) Complex
a) Integers are the whole numbers consisting of + or – sign like 100000, -99, 0, 17.
e.g. age=19
salary=20000
b) Floating Point: Numbers with fractions or decimal point are called floating point numbers.A
floating point number will consist of sign (+,-) and a decimal sign(.)
e.g. temperature= -21.9,growth_rate= 0.98333328
2. Boolean: Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
3. Sequence: A sequence is an ordered collection of items, indexed by positive integers. Three types
of sequence data type available in Python are Strings, Lists & Tuples.
a)String: is an ordered sequence of letters/characters. They are enclosed in single quotes (‘’) or double
(“”).
Example
>>> a = ‘Neha’
>>>a=”Neha”
b) Lists: List is also a sequence of values of any type. Values in the list are called elements /items. The
items of list are accessed with the help of index (index start from 0). List is enclosed in square
brackets.
Example
Student = [“Ami Patel”, 567, “CS”]
c) Tuples: Tuples are a sequence of values of any type, and are indexed by integers. They are
immutable i.e. we cannot change the value of items of tuples . Tuples are enclosed in ().
Student = (“Ami Patel”, 567, “CS”)
4. Sets
Set is an unordered collection of values, of any type, with no duplicate entry. Sets are immutable.
Example
s = set ([1,2,3,4])
5. Dictionaries: Dictionaries store a key – value pairs, which are accessed using key. Dictionary is
enclosed in curly brackets.
Example
d = {1:'a', 2:'b', 3:'c'}
6. String: Strings in Python are identified as a contiguous set of characters represented in the
quotation marks. Python allows for either pairs of single or double quotes.
✓ 'hello' is the same as "hello".
✓ Strings can be output to screen using the print function.
For example: print("hello").
>>> print("Good Morning")
mrcet college
>>> type("Good Morning")
<class 'str'>
Literals:A literal is a constant that appear directly in a program and this value does not change during
the program execution i.e. remain fixed.
Example:
Num1=5
Principle_amount= 5000.00
Here 5 and 5000.00 are literals. Python support the following literals
✓ Numeric literals
✓ String Literals
✓ Special Literals
✓ Collection Literals
❖ Comments:
Single-line comments begins with a hash(#) symbol and is useful in mentioning that the
whole line should be considered as a comment until the end of line.
A Multi line comment is useful when we need to comment on many lines. In python, triple
double quote(“ “ “) and single quote(‘ ‘ ‘)are used for multi-line commenting.
❖ Type Casting
Type Casting is the method to convert the variable data type into a certain data type in order to the
operation required to be performed by users.
There can be two types of Type Casting in Python –
✓ Implicit Type Casting
✓ Explicit Type Casting
Implicit Type Casting: In this, methods, Python converts data type into another data type
automatically. In this process, users don’t have to involve in this process.
Example:
a= 7 #Python automatically converts a to int
print(type(a))
Output:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
Explicit Type Casting: In this method, Python need user involvement to convert the variable data
type into certain data type in order to the operation required.
Mainly in type casting can be done with these data type function:
• Int() : Int() function take float or string as an argument and return int type object.
• float() : float() function take int or string as an argument and return float type object.
• str() : str() function take float or int as an argument and return string type object.
Example:
a= 5
n = float(a) # typecast to float
print(n)
print(type(n))
Output:
5.0
<class 'float'>
Example:
age = int(input(“Enter your age”))
percentage= float(input(“Enter you percentage”))
❖ Operators: Operators are special symbols which represents specific operations. They are applied on
operand(s),which can be values or variables.
✓ Relational Operators : Relation operators are used to compare two items. The
result of relational operator is true or false.
✓ Logical Operators: Logical Operators give the logical relationship based upon the
truth table.
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise or operator: Returns 1 if either of the bit is 1 else 0.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Bitwise not operator: Returns one’s complement of the number.
Example:
a = 10 = 1010 (Binary)
~a = ~1010
= -(1010 + 1)
= -(1011)
= -11 (Decimal)
Bitwise xor operator: Returns 1 if one of the bits is 1 and the other is 0 else returns false.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
Bitwise left shift: Shifts the bits of the number to the left and fills 0 on right as a result. Example:
Example
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Bitwise right shift: Shifts the bits of the number to the right and fills 0 on left( fills 1 in the case of a
negative number) as a result.
Example:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
✓ Membership Operator: Membership operators are used to test if a sequence is presented in an
object:
Example:1
x = ["apple", "orange"]
print("apple" in x)
output:
true
Example:2
x = ["apple", "orange"]
print("pineapple" not in x)
output:
true
✓ Assignment Operators
Assignment operators are used to assign a value to the variable.