0% found this document useful (0 votes)
4 views65 pages

1. Introduction to Python-1

Python was created by Guido Van Rossum in the late 1980s, with its first version released in 1994. It is a high-level, interpreted, and object-oriented programming language known for its readability and ease of use, with applications ranging from web development to scientific computing. Python supports various data types and features, including a large standard library, making it versatile for different programming needs.

Uploaded by

eddinmoses99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views65 pages

1. Introduction to Python-1

Python was created by Guido Van Rossum in the late 1980s, with its first version released in 1994. It is a high-level, interpreted, and object-oriented programming language known for its readability and ease of use, with applications ranging from web development to scientific computing. Python supports various data types and features, including a large standard library, making it versatile for different programming needs.

Uploaded by

eddinmoses99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

History of Python

Python laid its foundation in the late 1980s.


The implementation of Python was started in the December 1989 by Guido Van
Rossum at CWI in Netherland.
In February 1991, van Rossum published the code (labeled version 0.9.0) to
alt.sources.
In 1994, Python 1.0 was released with new features like: lambda, map, filter, and
reduce.
Python 2.0 added new features like: list comprehensions, garbage collection
system.
On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was
designed to rectify fundamental flaw of the language.
ABC programming language is said to be the predecessor of Python language
which was capable of Exception Handling and interfacing with Amoeba Operating
System.
Python Versions
Python Version Released Date
Python 1.0 January 1994
Python 1.5 December 31, 1997
Python 1.6 September 5, 2000
Python 2.0 October 16, 2000
Python 2.1 April 17, 2001
Python 2.2 December 21, 2001
Python 2.3 July 29, 2003
Python 2.4 November 30, 2004
Python 2.5 September 19, 2006
Python 2.6 October 1, 2008
Python 2.7 July 3, 2010
Python 3.0 December 3, 2008
Python 3.2 February 20, 2011
Python 3.3 September 29, 2012
Python 3.4 March 16, 2014
Python 3.5 September 13, 2015
Python 3.6 December 23, 2016
Python 3.7 June 27, 2018
Features of Python
Easy to Learn and Use : Python is easy to learn and use. It is developer-friendly and
high level programming language.
Expressive Language : Python language is more expressive means that it is more
understandable and readable.
Interpreted Language : Python is an interpreted language i.e. interpreter executes the
code line by line at a time. This makes debugging easy and thus suitable for beginners.
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.
Free and Open Source : Python language is freely available at official web address.
The source-code is also available. Therefore it is open source.
Object-Oriented Language : Python supports object oriented language and
concepts of classes and objects come into existence.
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.
Large Standard Library : Python has a large and broad library and provides rich
set of module and functions for rapid application development.
GUI Programming Support : Graphical user interfaces can be developed using
Python.
Integrated : It can be easily integrated with languages like C, C++, JAVA etc.
How Python runs?
When the Python software is installed on your machine, minimally, it has:
an interpreter
a support library.
The interpreter
Interpreter is nothing but a software which can run your Python scripts.
Interestingly, it can be implemented in any programming language!
CPython is the default interpreter for Python which is written in C
programming language.
Jython is another popular implementation of python interpreter written
using Java programming language.

Programmer’s view of interpreter


If you have been coding in Python for sometime, you must have heard about
the interpreter at least a few times.
From a programmer’s perspective, an interpreter is simply a software
which executes the source code line by line.
Python’s view of interpreter
Now, let us scan through the python interpreter and try to
understand how it works.
Have a look at the diagram shown below:
Compiler compiles your source code (the statements in your file) into a
format known as byte code.
As soon as source code gets converted to byte code, it is fed into PVM (Python
Virtual Machine).
 The PVM is the runtime engine of Python;
