CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
Presentation1 (1).pptx
1. Python Programming
II Year- I Sem
R19 Regulation
(2020-2021)
By
G.V .Vidya Lakshmi ,
Assistant Professor,
Dept of IT
1
2. Objectives
The Objectives of Python Programming are
• To learn about Python programming language
syntax, semantics, and the runtime environment
• To be familiarized with universal computer
programming concepts like data types, containers
• To be familiarized with general computer
programming concepts like conditional execution
Loops & functions .
• To be familiarized with general coding techniques
and object-oriented programming
2
3. Course Outcomes
• Develop essential programming skills in
computer programming concepts like data
types, containers
• Apply the basics of programming in the
Python language
• Solve coding tasks related conditional
execution, loops
• Solve coding tasks related to the
fundamental notions and techniques used
in object-oriented programming
3
4. Syllabus
Unit 1: Introduction to Python
Unit 2: Control Statement, Strings and Text
Files
Unit 3: List and Dictionaries, Design with
Function
Unit 4: File Operations, Object Oriented
Programming
Unit 5: Errors and Exceptions, Graphical
User Interfaces
4
27. 27
• Introduction to Python
• Program Development Cycle
• Input, Processing, and Output
• Displaying Output with the Print Function
• Comments, Variables, Reading Input from
the Keyboard
UNIT 1
Contents
28. UNIT 1
• Data Types
• Type conversions
• Expressions
• Operators.
• Using functions and Modules.
• Decision Structures and Boolean Logic
28
Contents
30. 30
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
31. 31
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
32. 32
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
33. 33
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
34. 34
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
35. 35
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
36. 36
Program Development Cycle
UNIT 1
1. Problem Definition
2. Problem Analysis
3. Algorithm Development
4. Coding & Documentation
5. Testing & Debugging
6. Maintenance
38. 38
Input, Processing, and Output
UNIT 1
Most useful programs accept inputs from some
source, process these inputs, and then finally output
results to some destination
39. 39
Displaying Output with the Print Function
UNIT 1
The programmer can also force the output of a
value by using the print function.
40. 40
Displaying Output with the Print Function
UNIT 1
Syntax : print(<expression>)
>>> print('Hi there')
Hi there
41. 41
Displaying Output with the Print Function
UNIT 1
You can also write a print function that includes two or
more expression separated by commas.
Syntax : print(<expression>, … ,<expression>)
42. 42
Displaying Output with the Print Function
UNIT 1
Whether it outputs one or multiple expressions, the
print function always ends its output with a newline.
print(<expression>, end="")
44. 44
Reading Input from the Keyboard
UNIT 1
As you create programs in Python, you’ll often want your
programs to ask the user for input. You can do this by using
the input function.
<variable identifier> = input(<a string prompt>)
45. 45
Reading Input from the Keyboard
UNIT 1
>>> name = input(“Enter your name: “)
Enter your name: Ken Lambert
>>> name
'Ken Lambert'
>>> print(name) Ken Lambert
>>>
46. 46
Reading Input from the Keyboard
UNIT 1
The input function always builds a string .
After inputting strings that represent numbers, the
programmer must convert them from strings to the
appropriate numeric types.
47. UNIT 1
Type conversions
47
To convert between types, you simply use
the type name as a function.
There are several built-in functions to
perform conversion from one data type to
another.
48. UNIT 1
Type conversions
48
Conversion Function Example Use Value Returned
int(<a number or a string>) int(3.77) 3
int("33") 33
float(<a number a string>) float(22) 22.0
str(<any value>) str(99) '99'
49. 49
Comments
UNIT 1
A hash sign (#)that is not inside a string literal is the
beginning of a comment.
All characters after the #, up to the end of the physical line,
are part of the comment and the Python interpreter ignores
them.
50. 50
Comments
UNIT 1
#single line comments
#these are
# multiline
#comments
“““these are
multiline
Comments”””
These is are not executable statements.
Improves readability
51. 51
Variables
UNIT 1
Variables are nothing but reserved
memory locations to store values.
It means that when you create a variable,
you reserve some space in the memory.
52. 52
Variables
UNIT 1
Syntax : variable-name =value
•The declaration happens automatically when you
assign a value to a variable.
•The equal sign (=) is used to assign values to
variables.
53. 53
Variables
UNIT 1
Assigning Values to Variables
counter = 100 # An integerassignment
miles = 1000.0 # A floating point
name = "John" # Astring
54. 54
Variables
UNIT 1
Assigning Values to Variables-Multiple Assignment
Python allows you to assign a single value to several
variables simultaneously.
For example 1-
a = b = c = 1
a, b, c = 1, 2, "john"
For example 2-
55. UNIT 1
Data Types
55
• Let us read the sentence “ In 2007, Hanesha paid
$120,000 for her house at Maple street” .
•The above sentence contains number , name , time
and address.
•When we use data in a computer program , we
need to keep in mind the type of the data we are
using.
56. UNIT 1
Data Types
56
•A data type consists of set of values and set of
operations that can be performed on those values.
•A literal is the way a value of data type looks to a
programmer.
58. UNIT 1
Data Types
58
Numbers
Number data types store numeric values.
Python supports 4 different numerical
types-
1.integer
2.long
3.float
4.complex
59. UNIT 1
Data Types
59
Numbers - 1.integer
Integer include ‘0’ , positive and
negative numbers.
Example: var1 = 1
var2 =-7
60. UNIT 1
Data Types
60
Numbers - 2.long
Python allows you to use long int.
Long int can be indicated by ‘l’ or ‘L’ .
Example: var = 122L
61. UNIT 1
Data Types
61
Numbers - 3.float
Floating numbers are the real numbers.
These numbers contain fractional part.
Long int can be indicated by ‘l’ or ‘L’ .
Example: var = 15.20
62. UNIT 1
Data Types
62
Numbers - 4.complex
Complex numbers contains two parts.
1.real
2.imaginary
Example:
var = 3.14j
63. UNIT 1
Data Types
63
Strings(Character sets)
In Python ,characters and strings both
are treated as Strings.
A string is a sequence of characters.
Example:
C=‘hello’
S=“ hello world”
65. UNIT 1
Data Types
65
Strings(Character sets)
String concatenation can be performed
using ‘+’
Example:
The operator ‘*’ allows us to build a string by
repeating another string a given number of times.
Example:
C=‘hello’ +’world’
“ h“*5+”Python”
hhhhhPython
66. UNIT 1
Operators
66
Operators are used in programs to
manipulate variables and values.
The values on which operators perform
operations are called operands.
70. UNIT 1
Arithmetic Operators
70
Operator Example
+ Addition a + b = 30
- Subtraction a – b = -10
* Multiplication a * b = 200
/ Division b / a = 2
% Modulus b % a = 0
** Exponent a**b =10 to the power 20
// 9//2 = 4 and 9.0//2.0 = 4.0,
-11//3 = -4, -11.0//3 = -4.0
Assume variable a holds 10 and variable b holds 20
72. UNIT 1
72
Relational Operators
Operator Example
== (a == b) is not true.
!= (a != b) is true.
<> (a <> b) is true. This is similar to != operator.
> (a > b) is not true.
< (a < b) is true.
>= (a >= b) is not true.
<= (a <= b) is true.
Assume variable a holds 10 and variable b holds 20
74. UNIT 1
74
Assignment Operators
Operator Example
= c = a + b assigns value of a + b into c
+= Add AND c += a is equivalent to c = c + a
-= Subtract AND c -= a is equivalent to c = c - a
*= Multiply AND c *= a is equivalent to c = c * a
/= Divide AND c /= a is equivalent to c = c / a
%= Modulus AND c %= a is equivalent to c = c % a
**= Exponent AND
c **= a is equivalent to c = c ** a
//= Floor Division c //= a is equivalent to c = c // a
Assume variable a holds 10 and variable b holds 20
76. UNIT 1
76
Logical Operators
Operator Example
and Logical AND (a and b) is true.
or Logical OR (a or b) is true.
not Logical NOT Not(a and b) is false.
Assume variable a holds 10 and variable b holds 20
78. UNIT 1
78
Bitwise Operators
Operator Example
& Binary AND (a & b) (means 0000 1100)
| Binary OR (a | b) = 61 (means 0011 1101)
^ Binary XOR (a ^ b) = 49 (means 0011 0001)
~ Binary Ones
Complement
(~a ) = -61 (means 1100 0011 in 2's complement
form due to a signed binary number.
<< Binary Left
Shift a << 2 = 240 (means 1111 0000)
>> Binary
Right Shift a >> 2 = 15 (means 0000 1111)
Assume if a = 60; and b = 13; Now in the binary format their values will
be 0011 1100 and 0000 1101 respectively.
81. UNIT 1
81
Identity Operators
Operator Example
is x is y, here is results in 1 if id(x) equals id(y).
is not
x is not y, here is not results in 1 if id(x) is not equal to
id(y).
82. UNIT 1
Using functions
82
The Python interpreter has a number of functions and types
built into it that are always available.
1. abs() returns the absolute value of a number.
A negative value’s absolute is that value is positive.
>>> abs(-7)
7
2. any() it takes one argument and returns True
if, even one value in the iterable has a Boolean value of
True.
>>> any((1,0,0))
True
3.type() function to check the type of object we’re dealing with.
>>> type((1))
<class ‘int’>
83. UNIT 1
Using Modules.
83
To import module the syntax:
import module-name
To import particular resource in module the syntax is:
from module-name import resource1,resource2….
84. UNIT 1
Using Modules-Example
84
To import module Example:
import math
To import particular resource in module the example
is:
from math import pi,sqrt
85. UNIT 1
Decision Structures
85
Decision structures evaluate multiple
expressions which produce True or False as
outcome.
Statement Description
if statements
if statement consists of a boolean
expression followed by one or more
statements.
if...else
statements
if statement can be followed by an optional
else statement, which executes when the
boolean expression is FALSE.
nested if
statements
You can use one if or else if statement inside
another if or else if statement(s).
91. UNIT 1
Repetition Structures Loops
91
A loop statement allows us to execute a
statement or group of statements multiple
times.
Loop
Type
Description
while
loop
Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the
loop body.
for loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
nested
loops
You can use one or more loop inside any another while, for
loop.