Unit-l Python
Unit-l Python
Introduction: Introduction to computer system, algorithms and flowcharts, Ethics and IT policy in
company, A Brief History of Python, Applications areas of python, The Programming Cycle for Python,
Python IDE.
1
Syllabus
UNIT-II: Decision Control Statements
2
Syllabus
UNIT-III: Function and Modules
3
Syllabus
UNIT-IV: Basic Data structures in Python
4
Syllabus
UNIT-V: File and Exception handling
5
Course Objective
11
Unit I Content
1. Introduction to computer 7. Elements of Python
system • Keywords and Identifiers
2. Algorithms and flowcharts • Variables
3. Ethics and IT Policy in Company • Data types and type conversion
• Operators in Python
4. A Brief History of Python
• Operator precedence and
5. Applications areas of python associativity
6. The Programming Cycle for • Expressions in Python
Python, Python IDE • Strings
• Indexing and Slicing of Strings
• Classes and Object
• Constructor
12
Unit I Objective
13
Topis Prerequisite
14
Topic Objective
15
Computer (CO1)
INPUT OUTPUT
Data PROCESS Information
Instructions
16
Computer (CO1)
17
Characteristics of Computers (CO1)
Storage
No IQ/ No
Speed
Feeling
Computers
Versatility Accuracy
Diligence
18
Application of Computers (CO1)
19
Block Diagram of Digital Computers (CO1)
Information
Data Input Unit Memory Unit Output Unit
or Result
Control Unit
CPU 20
Algorithm (CO1)
21
Characteristics of Algorithm (CO1)
1. Input
It takes zero or more values as input.
2. Output
It produces one or more values as output.
3. Finiteness
It should terminate after finite number of steps.
4. Definiteness
Each instruction must be clear, precise and unambiguous.
5. Effectiveness
Each instruction must be very basic but essential so that it can be
carried out using pen and paper.
22
Flowchart (CO1)
23
Elements of Flowchart (CO1)
Ellipse/Oval Start/End
Diamond Condition
24
Algorithm and flowchart (CO1)
1. Write the algorithm and draw the flowchart to compute the sum of two
numbers.
2. Write the algorithm and draw the flowchart to compute the sum and average
of five numbers.
3. Write the algorithm and draw the flowchart to compute the area of triangle
using heron’s formula.
4. Write the algorithm and draw the flowchart to compute the temperature in
degree Fahrenheit when temperature in degree Celsius is given.
5. Write the algorithm and draw the flowchart to swap two numbers using 3 rd
variable.
6. Write the algorithm and draw the flowchart to swap two numbers without
using 3rd variable.
25
Algorithm and flowchart(CO1)
7. Write the algorithm and draw the flowchart to check whether the
number is odd or even.
8. Write the algorithm and draw the flowchart to compute the greater of
two numbers.
9. Write the algorithm and draw the flowchart to compute the greatest
of three numbers.
10. Write the algorithm and draw the flowchart to check whether the
given year is leap year or not.
11. Write the algorithm and draw the flowchart to compute the sum of
first N natural numbers.
26
Algorithm and flowchart(CO1)
12. Write the algorithm and draw the flowchart to compute the factorial
of the given number.
13. Write the algorithm and draw the flowchart to compute the sum of
digits of the given number.
14. Write the algorithm and draw the flowchart to compute the reverse of
the given number. Also check the given number is in palindrome or
not.
15. Write the algorithm and draw the flowchart to convert the decimal
number to binary number.
16. Write the algorithm and draw the flowchart to convert the binary
number to decimal number.
27
Algorithm and flowchart(CO1)
12. Write the algorithm and draw the flowchart to compute the factorial
of the given number.
13. Write the algorithm and draw the flowchart to compute the sum of
digits of the given number.
14. Write the algorithm and draw the flowchart to compute the reverse of
the given number. Also check the given number is in palindrome or
not.
15. Write the algorithm and draw the flowchart to convert the decimal
number to binary number.
16. Write the algorithm and draw the flowchart to convert the binary
number to decimal number.
28
Code of Ethics and Professional Conduct (CO1)
The Code is designed to inspire and guide the ethical conduct of all
computing professionals, including current and aspiring
practitioners, instructors, students, influencers, and anyone who
uses computing technology in an impactful way.
29
Code of Ethics and Professional Conduct (CO1)
30
Code of Ethics and Professional Conduct… (CO1)
2. PROFESSIONAL RESPONSIBILITIES
A computing professional should…
• 2.1 Strive to achieve high quality in both the processes and
products of professional work.
• 2.2 Maintain high standards of professional competence, conduct,
and ethical practice.
• 2.3 Know and respect existing rules pertaining to professional
work.
• 2.4 Accept and provide appropriate professional review.
31
Code of Ethics and Professional Conduct… (CO1)
2. PROFESSIONAL RESPONSIBILITIES
• 2.5 Foster public awareness and understanding of computing,
related technologies, and their consequences.
• 2.6 Access computing and communication resources only when
authorized or when compelled by the public good.
• 2.7 Design and implement systems that are robustly and usably
secure.
32
Code of Ethics and Professional Conduct… (CO1)
33
Code of Ethics and Professional Conduct (CO1)
35
Code of Ethics and Professional Conduct… (CO1)
36
Code of Ethics and Professional Conduct… (CO1)
37
IT Policy (CO1)
• Companies provides and maintains technological products, services
and facilities like Personal Computers (PCs), peripheral equipment,
servers, telephones, Internet and application software to its employees
for official use.
• The Information Technology (IT) Policy of the organization defines
rules, regulations and guidelines for proper usage and maintenance of
these technological assets to ensure their ethical and acceptable use
and assure health, safety and security of data, products, facilities as
well as the people using them.
• It also provides guidelines for issues like purchase, compliance, IT
support and grievance redressal of the employees pertaining to
technological assets and services used for office work.
38
IT Policy… (CO1)
39
Python Introduction (CO1)
40
Introduction (CO1)
44
A Brief History of Python Cont…(CO1)
45
A Brief History of Python Cont…(CO1)
46
A Brief History of Python Cont… (CO1)
47
Applications areas of python (CO1)
48
Features of Python (CO1)
49
Features of Python (CO1)
• Portability.
• Object-oriented
– Everything in Python is an object.
52
Getting Python (CO1)
53
The Programming Cycle for Python (CO1)
54
The Programming Cycle for Python (CO1)
Traditional Development Cycle Python’s Development Cycle
55
Python IDE (CO1)
• Spyder
– https://www.spyder-ide.org/
• IDLE
– https://docs.python.org/3/library/idle.html
• Sublime Text 3
– https://www.sublimetext.com/3
• Jupyter
– https://jupyter.org/install.html
61
Interacting with Python Programs (CO1)
6. Click on the code cell write down the code 1+1 and then press
shift + Enter from keyboard/run command from interface. Then
check the output.
63
Elements of Python (CO1)
64
Topic Objective
The students will study the elements of Python like Keywords and
Identifiers, Variables, Data types and Operators.
65
Prerequisite and Recap
• Expression
• Operators
• Constant and variable
66
Elements of Python (CO1)
67
Keywords (CO1)
69
Rules for writing Identifiers (CO1)
70
Variables (CO1)
71
Variables (CO1)
72
Multiple Assignment in Variables (CO1)
73
Data types (CO1)
74
Standard Data types (CO1)
Data Types Keyword
Text Type str
Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set
Boolean Types bool
Binary Types bytes
75
Numbers (CO1)
• They store numeric values. Number objects are created when a value is
assigned to them. For ex:
>>> var1 = 1
>>> var2 = 10
• They can be deleted the reference to a number object by using the del
statement. The syntax of the del statement is
del var1[,var2[,var3[....,varN]]]]
• A single object or multiple objects can be deleted by using the del statement.
For example
>>> del var
>>> del var_a, var_b
76
Types of Number Types (CO1)
77
Examples of Number Types (CO1)
78
Strings (CO1)
79
Strings (CO1)
• For example
>>> str = ‘Hello World!’
>>> print(str) # Prints complete string
>>> print(str[0]) # Prints first character of the string
>>> print(str[2:5]) # Prints characters starting from 3rd to 5th
>>> print(str[2:]) # Prints string starting from 3rd character
>>> print(str * 2) # Prints string two times
>>> print(str + ‘TEST’) # Prints concatenated string
80
Slicing Strings (CO1)
Slicing
• A range of characters can be returned by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
Example
Get the characters from position 2 to position 5 (not included):
Program
b = "Hello, World!"
print(b[2:5])
Program Output
llo
81
Program to demonstrate slicing of Strings (CO1)
# String slicing
String ='ASTRING'
# Using slice constructor
s1 = slice(3)
s2 = slice(1, 5, 2)
s3 = slice(-1, -12, -2)
print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])
82
Output of slicing of Strings (CO1)
Output:
String slicing
AST
SR
GITA
83
Lists (CO1)
84
Lists (CO1)
• The plus (+) sign is the list concatenation operator, and the asterisk
(*) is the repetition operator.
85
Lists (CO1)
86
Tuple (CO1)
88
Set (CO1)
91
Implicit Type conversion (CO1)
95
Arithmetic Operators (CO1)
Operator Description Example
(a=5, b=3)
+ Adds values on either side of the operator. a+b=8
97
Assignment Operators (CO1)
Operator Description
= a=b
Assigns values from right side operands(b) to left side operand (a)
+= a+=b is same as a = a + b
It adds right operand to the left operand and assign the result to left operand
-= a-=b is same as a = a - b
It subtracts right operand from the left operand and assign the result to left operand
*= a*=b is same as a = a * b
It multiplies right operand with the left operand and assign the result to left operand
/= a/=b is same as a = a / b
It divides left operand with the right operand and assign the result to left operand
98
Assignment Operators (CO1)
Operator Description
%= a%=b is same as a = a % b
It takes modulus using two operands and assign the result to left operand
a**=b is same as a = a ** b
**= Performs exponential (power) calculation on operators and assign value to the
left operand
//= a//=b is same as a = a //b
It performs floor division on operators and assign value to the left operand
99
Logical Operators (CO1)
Operator Description
100
Logical Operators (CO1)
a b a and b a or b not a
101
Bitwise Operators (CO1)
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
103
Bitwise Operators (CO1)
104
Bitwise Operators (CO1)
105
Membership Operators (CO1)
106
Identity Operators (CO1)
is not True if the operands are not identical (do not a is not b False
refer to the same object)
107
Operator Precedence and associativity(CO1)
108
Operator Precedence and associativity(CO1)
Operator Description
() Parenthesis
** Exponentiation
~,+,- Unary operators
*,/,//,% Arithmetic multiply, division, floor and
modulo division
+, - Addition and subtraction
>>,<< Bitwise left and right shift operator
& Bitwise and operator
^ Bitwise Ex-or operator
| Bitwise or operator
109
Operator Precedence and associativity(CO1)
Operator Description
<=,>=,<,> Relational inequality operators
==, != Equal and not equal operators
=,*=,/=,//=,%=,+=,-=,**=,&= Assignment operators
is, is not Identity operators
in, not in Membership operators
not Logical not operator
and Logical and operator
or Logical or operator
110
Expressions in Python (CO1)
111
Classes (CO1)
113
Example of Object (CO1)
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
114
Output of example (CO1)
Toyota 2016
115
Python Constructor(CO1)
116
Creating Python Constructor(CO1)
117
Example Python Constructor(CO1)
class Employee:
def __init__(self, name, id):
self.id = id
self.name = name
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
ID: 101
Name: John
ID: 102
Name: David
119
Python Non-Parameterized Constructor (CO1)
The non-parameterized constructor uses when we do not want to
manipulate the value or the constructor that has only self as an
argument.
Example:
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John") 120
Output of Non-Parameterized Constructor (CO1)
121
Python Parameterized Constructor (CO1)
122
Python Parameterized Constructor (CO1)
Example:
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
Output:
This is parametrized constructor
Hello John
123
Python Default Constructor (CO1)
124
Faculty Video Links, You tube & NPTEL Video Links and Online Courses Details
125
Daily Quiz
2. _________Operator
* is used to multiply numbers.
126
Weekly Assignment 1.1
127
Weekly Assignment 1.1
129
Weekly Assignment 1.2
(CO1)
2. Discuss about feature and application area of python
(CO1)
3. What is PEP 8? How is memory managed in Python?
(CO1)
4. Explain types of operators used in python. (CO1)
130
Weekly Assignment 1.2
133
MCQs
5. Which operator can be used to compare two values?
a) <>
b) ><
c) ==
d) =
134
MCQs
135
Old Question Papers
138
Old Question Papers
139
Old Question Papers
19. Write short notes with example: The Programming cycle for
Python, Elements of Python, Type conversion in Python,
operator precedence and Boolean expression.
[AKTU 2019-2020(odd), 10
marks]
140
Expected Questions for University Exam
141
Summary
142
References
143
THANK YOU
144