Professional Documents
Culture Documents
Presented by
-------------------------
Dr Dileep Kumar M
Contents Of UNIT-I
Introduction
History of Python Programming
Features
Applications
Versions ,Flavors in Python
Programming Modes
Download & Install the Python in Windows & Linux
How to set Python Environment in the System?
Downloading and Installing Anaconda, Jupyter
Notebook & Spyder
Python IDE - Jupyter Notebook Environment,
Spyder Environment
Introduction
Interactive
Interpreted
Data Science
Artificial Intelligence
Machine Learning
Deep Learning
Data Analysis
NLP
Neural Networks
IOT
Robotics
Existing Vs Python
Comparing Python to Other Languages
2. Script mode
1. Immediate mode: (in windows)
In the mode, you type Python expressions into the Python Interpreter
window, and the interpreter immediately shows the results.
2. Script mode (in windows)
we can open IDLE python then click on file, open new file
and we can write python program and save as filename.py.
After that ,to run this program press F5 button or click run
button click Run module.
Python Versions:
The implementation of Python was started in December 1989.
In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to alt.sources.
2. JPython:
3. IronPython:
4. PyPy:
Contd…
5. Ruby Python:
6. Pythonxy:
7.Anaconda Python:
8. Stackless Python:
Download & Install the Python in Windows
https://www.python.org/downloads/
Installing Python in Windows
After downloading Run python-3.8.5. exe file. Install it and click next.
In window search button, type IDLE python, we get version of python
installed
python interpreter:
Installing Python in ubuntu
1. Open terminal via Ctrl+Alt+T or searching for “Terminal” from app launcher.
Anaconda is not an IDE, Anaconda is a Python distribution, (including for commercial use and
redistribution). It includes more than 400 of the most popular Python packages for science,
math, engineering, and data analysis.
Python IDE - Jupyter Notebook
Environment
Python IDE - Spyder Environment
Spyder Environment
Microsoft Visual Studio
Later, if you change the value of n and use it again, the new value will be
substituted instead
Python Identifiers:
Identifier is a name used to identify a variable, function,
class or a module.
Abc10d 99
ABC_DE 9abc
_abc X+Y
_ For
Reserved Keywords:
if 5>2:
Print(“five is greater than two!”)
This gives an error as there is an indentation missing.
if 5>2:
print(“five is greater than two!”)
Quotations:
1. Integer
2. Floating number
3. Complex numbers
4. Boolean and
5. Strings
INTEGER:
Eg: a=4.2
b=4. or .2
c= .4e7
d= 4.2e-4
COMPLEX NUMBERS:
Eg: a=2-14j
b= 2.0+3j
BOOLEAN:
Represented as <class ‘bool’>.
It is the datatype with two built-in values, “True” or “False”.
Non-Boolean objects can also be evaluated in Boolean
context.
Eg: a=6;b=7
a>b
print(type(True))
print(type(False))
print(type(false))
STRINGS:
Represented as <class ‘str’>.
These are arrays of bytes representing Unicode characters.
These are the collection of characters in a ‘’,””,’’’.
Here we don’t have any datatype like char in python.
1. How to create a string?
2. How to access the values of a string?
I C F A I T E C H s
Example:
Eg: t=(1,3,2,4)
l=[5,6,7,8]
T=tuple(l) L=list(t)
print(T) print(L)
print(type(T)) print(type(L))
Python Operators:
The operator can be defined as a symbol which is responsible
for a particular operation between two operands
Python provides a variety of operators.
1. Arithmetic Operators
2. Comparison Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
7. Identity Operators.
Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations
between two operands.
Comparison Operators:
Insert: To add an item at the specified index, use the insert () method:
>>> x=[1,2,4,6,7]
>>> x.insert(2,10) #insert(index no, item to be inserted)
>>> x
[1, 2, 10, 4, 6, 7]
-------------------------
>>> x.insert(4,['a',11])
>>> x
[1, 2, 10, 4, ['a', 11], 6, 7]
Pop & Remove methods
Pop: The pop() method removes the specified index, (or the last item if index
is not specified) or simply pops the last item of list and returns the item.
>>> x=[1, 2, 10, 4, 6, 7]
>>> x.pop()
7
>>> x
[1, 2, 10, 4, 6]
-----------------------------------
>>> x=[1, 2, 10, 4, 6]
>>> x.pop(2)
10
>>> x
[1, 2, 4, 6]
Remove: The remove() method removes the specified item from a given list.
>>> x=[1,33,2,10,4,6]
>>> x.remove(33)
>>> x
[1, 2, 10, 4, 6]
>>> x.remove(4)
>>> x
[1, 2, 10, 6]
Reverse & Sort
Reverse: Reverse the order of a given list.
>>> x=[1,2,3,4,5,6,7]
>>> x.reverse()
>>> x
[7, 6, 5, 4, 3, 2, 1]
for i in range(length):
print(list[i])
Output:
1
3
5
7
9
Method #3: using while loop
SN Function Description
1 cmp(tuple1, tuple2) It compares two tuples and returns
true if tuple1 is greater than tuple2
otherwise false.
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was
found
Syntax:
tuple.count(element)
Where the element is the element that is to be counted.
Syntax:
tuple.index(element, start, end)
Parameters:
•element: The element to be searched.
•start (Optional): The starting index from where the searching is started
•end (Optional): The ending index till where the searching is done
Count
Count of
of 33 in
in Tuple1
Tuple1 is:
is: 33 Count
Count of
of Python
Python in
in Tuple2
Tuple2 is:
is: 33
# Creating tuples
Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
Tuple2 = ('python', 'geek', 'python',
'for', 'java', 'python')
# Creating tuples
Tuple = (0, 1, 2, 3, 2, 3, 1, 3, 2)
Print(s[1])
TypeError: 'set' object is not subscriptable
Duplicate stings was ignored(without error)
Sets are mutable,but set elemets must be
immutable. Therefore set cannot have other
sets as elements.
s={'red',1,'green','b',10.5,1,'red',10.5}
Print(s)
{1, 'b', 'green', 'red', 10.5}
SET
A Python set is the collection of the unordered
items.
Each element in the set must be unique, immutable,
and the sets remove the duplicate elements.
Sets are mutable which means we can modify it
after its creation.
Sets are written with curly brackets.
thisset = {"apple", "banana", "cherry"}
CREATING A SET
The set can be created by enclosing the comma-separated
immutable items with the curly braces {}.
Python also provides the set() method, which can be used to
create the set by the passed sequence
It can contain any type of element such as integer, float,
tuple etc. But mutable elements (list, dictionary, set) can't
be a member of set.
1. s1={10,20,30,(5,6,7),40}
s1
{(5, 6, 7), 20, 40, 10, 30}
2. s1={10,20,30,[5,6,7],40}
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
s1={10,20,30,[5,6,7],40}
TypeError: unhashable type: 'list‘
3. s1={10,20,30,{5,6,7},40}
SET OPERATIONS
Set can be performed mathematical operation such as union,
intersection, difference, and symmetric difference.
Python provides the facility to carry out these operations
with operators or methods
ADDING ITEMS TO THE SET
Python provides the add() method and update()
method which can be used to add some particular
item to the set.
The add() method is used to add a single element
whereas the update() method is used to add
multiple elements to the set.
Months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(months)
print("\nAdding other months to the set...");
Months.add("July");
Months.add ("August");
print("\nPrinting the modified set...");
print(Months)
print("\nlooping through the set elements ... ")
for i in Months:
print(i)
REMOVING ITEMS FROM THE SET
Python provides the discard() method and remove() method
which can be used to remove the items from the set.
The difference between these function, using discard() function
if the item does not exist in the set then the set remain
unchanged whereas remove() method will through an error.
months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(months)
print("\nRemoving some months from the set...");
months.discard("January");
months.discard("May");
print("\nPrinting the modified set...");
print(months)
print("\nlooping through the set elements ... ")
for i in months:
print(i)
DIFFERENCE BETWEEN DISCARD() AND
REMOVE()
Despite the fact that discard() and remove() method both perform the
same task, There is one main difference between discard() and remove().
If the key to be deleted from the set using discard() doesn't exist in the
set, the Python will not give the error. The program maintains its control
flow.
On the other hand, if the item to be deleted from the set using remove()
doesn't exist in the set, the Python will raise an error.
The intersection of two sets can be performed by the and & operator
or the intersection() function. The intersection of the two sets is given
as the set of the elements that common in both sets.
The intersection_update() method removes the items from the
original set that are not present in both the sets (all the sets if more
than one are specified).
The intersection_update() method is different from the intersection()
method since it modifies the original set by removing the unwanted
items, on the other hand, the intersection() method returns a new set.
Frozenset = frozenset([1,2,3,4,5])
print(type(Frozenset))
print("\nprinting the content of frozen set...")
for i in Frozenset:
print(i);
Frozenset.add(6) #gives an error since we cannot change the content of Frozense
t after creation
DICTIONARY DATA STRUCTURE
Dictionaries are used to store data values in
key:value pairs.
A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
As of Python version 3.7, dictionaries
are ordered. In Python 3.6 and earlier,
dictionaries are unordered.
Dictionaries are written with curly brackets,
and have keys and values:
Duplicates Not Allowed
1. thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
2. x = thisdict.get("model")
3. x = thisdict.keys()
4. x = thisdict.values()
5. x = thisdict.items()
6. car["color"] = "red"
Change Dictionary Items
1. thisdict["year"] = 2018
2. thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
Remove Dictionary Items
1. thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
2. thisdict.popitem() #remove last element #before
3.7 version random element deleted
3. del thisdict["model"]
4. thisdict.clear()
INTRODUCTION
Python Dictionary is used to store the data in a key-value pair
format.
The dictionary is the data type in Python, which can simulate
the real-life data arrangement where some specific value
exists for some particular key.
It is the mutable data-structure.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
CREATING THE DICTIONARY
The dictionary can be created by using multiple key-value
pairs enclosed with the curly brackets {}, and each key is
separated from its value by the colon (:)
Python provides the built-in function dict() method which is
also used to create dictionary.
The empty curly braces {} is used to create empty dictionary
SN Function Description
1 cmp(dict1, It compares the items of both the dictionary and returns true if the
dict2) first dictionary values are greater than the second dictionary,
otherwise it returns false.