MODULE1
MODULE1
Prepared By
Prof. Sherin Mariam Jijo
Prof. Nikesha Patel
IT Department
1.Introduction to Python
• Data types, Keywords, Identifiers
• Variables, Comments, Operators
• Simple Input and Output
• Branching, Control structures
• Iterations, break, continue
• Functions, Recursion
WHY PYTHON IS IMPORTANT?
1) Python is an easy to learn, powerful programming language.
2) It has efficient high-level data structures and a simple but
effective approach to object-oriented programming.
3) Python’s elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and
rapid application development in many areas on most
platforms.
4) The Python interpreter and the extensive standard library are
freely available in source .
Continue…
5) It’s Powerful due to
i. Dynamic typing
ii. Built-in types and tools
iii. Library utilities
iv. Third party utilities (e.g. Numeric, NumPy, sciPy)
v. Automatic memory management
6) Used heavily in Datascience, Machine Learning and Scientific
Learning (Scikit learn, pandas, numpy etc)
7) Plenty of job opportunities (Google, Facebook, You tube etc)
HISTORY OF PYTHON
• Invented in the Netherlands, early 90s by Guido van Rossum
• Named after Monty Python
• Open sourced from the beginning
• Considered a scripting language, but is much more
• Scalable, object oriented and functional from the beginning
• Used by Google from the beginning
• Increasingly popular
Python’s Benevolent Dictator For Life
• “Python is an experiment in how much freedom programmers
need. Too much freedom and nobody can read another's code;
too little and expressive-ness is endangered.”
• - Guido van Rossum
Understanding code
• Indentation matters to meaning the code
• Block structure indicated by indentation
• The first assignment to a variable creates it
• Dynamic typing: no declarations, names don’t have types, objects do
• Assignment uses = and comparison uses ==
• For numbers + - * / % are as expected.
• Use of + for string concatenation.
• Use of % for string formatting (like printf in C)
• Logical operators are words (and,or,not) not symbols
• The basic printing command is print
Whitespace
Whitespace is meaningful in Python, especially indentation and
placement of newlines
∙Use a newline to end a line of code
Use \ when must go to next line prematurely
∙No braces {} to mark blocks of code, use consistent indentation
instead
• First line with less indentation is outside of the block
• First line with more indentation starts a nested block
∙Colons start of a new block in many constructs, e.g. function
definitions, then clauses
Comments
• Start comments with #, rest of line is ignored
• Can include a “documentation string” as the first line of a new
function or class you define
• Development environments, debugger, and other tools use it:
it’s good style to include one
def fact(n):
“““fact(n) assumes n is a positive integer and
returns facorial of n.”””
assert(n>0)
return 1 if n==1 else n*fact(n-1)
Naming Rules
• Names are case sensitive and cannot start with a number.
They can contain letters, numbers, and underscores.
bob Bob _bob _2_bob_ bob_2 BoB
• Keywords
and, assert, break, class, continue, def, del, elif,
else, except, exec, finally, for, from, global, if,
import, in, is, lambda, not, or, pass, print, raise,
return, try, while
Data types
Integers
• Integers are whole numbers. They have no fractional parts.
• Integers can be positive or negative.
• There are two types of integers in Python:
• i) Integers(Signed) : It is the normal integer representation of
• whole numbers using the digits 0 to 9. Python provides
• single int data type to store any integer whether big or small.
• It is signed representation i.e. it can be positive or negative.
• ii) Boolean : These represent the truth values True and False. It
• is a subtype of integers and Boolean values True and False
• corresponds to values 1 and 0 respectively
Demonstration of Integer Data Type
#Demonstration of Integer-Addition of two integer number
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
sum=a+b
print("The sum of two integers=",sum)
Output:
• Enter the value of a: 45
• Enter the value of b: 67
• The sum of two integers= 112
Floating point
• A number having fractional part is a floating point number.
• It has a decimal point. It is written in two forms :
• i) Fractional Form : Normal decimal notation e.g. 675.456
• ii) Exponent Notation: It has mantissa and exponent.
• e.g. 6.75456E2
• Advantage of Floating point numbers:
• They can represent values between the integers.
• They can represent a much greater range of values.
• Disadvantage of Floating point numbers:
• Floating-point operations are usually slower than integer operations.
#Demonstration of Float Number- Calculate Simple
Interest
if x < y:
1. Check if x is less
print (x) than y.
else: 2. If so, print x
print (y) 3. Otherwise, print y.
print (‘is the minimum’)
if statement (no else!)
• General form of the if statement
if boolean-expr :
S1
S2
• Execution of if statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and then control
moves to the S2.
• If expression evaluates to false, then control moves to the S2 directly.
if-else statement
if boolean-expr :
S1
else:
S2
S3
• Execution of if-else statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and then control
moves to S3.
• If expression evaluates to false, then S2 is executed and then control
moves to S3.
• S1/S2 can be blocks of statements!
Nested if, if-else
if a <= b:
if a <= c:
…
else:
…
else:
if b <= c) :
…
else:
…
Elif
• A special kind of nesting is the chain of if-else-if-else-…
statements
• Can be written elegantly using if-elif-..-else
if cond1: if cond1:
s1 s1
else: elif cond2:
if cond2: s2
s2 elif cond3:
else: s3
if cond3: elif …
s3 else
else: last-block-of-stmt
…
ITERATION OR LOOPING
• What is loop or iteration?
• Loops can execute a block of code number of times until a
certain condition is met.
OR
• The iteration statement allows instructions to be executed until a
certain condition is to be fulfilled.
• The iteration statements are also called as loops or Looping
statements.
• Python provides two kinds of loops & they are,
while loop
For loop
While loop
• A while loop allows general repetition based upon the repeated
testing of a Boolean co
• The syntax for a while loop in Python is as follows:
while condition:
: Colon Must
body
• Where, loop body contain the single statement or set of
statements (compound statement) or an empty statement.
• The loop iterates while the expression evaluates to true, when
expression becomes false the loop terminates.
# Python Program to Print Natural Numbers from 1 to N
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
for LOOP - range KEYWORD
Control after break/continue 'break' resumes the control of 'continue' resumes the control of
the program to the end of loop the program to the next iteration
enclosing that 'break'. of that loop enclosing 'continue'.
Other uses 'break' can be used with 'continue' can not be executed
'switch', 'label'. with 'switch' and 'labels'.
FUNCTIONS
#Python program to add two numbers using
function