Mod1_ppt dda
Mod1_ppt dda
PYTHON BASICS
FLOW CONTROL
FUNCTIONS
PYTHON BASICS
ENTERING EXPRESSIONS INTO THE
INTERACTIVE SHELL
ENTERING EXPRESSIONS INTO THE
INTERACTIVE SHELL
A >>> prompt appears in the
interactive shell
The ** operator is
evaluated first the
*, /, //, and % operators
are evaluated next(from
left to right)
and the + and - operators
are evaluated last (from
left to right).
The integer (or int) data type indicates values that are
whole numbers
True
False
a b c
0 0 0
0 1 0
1 0 0
1 1 1
OR STATEMENT
a b c
0 0 0
0 1 1
1 0 1
1 1 1
BOOLEAN OPERATORS
elif statements
while loop
for loop
break
continue
IF STATEMENTS
The most common type of flow control statement
An if statement’s clause will execute if the
statement’s condition is True
An if statement consists of the following:
if keyword
A condition (an expression) that evaluates to
True or False
A colon
Starting on the next line, an indented block of
code called the if clause
EXAMPLE OF IF STATEMENT
SIMPLE IF
ELSE
print('Enter Salary')
salary = int (input())
print ("Enter year of service")
yos = int(input())
if yos > 5:
print ('Bonus is', .05 * salary)
else:
print ("No Bonus")
TAKE VALUES OF LENGTH AND BREADTH OF A
RECTANGLE FROM USER AND CHECK IF IT IS SQUARE
OR NOT.
Output
WHAT DOES THE FOLLOWING CODE DO?
Output 1
Output 2
PROGRAM TO PRINT 1 TO 10 USING
WHILE LOOP
i=1
#The while loop will iterate until condition
becomes false.
while i<=10:
print(i)
i=i+1
PROGRAMS
Write a Python program to input an integer
number and print its last digit
Write a Python program to input a number
and count the number of digits in it using
while loop.
Write a Python program to find multiplication
table of a number.
Write a python program to input an integer
number and print reverse of that number.
Write a Python program to input a positive
integer and find its factorial.
Write a python program to input an integer
number and print reverse of that number Also
check the number is palindrome or not.
BREAK
STATEMENTS
#statement(s)
while condition :
#statement(s)
if break_condition :
break
#statement(s)
BREAK STATEMENTS
keyword is used
BREAK STATEMENT WITH WHILE LOOP
i=0
while 1:
print(i)
i=i+1;
if i == 20:
break
print('came out of while loop')
break statement with while loop
Example Programs
Program to check prime number or not
num = int(input("Enter a number: "))
if num <= 1:
print(f"{num} is not a prime number.")
else:
i=2
while i <= num ** 0.5:
if num % i == 0:
print(f"{num} is not a prime number.")
break
i=i+1
else:
print(f"{num} is a prime number.")
CONTINUE
STATEMENTS
Output:
i=0
1
while(i < 10): 2
i = i+1 3
4
if(i == 5): 6
continue 7
print(i) 8
9
10
CONTINUE STATEMENTS
Output 1
Output 2
Values like 0, 0.0, ' '(empty string)
are considered False
Output
You could have entered
name != ' ' instead of not
name, and
numOfGuests != 0 instead
of numOfGuests
THE FOR LOOPS AND
THE RANGE()
FUNCTION
FOR LOOPS
>>> range(10)
range(0, 10)
USING RANGE() IN FOR
range() is also used in for loops to iterate over a
sequence of numbers
Example
n = int(input('Enter a number'))
for i in range(1,n+1):
print(i)
Enter a number 3
1
2
3
>>>
EXAMPLE OF FOR LOOP AND RANGE()
Output
WHAT IS THE OUTPUT OF THE FOLLOWING
CODE?
Output
EXAMPLE OF FOR LOOP AND RANGE( )
total = 0
for num in range(10):
total = total + num Outpu
print(total) t:
45
THE STARTING, STOPPING, AND STEPPING
ARGUMENTS TO RANGE()
Syntax
range(start, stop, step)
Output
PROGRAM TO PRINT TABLE OF GIVEN
NUMBER.
Output:
Enter the
n = int(input('Enter the
number: 19
number: \n')) 19 * 1 = 19
for i in range(1,11): 19 * 2 = 38
c = n*i 19 * 3 = 57
19 * 4 = 76
print(n,'*',i,'=',c) 19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
PROGRAM TO PRINT EVEN NUMBER
USING STEP SIZE IN RANGE().
Output:
Enter the
number: 21
n = int(input('Enter the number: 2
\n')) 4
6
for i in range(2,n,2):
8
print(i) 10
12
14
16
18
20
NEGATIVE ARGUMENTS IN RANGE()
Output
PROGRAMS
IMPORTING
MODULES
IMPORTING MODULES
Python also comes with a set of modules called the
standard library
Examples
math module has mathematics-related functions
random module has random number-related
functions
IMPORT STATEMENT
Run 2:
Run 1: Run 2:
CONTD….
FROM IMPORT STATEMENTS
composed of
the from keyword
the import keyword
an asterisk
E.g.
from random import *
SYS
sys.argv
CONTD ….
sys.version
sys.path
sys.maxsize
ENDING A PROGRAM EARLY WITH THE
SYS.EXIT() FUNCTION
SYS.EXIT()
Used to terminate a program
The exit method belongs to sys module
Output
FUNCTIONS
print()
input()
len()
USER-DEFINED FUNCTIONS
• def keyword
• Function name
• A colon
• An indented block of code
• 2 parts of a function
• Function definition
• Function call
Output
ARGUMENTS AND PARAMETERS
ARGUMENTS AND PARAMETERS
Output
RETRUN VALUES AND RETURN
STATEMENTS
RETURN VALUES AND RETURN
STATEMENTS
Output Output
THE NONE VALUE
THE NONE VALUE
Output
Output
KEYWORD ARGUMENTS AND THE PRINT() FUNCTION
KEYWORD ARGUMENTS AND THE PRINT()
FUNCTION
Keyword arguments are often used for optional
parameters.
Variables that are assigned outside all functions are said to exist
in the global scope.
Code in the global scope, outside of all functions, cannot use any
local variables.
You can use the same name for different variables if they are in
different scopes.
LOCAL VARIABLES CANNOT BE USED IN THE
GLOBAL SCOPE
LOCAL SCOPES CANNOT USE VARIABLES IN OTHER
LOCAL SCOPES
GLOBAL VARIABLES CAN BE READ FROM A LOCAL
SCOPE
LOCAL AND GLOBAL VARIABLES WITH THE
SAME NAME
THE GLOBAL STATEMENT
THE GLOBAL STATEMENT
Common Exceptions
ZeroDivisionError: Occurs when a number is divided by zero.
EOFError: It occurs when the end of the file is reached, and yet
operations are being performed
EXCEPTION HANDLING
The code that could potentially have an error is put in a try clause.
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print("a/b = %d" %c)
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
except:
print("Can't divide with zero")
Output:
Enter a:30
Enter b:0
Can't divide with zero
THE EXCEPT STATEMENT WITH NO
EXCEPTION
Example
Enter a:5
Enter b:0
can't divide by zero
EXCEPTION HANDLING
Naming variables
CONTD ….
Global Keyword
Program to print Collatz
Sequence
More example programs on functions
Example
Output:
2
4
Example of a recursive function
Output:
The factorial of 3 is
6
Using Global and Local variables in the
same code
Output:
global global
local
Global variable and Local variable with
same name
Output:
local x: 10
global x: 5