Professional Documents
Culture Documents
DINESHKUMAR THANGAVEL
PYTHON PROGRAMMING
CONTENT
Programming Languages
Introduction to PYTHON
What is Programming Language
✓ Programming Language is a collection of rules that command a computer, devices, applications to work according to
✓ The programing language enables us to write efficient programs and develop online solutions such as- mobile
✓ Programming is used to automate, maintain, assemble, measure and interpret the processing of the data and
information. It helps in accelerating the input and output of the devices or applications.
Programming Languages
➢ Low Level Language
▪ Assembly, Turtle
➢ Early programming languages
▪ COBOL, FORTRAN
➢ Middle level Languages
▪ C
➢ High Level Languages
▪ C++, Java, Python
INTRODUCTION TO PYTHON
Python Installation
Applications of Python
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
Introduction
Introduction
50%
❖ Python is a general purpose, high level,
40% interpreted and dynamically typed programming language.
60%
30% 70%
General Purpose
0%
Introduction
50%
❖ Python is a general purpose, high level,
40% interpreted and dynamically typed programming language.
60%
30% 70%
General Purpose
0%
Introduction
50%
❖ Python is a general purpose, high level,
40% interpreted and dynamically typed programming language.
60%
30% 70%
General Purpose
Python programs saved and execute in the same format in which it was
High Level 20% 80%where the code is first
created, as opposed to compiled languages
0%
Introduction
50%
❖ Python is a general purpose, high level,
40% interpreted and dynamically typed programming language.
60%
30% 70%
General Purpose
0%
Introduction
50%
❖ Python is a general purpose, high level,
40% interpreted and dynamically typed programming language.
60%
30% 70%
General Purpose
0%
50%
40% 60%
30% 70%
20% 80%
Why
Python
10% 90%
0%
Why Python
50%
60%
10% 90%
0%
Why Python
50%
60%
10% 90%
0%
Why Python
50%
60%
10% 90%
0%
Why Python
50%
60%
4
10% 90%
0%
Why Python
50%
5
60%
4
3 ➢ Huge Community
30% 70%
2 Python has a huge community, which means that in case you
get stuck, there will be many fellow developers you can reach
1 out for help
20% 80%
10% 90%
0%
50%
40% 60%
30% 70%
Python
Features 20% 80%
10% 90%
0%
Python Features
Cross-platform Language
01 50%
40% 60%
02 Object-Oriented Language
05
20%
GUI Programming Support
80%
06 Integrated
10% 90%
07 Embeddable
04
05
20% 80%
06
10% 90%
07
08 0%
Python Features
01 50%
40% 60%
02 Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects come into
03 30% 70%
existence. It supports inheritance, polymorphism, and encapsulation, etc. The object-
04
oriented procedure helps to programmer to write reusable code and develop
05
20%
applications in less code.
80%
06
10% 90%
07
08 0%
Python Features
01 50%
40% 60%
02
04
It implies that other languages such as C/C++ can be used to compile the code and thus it
20%
can be used further in our Python code. It converts the program 80%
into byte code, and any
05
platform can use that byte code.
06
10% 90%
07
08 0%
Python Features
01 50%
40% 60%
02
03 30% 70%
05
20%
It provides a vast range of libraries for the various fields such as80%
machine learning, web
developer, and also for the scripting. There are various machine learning libraries, such as
06
Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are the
10% 90%
07 popular framework for Python web development.
08 0%
Python Features
01 50%
40% 60%
02
03 30% 70%
04
05
20% Support
GUI Programming
80%
Graphical User Interface is used for the developing Desktop application. PyQT5, Tkinter,
06
Kivy are the libraries 10% 90%
which are used for developing the web application.
07
08 0%
Python Features
01 50%
40% 60%
02
03 30% 70%
04
05
20% 80%
06 Integrated
10% 90%
It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code line
07
by line like C,C++ Java. It makes easy to debug the code.
08 0%
Python Features
01 50%
40% 60%
02
03 30% 70%
04
05
20%
The code of the 80% source code. We can use
other programming language can use in the Python
Python source code in another programming language as well. It can embed other language into
06
our code. 10% 90%
07 Embeddable
08 0%
Python Features
01 50%
40% 60%
02
03 30% 70%
04
05
20% 80%
06 In Python, we don't need to specify the data-type of the variable. When we assign some value to
10% 90%
the variable, it automatically allocates the memory to the variable at run time. Suppose we are
07
assigned integer value 15 to x, then we don't need to write int x = 15. Just write x = 15.
08 Dynamic Memory Allocation 0%
50%
40% 60%
Python
30%
Applications
70%
20% 80%
10% 90%
0%
Python Applications
Web Application
50% Data
40% 60%Analysis
Create web applications of different levels of
complexity using Python.
30% 70%
Machine
Learning
20% 80%
10% 90%
Automation
Game
0%
Python Applications
Data Analysis
50% Web
40% 60%
Python is leading language choice for Data Application
Analysis due to its’s popular libraries including
Numpy and Panda 30% 70%
Machine
20% Learning 80%
10% 90%
Automation
Game
0%
Python Applications
Machine Learning
50% Web
40% 60%
Python can be used to implement predictive Application
analysis making use of it’s machine learning
30%
library called Scikit learn 70%
Data
20% Analysis 80%
10% 90%
Automation
Game
0%
Python Applications
Game Development
50% Web
40% 60%
Python is used for Game Development as well, Application
using a module named Pygame.
30% 70%
Data
20% Analysis 80%
10% 90%
Automation
ML
0%
Python Applications
Automation
50% Web
40% 60%
Python can be used to automate repetitive Application
stuff such as sending same emails and more.
30% 70%
Data
20% Analysis 80%
10% 90%
Machine
Learning Game
0%
Tokens
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
Tokens
KEYWORDS ✓ Python keywords are special reserved words
✓ Convey a special50%
40%
meaning to the compiler or interpreter
60%
✓ Each keyword have a special meaning and a specific operation
Identifiers ✓ Never use it as a variable
30% 70%
✓ There are 36 keywords in Python 3.10 version
20% 80%
Literals Program to print all the keywords
import keyword
10% 90%
print(keyword.kwlist) #Prints all keywords in list type
0%
Tokens
Keywords “An identifier is a name given to an entity”.
50%
40%
In very simple words, an identifier is a user-defined name to represent the
60%
basic building blocks of Python. It can be a variable, a function, a class,
IDENTIFIERS a module, or any other object.
30% 70%
Rules for defining variables
✓A variable can have alphabets, digits, and underscore.
20% 80%
Literals ✓A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
10% whitespace is allowed within the variable
✓No 90% name.
Operators ✓A variable name must not be any reserved word or keyword, e.g. int, float,
etc. 0%
Tokens
Keywords Program to check the identifier
50%
40%
print( “name”.isidentifier() )
60%
print( “#today”.isidentifier() )
IDENTIFIERS print( “_12hello”.isidentifier() )
30% 70%
print( “8cellos”.isidentifier() )
20% 80%
Literals Output:
True
10%
False 90%
Operators True
False 0%
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
Variables
Variables
50%
❖ Variable is a name that is used to40%
refer to memory location. Python variable is also known as an
60%
identifier and used to hold value.
❖ In Python, we don't need to
30% 70% is a infer language and smart
specify the type of variable because Python
enough to get variable type.
❖ Variable names can be a group of both the letters and digits, but they have to begin with a letter or an
20% 80%
underscore.
❖ It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two
different variables. 10% 90%
0%
50%
40% 60%
30% 70%
20% 80%
Assigning
Values
10% 90%
0%
Assigning Values
50%
Single Value Assignment: 40% 60%
counter = 100 # An integer assignment
miles 30% = 50.25 # A floating point 70%
name = "John" # A string
20% 80%
Multi-Value Assignment:
a = b = c = 1
10%
counter, miles, name = 100, 50.25, ‘John’
90%
0%
Assigning Values
Single Value Assignment:
50%
counter 40%
= 100 # An integer assignment
60%
miles = 50.25 # A floating point
name 30% = "John" # A string 70%
type(counter)
type(miles)
20% 80%
type(name)
Output:
class <int> 10% 90%
class <float>
class <str> 0%
50%
40% 60%
Naming
30%
Rules
70%
20% 80%
10% 90%
0%
Naming Rules
✓ The first character of the variable must be an alphabet
50% or underscore ( _ ).
40%
✓ All the characters except the first character 60% of lower-case(a-z), upper-case (A-Z),
may be an alphabet
Naming
underscore, or digit (0-9).
30%
Rules
✓ Identifier name must not 70%
contain any white-space, or special character (!, @, #, %, ^, &, *).
✓ Identifier name must not be similar to any keyword defined in the language.
✓ Identifier names are case
20%sensitive; for example, my name, and MyName is not the same.
80%
✓ Examples of valid identifiers: a123, _n, n_9, etc.
✓ Examples of invalid identifiers: 1a, n%4, n 9, etc.
10% 90%
0%
Naming Rules
1) Camel Case 50%
40%
• Each word, except the first, starts with a capital letter: 60%
Naming = “Python"
• myVariableName
30%
Rules
2) Pascal Case 70%
0%
Types of
Variables
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
Types of Variables
Types
Of
Variables
50%
40% 60%
30% 70%
1 2
20% Local Global 80%
Variables Variables
10% 90%
0%
Local Variables
Types
Of
Variables
Local variables are the variables that declared inside the function and have scope within the function. Let's
50%
understand the following example. 40% 60%
# Declaring a function
def add(): 30% 70%
# Defining local variables. They has scope only within a function
a = 20
b = 30 20% 80%
c=a+b
print("The sum is:", c)
0%
Global Variables
Types
Of
• Variables
Global variables can be used throughout the program, and its scope is in the entire program.
50%
40%
• Global variables can be used inside or outside the function. 60%
• A variable declared outside the function is the global variable by default.
keyword to use global variable inside the70%
• Python provides the global30% function.
• If ‘global’ keyword is not used, the function treats it as a local variable.
20% 80%
10% 90%
0%
Global Variables
Types
# Declare a variable and initialize it Of
Variables
x = 101 50%
# Global variable in function 40% 60%
def mainFunction():
# printing a global variable 30% 70%
print(x)
global y 20% 80%
# modifying a global variable
y = 'Welcome To Python'
10% 90%
print(x)
mainFunction()
0%
print(x)
50% Deleting
40% Variable
60%
30% 70%
20% 80%
10% 90%
0%
Deleting Variable
x=6
print(x) 20% 80%
# deleting a variable.
del x
10% 90%
print(x)
0%
50%
40% 60%
Type
30% 70% Conversion
20% 80%
10% 90%
0%
Type Conversion
50%
❖ To convert between types, you simply use the type name as a function.
40% 60%
counter = 100 # An integer assignment
Type
30% 70% Conversion
INT to STRING
str_1 = str(counter)
20% 80%
name = "J" # A string
10% 90%
STRING to INT
Int_1 = int(name)
0%
01
Tokens
String Literal
Eg:
Str = ‘Python is a high-level programming language’
Keywords
02 50%
Numeric Literal
40%Eg: 60%
A = 10
IDENTIFIERS B = 1.25
30% C = 3+j 70%
03 Boolean Literal
20% 80%
LITERALS Eg:
Bool = True
Bool = False
10% 90%
Operators 04 Special Literal
Eg: 0%
a = None
OPERATORS
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
OPERATORS
1 2 3 4 5 6 7
ARITHMETIC ASSIGNMENT RELATIONAL LOGICAL BITWISE MEMBERSHIP IDENTITY
OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS OPERATORS
Operators Description Expression
1 +
-
Addition
Subtraction
a+b
a–b
* Multiplication a*b
ARITHMETIC / Division a/b
OPERATORS
% Remainder / Modulo a%b
// Floor Division a // b
** Exponent a ** 2
Operators Expression
= a = 10;
2 += a += 10; a = a + 10;
-= a -= 10; a = a - 10;
ASSIGNMENT
*= a *= 10; a = a * 10;
OPERATORS
/= a /= 10; a = a / 10;
%= a %= 10; a = a % 10;
//= a //= 10; a = a // 10
**= a **= 10; a = a ** 2
Operators Description Expression
== Equal to a == b
3 != Not Equal to a != b
> Greater than a>b
RELATIONAL
OPERATORS
< Lesser than a<b
>= Greater than or Equal to a >= b
<= Lesser than or Equal to a <= b
4
Operators Description Expression
LOGICAL
and Logical AND (a > b) and (a>c)
OPERATORS
or Logical OR (a > b) or (a>c)
Operators Description Expression
5
~ Unary bitwise complement ~a
<< Left shift a <<1
>> Right shift a >>2
BITWISE
OPERATORS & Bitwise AND a&b
| Bitwise OR a|b
^ Bitwise Exclusive OR / XOR a^b
Input Input Output
0 0 0
0 1 0
1 0 0
5 1 1 1
BITWISE
OPERATORS
Not
Input Output
0 1
1 0
Bitwise AND Input Input Output
5&3 0 0 0
0 1 0
5 = 0101
1 0 0
5 3 = 0011 1 1 1
1 = 0001
BITWISE
OPERATORS Bitwise OR
3 = 0011 1 0 1
1 1 1
7 = 0111
Bitwise XOR
Input Input Output
5^3
0 0 0
5 5 = 0101 0 1 1
1 0 1
3 = 0011
1 1 0
BITWISE
OPERATORS 6 = 0110
Bitwise NOT
~5
Input Output
5 = 0101 0 1
1 0
~5 = 1010
1’s and 2’s Complement
5 5
2’s= 1011
Right Shift
5 > >2
5 5 0 0 0 0 0 1 0 1
BITWISE
5 >> 2 0 0
OPERATORS
Left Shift
5 0 0 0 0 0 1 0 1
5 << 2 0 0
Operator Description Example
x = 5
6
if (type(x) is int):
print("true")
else:
print("false")
MEMBERSHIP
OPERATORS Example 2:
x = 5.2
if (type(x) is not int):
print("true")
else:
print("false")
x = 24
y = 20
list = [10, 20, 30, 40, 50 ];
6
if ( x not in list ):
print("x is NOT present in given list")
IDENTITY
else:
OPERATORS print("x is present in given list")
if ( y in list ):
print("y is present in given list")
else:
print("y is NOT present in given list")
Operator Precedence
The precedence of the operators is essential 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.
30% 70%
20% 80%
10% 90%
0%
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
Data Types
Data Types
50%
❖ Python provides various standard data
40%types that define the storage method on each of them. The
60%
data types defined in Python are given below.
30% 70%
• LIST
BOOLEAN 20%
DICTIONARY SET • TUPLE 80%
• INT
• FLOAT
• STRING
• COMPLEX
10% 90%
BOOLEAN DICTIONARY SET SEQUENCE NUMERIC
0%
50%
40% 60%
30% 70%
NUMERIC
20% 80%
10% 90%
0%
NUMERIC
50%
❖ Python provides various standard data
40%types that define the storage method on each of them. The
60%
data types defined in Python are given below.
Example: 30% 70%
a=5 Output:
print("The type of a", type(a)) The type of a <class 'int’>
The type of b <class 'float’> 80%
b = 40.5
20%
print("The type of b", type(b)) The type of c <class 'complex’>
0%
50%
40% 60%
SEQUENCE:
String 30% 70%
20% 80%
10% 90%
0%
SEQUENCE: String
50%
❖ The string can be defined as the 40%sequence of characters represented in the quotation marks. In
60%
Python, we can use single, double, or triple quotes to define a string.
STRING
Example: 30% 70%
Output:
30% 70%
20% 80%
10% 90%
0%
SEQUENCE: LIST
• Python Lists are similar to arrays in C. However, the list can contain data of different types.
LIST
• The items stored in the list are separated with 50%
a comma (,) and enclosed within square brackets [].
• We can use slice [:] operators to40%
access the data of the list. 60%
• The concatenation operator (+) and repetition operator (*) works with the list in the same way as they
30% 70%
were working with the strings.
Output:
<class ‘list’>
Example: 20% 80% 2]
[1, 'hi', 'Python',
list1 = [1, "hi", "Python", 2] [2] [1, 'hi’]
print(type(list1)) #Checking type of given list
print (list1) #Printing the list1 [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
10% 90%
print (list1[3:]) # List slicing [1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
print (list1[0:2])
print (list1 + list1) # List Concatenation using + operator
print (list1 * 2) # List repetation0%
using * operator
SEQUENCE:
TUPLE
50%
40% 60%
30% 70%
20% 80%
10% 90%
0%
SEQUENCE: TUPLE
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. 50%
40% 60% in parentheses ().
• The items of the tuple are separated with a comma (,) and enclosed
• A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.
30% 70%
Output:
Example: <class 'tuple’>
20% ('hi', 'Python', 2)
80%
('Python', 2)
tup = ("hi", "Python", 2) # Checking type of tup
('hi’,)
print (type(tup)) #Printing the tuple
('hi', 'Python', 2, 'hi', 'Python', 2)
print (tup) # Tuple slicing
10% ('hi',90%
'Python', 2, 'hi', 'Python’)
print (tup[1:])
print (tup[0:1])
print (tup + tup) # Tuple concatenation using + operator
print (tup * 2) 0%using * operator
# Tuple repetition
50% SEQUENCE:
40% SET
60%
30% 70%
20% 80%
10% 90%
0%
SEQUENCE: SET
• Python Set is the unordered collection of the data type.
50%and has unique elements.
• It is iteratable, mutable(can modify after creation), SET
40%
60%
• In set, the order of the elements is undefined; it may return the changed sequence of the element.
• The set is created by using a built-in function set(), or a sequence of elements is passed in the curly
30% 70%
braces and separated by the comma. It can contain various types of values.
Example: Output:
20% 80%
set1 = set() # Creating Empty set {3, 'Python', 'James', 2}
set2 = {'James', 2, 3,'Python'} {'Python', 'James', 3, 2, 10}
print(set2) #Printing Set value
10% 90% 'James', 3, 10}
{'Python',
set2.add(10) # Adding element to the set
print(set2)
set2.remove(2) #Removing element from the set
print(set2) 0%
50%
40% 60%
DICTIONARY
30% 70%
20% 80%
10% 90%
0%
DICTIONARY
• Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table
50%hold any primitive data type, whereas value is an
where each key stores a specific value. Key can
40% 60%
arbitrary Python object.
• The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.
30% 70%
Output:
Example: {1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike’}
20% 1st name 80%
is Jimmy
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'} 2nd name is mike
dict_keys([1, 2, 3, 4])
print (d) # Printing dictionary
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
print("1st name is "+d[1]) # Accessing value using keys
10% 90%
print("2nd name is "+ d[4])
30% 70%
10% 90%
0%
BOOLEAN
• Boolean type provides two built-in values, True and False. These values are used to determine the
50%
given statement true or false. It denotes by the class bool. True can be represented by any non-zero
40%
value or 'T' whereas false can be represented by the 0 or 'F’. 60%
30% 70%
Example:
Output:
<class ‘bool’>
b = True 20% False 80%
b = False
print(type(b))
10% 90%
print(b)
0%
50%
40% 60%
30% 70%
20% 80%
90%
10% Control
Statements
0%
Decision Making Statements
…
S. No 50%
Statement & Description
1 if statements 40% 60%
An if statement consists of a Boolean expression followed by one or more
Control
statements.
30% 70% Condition
Statements
2 if...else statements
You can use one if or else if statement inside another if or else else block
if statement(s). 10% 90%
0% …
Exercise Problems
…
numbers?
False if block
4. 20%
Write a program to find out leap year? 80%
else block
10% 90%
0% …
Looping Statements LOOPING
50%
✓ A loop statement allows us to execute a statement or group of statements multiple times.
✓ Types: 40% 60%
Control
70%
S.No Loop Type & Description
30% Statements
1 for loop
Executes a sequence of statements multiple times and abbreviates the code that
manages the20%
loop variable. 80%
2 while loop
Terminates the loop statement and transfers execution to the statement immediately following the
loop.
20% 80%
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.
10% 90%
3 pass statement
The pass statement in Python is used when a statement is required syntactically but you do not
want any command or code to execute. 0%
for Loop while expression:
statement/s
0%
for Loop – using range
✓ The range() function is used to generate the sequence of the
50%
numbers. 40%
✓ If we pass the range(10), it will generate the numbers from 060% Example:-
to 9. n = int(input("Enter the number "))
Syntax:
30% for70%
i in range(n):
print(i)
range(start,stop,step size)
for i in range(1, n):
✓ The start represents the20% beginning of the iteration. 80%
print(i)
✓ The stop represents that the loop will iterate till stop-1.
✓ The range(1,5) will generate numbers 1 to 4 iterations. It is for i in range(2,n,2):
print(i)
optional. 10% 90%
✓ The step size is used to skip the specific numbers from the
iteration. It is optional to use. By default, the step size is 1. It
is optional. 0%
while Loop while expression:
statement/s
50%
✓ A while loop statement in Python programming language
40%
repeatedly executes a target statement as long as a given 60%
condition is true.
Control
Condition
✓ Syntax: 30% 70% Statements
while expression:
statement/s True False
20%
Using else statement with while loop 80%
conditional
✓ Python supports to have an else statement associated with a code
loop statement.
10% 90%
✓ If the else statement is used with a while loop,
the else statement is executed when the condition becomes
false. 0%
Loop – Exercise Problems
1. Write a program to print first N natural numbers?50%
40%
2. Write a program to reverse a given number? 60%
3. Write a program to find a given number is Control
Condition
palindrome or not? 30% 70% Statements