PyPy is an implementation of Python which does not use an interpreter! It is
implemented using something called just-in-time compiler!
Interestingly, it often runs faster than the standard implementation of
Python, CPython.
Whenever a Python script is executed, the byte code is generated in memory and
simply discarded when program exits.
But, if a Python module is imported, a .pyc file for the module is generated which
contains its Byte code.
Thus, when the module is imported next time, the byte code from .pyc file is used,
hence skipping the compilation step!
Python Application
Python is known for its general purpose nature that makes it
applicable in almost each domain of software development.
Python as a whole can be used in any sphere of development.
Here, we are specifying applications areas where python can
be applied.
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,
beautifulSoup, Feedparser etc.
It also provides Frameworks such as Django, Pyramid, Flask
etc to design and develop web based applications. Some
important developments are: PythonWikiEngines, Pocoo,
PythonBlogSoftware etc.
2) Desktop GUI Applications
Python provides Tk GUI library to develop user interface in
python based application.
Some other useful toolkits wxWidgets, Kivy, pyqt that are
useable on several platforms.
The Kivy is popular for writing multitouch applications.
3) Software Development
Python is helpful for software development process.
It works as a support language and can be used for build
control and management, testing etc.
4) Scientific and Numeric
Python is popular and widely used in scientific and numeric
computing.
Some useful library and package are SciPy, Pandas, IPython
etc.
SciPy is group of packages of engineering, science and
mathematics.
5) Business Applications
Python is used to build Business applications like ERP and e-
commerce systems.
Tryton is a high level application platform.
6) Console Based Application
We can use Python to develop console based applications.
For example: IPython.
7) Audio or Video based Applications
Python is awesome to perform multiple tasks and can be used
to develop multimedia applications.
Some of real applications are: TimPlayer, cplay etc.
8) 3D CAD(Computer Aided Design) Applications
To create CAD application Fandango is a real application
which provides full features of CAD.
9) Enterprise Applications
Python can be used to create applications which can be used
within an Enterprise or an Organization.
Some real time applications are: OpenErp, Tryton, Picalo
etc.
10) Applications for Images
Using Python several application can be developed for image.
 Applications developed are: VPython, Gogh, imgSeek etc.
Python Variables
Variable is a name which is used to refer memory location.
Variable is also known as identifier and used to hold value.
In Python, we don't need to specify the type of variable
because Python is a type infer language and smart enough to get
variable type.
Variable names can be a group of both letters and digits,
but they have to begin with a letter or an underscore.
It is recommended to use lowercase letters for variable
name. Rahul and rahul both are two different variables.
Identifier
Variables are the example of identifiers.
An Identifier is used to identify the literals used in the
program.
The rules to name an identifier are given below.
1. The first character of the variable must be an alphabet or
underscore ( _ ).
2. All the characters except the first character may be an
alphabet of lower-case(a-z), upper-case (A-Z),
underscore or digit (0-9).
3. Identifier name must not contain any white-space, or
special character (!, @, #, %, ^, &, *).
4. Identifier name must not be similar to any keyword
defined in the language.
5. Identifier names are case sensitive for example myname,
and MyName is not the same.
6. Examples of valid identifiers : a123, _n, n_9, etc.
7. Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
Python does not bound us to declare variable before using in
the application.
It allows us to create variable at required time.
We don't need to declare explicitly variable in Python.
When we assign any value to the variable that variable is
declared automatically.
The equal (=) operator is used to assign value to a variable.
Multiple Assignment
Python allows us to assign a value to multiple variables in a
single statement which is also known as multiple assignment.
We can apply multiple assignments in two ways either by
assigning a single value to multiple variables or assigning
multiple values to multiple variables. Lets see given examples.
1. Assigning single value to multiple variables
x=y=z=50
print x
print y
print z
2.Assigning multiple values to multiple variables:
a,b,c=5,10,15
print a
print b
print c
Python Data Types
Variables can hold values of different data types.
Python is a dynamically typed language hence we need not
define the type of the variable while declaring it.
The interpreter implicitly binds the value with its type.
Python enables us to check the type of the variable used in the
program.
Python provides us the type() function which returns the type
of the variable passed.
A=10
b="Hi Python"
c = 10.5
print(type(a));
print(type(b));
print(type(c));
Standard data types :
A variable can hold different types of values.
For example, a person's name must be stored as a string
whereas its id must be stored as an integer.
Python provides various standard data types that define the
storage method on each of them.
The data types defined in Python are given below.
Numbers
String
List
Tuple
Dictionary
1. Numbers
Number stores numeric values.
Python creates Number objects when a number is assigned to
a variable.
For example :
a = 3 , b = 5 #a and b are number objects
Python supports 4 types of numeric data.
int (signed integers like 10, 2, 29, etc.)
long (long integers used for a higher range of values
like 908090800L, -0x1929292L, etc.)
float (float is used to store floating point numbers
like 1.9, 9.902, 15.2, etc.)
complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.)
Python allows us to use a lower-case L to be used with long
integers.
However, we must always use an upper-case L to avoid
confusion.
A complex number contains an ordered pair, i.e., x + iy
where x and y denote the real and imaginary parts
respectively).
2. String
The string can be defined as the sequence of characters
represented in the quotation marks.
In python, we can use single, double, or triple quotes to define
a string.
String handling in python is a straightforward task since there
are various inbuilt functions and operators provided.
In the case of string handling, the operator + is used to
concatenate two strings as the operation "hello"+"
python" returns "hello python".
The operator * is known as repetition operator as the operation
"Python " *2 returns "Python Python ".
For example :
str1 = 'hello javatpoint' #string str1
str2 = ' how are you' #string str2
print (str1[0:2])
#printing first two character using slice operator
print (str1[4])
#printing 4th character of the string
print (str1*2)
#printing the string twice
print (str1 + str2)
#printing the concatenation of str1 and str2
3. List
Lists are similar to arrays in C.
the list can contain data of different types.
The items stored in the list are separated with a comma (,) and
enclosed within square brackets [].
We can use slice [:] operators to access the data of the list.
The concatenation operator (+) and repetition operator (*)
works with the list in the same way as they were working with
the strings.
For example :
l = [1, "hi", "python", 2]
print (l[3:]);
print (l[0:2]);
print (l);
print (l + l);
print (l * 3);
Output :
[2]
[1, 'hi']
[1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
[1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
4. Tuple
A tuple is similar to the list in many ways.
Like lists, tuples also contain the collection of the items of
different data types.
The items of the tuple are separated with a comma (,) and
enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the
size and value of the items of a tuple.
For Example :
t = ("hi", "python", 2)
print (t[1:]);
print (t[0:1]);
print (t);
print (t + t);
print (t * 2);
print (type(t)) Output :
t[2] = "hi"; ('python', 2)
('hi',)
('hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python',2)
<type 'tuple'> Traceback (most recent call last):
File "main.py", line 8, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support
item assignment
5. Dictionary
Dictionary is an ordered set of a key-value pair of items.
It is like an associative array or a hash table where each key
stores a specific value.
Key can hold any primitive data type whereas value is an
arbitrary Python object.
The items in the dictionary are separated with the comma and
enclosed in the curly braces {}.
For Example :
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'};
print("1st name is "+d[1]);
print("2nd name is "+ d[4]);
print (d);
print (d.keys());
print (d.values());
Output :
1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
[1, 2, 3, 4]
['Jimmy', 'Alex', 'john', 'mike']
Python Keywords
Python Keywords are special reserved words which convey a
special meaning to the compiler/interpreter.
Each keyword have a special meaning and a specific
operation.
These keywords can't be used as variable.
Following is the List of Python Keywords.

True False None and as


asset def class continue break
else finally elif del except
global for if from import
raise try or return pass
nonlocal in not is lambda
Python Literals
Literals can be defined as a data that is given in a variable or
constant.
Python support the following literals:
String literals
String literals can be formed by enclosing a text in the quotes.
We can use both single as well as double quotes for a String.
Eg:
"Aman" , '12345'
Types of Strings:
There are two types of Strings supported in Python:
a).Single line String- Strings that are terminated within a single
line are known as Single line Strings.
Eg:
>>> text1='hello'
b).Multi line String- A piece of text that is spread along multiple
lines is known as Multiple line String.
Using triple quotation marks:-
>>> str2='''welcome
to
SSSIT'''
>>> print str2
welcome
to
SSSIT
Adding black slash at the end of each line.
>>> text1='hello\
user'
>>> text1
hellouser
Numeric literals
Numeric Literals are immutable.
Numeric literals can belong to following four different
numerical types.

Int(signed Long(long float(floating Complex(co


integers) integers) point) mplex)
Numbers( can Integers of Real numbers In the form of
be both unlimited size with both a+bj where a
positive and followed by integer and forms the real
negative) with lowercase or fractional part part and b
no fractional uppercase L eg: -26.2 forms the
part.eg: 100 eg: imaginary part
87032845L of complex
number. eg:
3.14j
Boolean literals
A Boolean literal can have any of the two values: True or
False.
Special literals
Python contains one special literal i.e., None.
None is used to specify to that field that is not created. It is
also used for end of lists in Python.
>>> val1=10
>>> val2=None
>>> print (val1)
10
>>> print (val2)
None
Literal Collections
Collections such as tuples, lists and Dictionary are used in
Python.
List:
List contain items of different data types. Lists are
mutable i.e., modifiable.
The values stored in List are separated by commas(,) and
enclosed within a square brackets([]). We can store different
type of data in a List.
Value stored in a List can be retrieved using the slice
operator([] and [:]).
The plus sign (+) is the list concatenation and asterisk(*)
is the repetition operator.
>>> list=['aman',678,20.4,'saurav']
>>> list1=[456,'rahul']
>>> list
['aman', 678, 20.4, 'saurav']
>>> list[1:3]
[678, 20.4]
>>> list+list1
['aman', 678, 20.4, 'saurav', 456, 'rahul']
>>> list1*2
[456, 'rahul', 456, 'rahul']
>>>
Python Operators
The operator can be defined as a symbol which is responsible
for a particular operation between two operands.
Operators are the pillars of a program on which the logic is
built in a particular programming language.
Python provides a variety of operators described as follows.
1. Arithmetic operators
2. Comparison operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations
between two operands.
It includes +(addition), - (subtraction), *(multiplication),
/(divide), %(reminder), //(floor division), and exponent (**).
Consider the following table for a detailed explanation of
arithmetic operators.
Operator Description

+ (Addition) It is used to add two operands. For example, if a = 20,


b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first
operand. If the first operand is less than the second
operand, the value result negative. For example, if a =
20, b = 10 => a ? b = 10
/ (divide) It returns the quotient after dividing the first operand
by the second operand. For example, if a = 20, b = 10
=> a/b = 2
* (Multiplication) It is used to multiply one operand with the other. For
example, if a = 20, b = 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand
by the second operand. For example, if a = 20, b = 10
=> a%b = 0
** (Exponent) It is an exponent operator represented as it calculates
the first operand power to second operand.
// (Floor division) It gives the floor value of the quotient produced by
dividing the two operands.
Comparison operator
Comparison operators are used to comparing the value of the
two operands and returns Boolean true or false accordingly.
The comparison operators are described in the following table.

Operator Description
== If the value of two operands is equal, then the condition
becomes true.
!= If the value of two operands is not equal then the condition
becomes true.
<= If the first operand is less than or equal to the second operand,
then the condition becomes true.
>= If the first operand is greater than or equal to the second
operand, then the condition becomes true.
<> If the value of two operands is not equal, then the condition
becomes true.
> If the first operand is greater than the second operand, then
the condition becomes true.
< If the first operand is less than the second operand, then the
condition becomes true.

Python assignment operators


The assignment operators are used to assign the value of the
right expression to the left operand.
The assignment operators are described in the following table.

Operator Description
= It assigns the the value of the right expression to the left
operand.
+= It increases the value of the left operand by the value of
the right operand and assign the modified value back to
left operand. For example, if a = 10, b = 20 => a+ = b
will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of
the right operand and assign the modified value back to
left operand. For example, if a = 20, b = 10 => a- = b will
be equal to a = a- b and therefore, a = 10.

*= It multiplies the value of the left operand by the value of


the right operand and assign the modified value back to
left operand. For example, if a = 10, b = 20 => a* = b
will be equal to a = a* b and therefore, a = 200.

%= It divides the value of the left operand by the value of the


right operand and assign the reminder back to left
operand. For example, if a = 20, b = 10 => a % = b will
be equal to a = a % b and therefore, a = 0.

**= a**=b will be equal to a=a**b, for example, if a = 4, b


=2, a**=b will assign 4**2 = 16 to a.

//= A//=b will be equal to a = a// b, for example, if a = 4, b =


3, a//=b will assign 4//3 = 1 to a.
Bitwise operator
The bitwise operators perform bit by bit operation on the
values of the two operands.
Operator Description
& (binary and) If both the bits at the same place in two operands
are 1, then 1 is copied to the result. Otherwise, 0 is
copied.

| (binary or) The resulting bit will be 0 if both the bits are zero
otherwise the resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are
different otherwise the resulting bit will be 0.

~ (negation) It calculates the negation of each bit of the operand,


i.e., if the bit is 0, the resulting bit will be 1 and vice
versa.
<< (left shift) The left operand value is moved left by the number
of bits present in the right operand.

>> (right shift) The left operand is moved right by the number of
bits present in the right operand.

Logical Operators
The logical operators are used primarily in the expression
evaluation to make a decision.
Operator Description
and If both the expression are true, then the condition will be
true. If a and b are the two expressions, a → true, b → true
=> a and b → true.
or If one of the expressions is true, then the condition will be
true. If a and b are the two expressions, a → true, b → false
=> a or b → true.
not If an expression a is true then not (a) will be false and vice
versa.
Membership Operators
Python membership operators are used to check the
membership of value inside a data structure.
If the value is present in the data structure, then the resulting
value is true otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the
second operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in
the second operand (list, tuple, or dictionary).
Identity Operators

Operator Description
is It is evaluated to be true if the reference present at both sides
point to the same object.

is not It is evaluated to be true if the reference present at both side


do not point to the same object.
Operator Precedence
The precedence of the operators is important to find out since
it enables us to know which operator should be evaluated first.
The precedence table of the operators in python is given
below.
Operator Description
** The exponent operator is given priority over all
the others used in the expression.
~+- The negation, unary plus and minus.
* / % // The multiplication, divide, modules, reminder,
and floor division.
+- Binary plus and minus
>> << Left shift and right shift
& Binary and.

^| Binary xor and or

<= < > >= Comparison operators (less then, less then
equal to, greater then, greater then equal to).

<> == != Equality operators.

= %= /= //= -= += Assignment operators


*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators


Python Comments
Comments in Python can be used to explain any program
code. It can also be used to hide the code as well.
Comments are the most helpful stuff of any program.
It enables us to understand the way, a program works.
In python, any statement written along with # symbol is
known as a comment.
The interpreter does not interpret the comment.
Comment is not a part of the program, but it enhances the
interactivity of the program and makes the program readable.
Python supports two types of comments:
Single Line Comment:
In case user wants to specify a single line comment, then
comment must start with #
# This is single line comment.
print "Hello Python"
13.2. Multi Line Comment:
Multi lined comment can be given inside triple quotes.
''' This
Is
Multiline comment'''

You might also like