Unit 3 Python
Unit 3 Python
Output:
enter your purchase
amount
2500
amount to pay
2000
2. Program to provide bonus mark if the category is sports
Output:
enter ur mark out of 100
85
enter ur categery G/S
S
mark is 90
alternative (if-else) :
• In this else statement can be combined with if
statement.
• If the condition is true if statements gets executed,
otherwise else part gets executed.
syntax:
Flowchart:(if-else) :
n=eval(input("enter a number"))
if(n>=0):
print("positive number")
else:
print("negative number")
Output
enter a number : 8
positive number
Examples 3: Find the Given Year is Leap Year or Not
y=eval(input("enter a year"))
if(y%4==0):
print("leap year")
else:
print("not leap year")
Output:
enter a year:2000
leap year
HW:
1. Find the Greatest of Two Numbers
2. Find the Given Age is Eligible for Voting or not
Chained conditionals(if-elif-else)
• The elif is short for else if.
• This is used to check more than one condition.
• If the condition1 is False, it checks the condition2 of the elif
block. If all the conditions are False, then the else part is
executed.
• Among the several if...elif...else part, only one part is executed
according to the condition
• The if block can have only one else block. But it can have
multiple elif blocks.
• The way to express a computation like that is a chained
conditional.
Chained conditionals(if-elif-else):
syntax:
Chained conditionals(if-elif-else)
Flowchart:
Example:
1. student mark system
2. traffic light system
3. compare two numbers
4. roots of quadratic equation
Example 1: Find the Student Mark
Statement
mark=eval(input("enter ur mark:"))
if(mark>=90):
Output
print("grade:S") enter ur mark:78
elif(mark>=80): grade:B
print("grade:A")
elif(mark>=70):
print("grade:B")
elif(mark>=50):
print("grade:C")
else:
print("fail")
Example 2: Traffic light system
Output:
enter colour of light:green
GO
HW:
1. Compare two numbers
2. Roots of quadratic equation
ITERATION/CONTROL STATEMENTS:
• while
• for
• break
• continue
• pass
While loop:
In while loop, test expression is checked first. The
body of the loop is entered only if the
test_expression is True. After one iteration, the test
expression is checked again. This process continues
until the test_expression evaluates to False.
The statements inside the while starts with
indentation
Syntax:
While loop:
Flowchart:
Examples 1 : Find sum of n numbers:
Output:
enter n
10
55
Examples 2 : find factorial of a number
n=eval(input("enter n"))
i=1
fact=1
while(i<=n):
fact=fact*i
i=i+1
print(fact)
Output:
enter n
5
120
Examples 3 :find sum of digits of a number:
n=eval(input("enter a number"))
sum=0
while(n>0):
a=n%10
sum=sum+a
n=n//10
print(sum)
Enter a number
123
6
HW:
1. Program to Reverse the given number:
2. Program to find number is Armstrong number or not
3. Program to check the number is palindrome or not.
For loop:
for in range:
We can generate a sequence of numbers using range() function.
range(10) will generate numbers from 0 to 9 (10 numbers).
In range function have to define the start, stop and step size as
range(start,stop,step size). step size defaults to 1 if not provided.
syntax
Flowchart:
For in sequence
The for loop in Python is used to iterate over a
sequence (list, tuple, string).
Iterating over a sequence is called traversal. Loop
continues until we reach the
last element in the sequence.
The body of for loop is separated from the rest of the
code using indentation.
n=eval(input("enter a"))
for i in range(1,n,1):
if(i%5==0 and i%10!=0):
print(i)
enter a:30
5
15
25
Fibonacci series
output
a=0
b=1
n=eval(input("Enter the number of terms: "))
print("Fibonacci Series: ")
print(a,b)
for i in range(1,n,1):
c=a+b
print(c)
a=b
b=c
n=eval(input("enter a number:"))
for i in range(1,n+1,1):
if(n%i==0):
print(i)
enter a number:10
1
2
5
10
check the no is prime or not
Syntax:
Flowchart
example
for i in "welcome":
if(i=="c"):
break
print(i)
w
e
l
CONTINUE
Syntax:
Flowchart
Example:
for i in "welcome":
if(i=="c"):
continue
print(i)
w
e
l
o
m
e
PASS
Syntax:
pass
break
Example
w
for i in “welcome”: e
if (i == “c”): l
pass c
print(i) o
m
e
else statement in loops:
else in for loop:
If else statement is used in for loop, the else statement
is executed when the loop
has reached the limit.
The statements inside for loop and statements inside
else will also execute.
example
for i in range(1,6):
print(i)
else:
print("the number greater than 6")
1
2
3
4
5 the number greater than 6
else in while loop:
If else statement is used within while loop , the else
part will be executed when
the condition become false.
The statements inside for loop and statements inside
else will also execute.
Program
i=1
while(i<=5):
print(i)
i=i+1
else:
print("the number greater than 5")
1
2
3
4
5
the number greater than 5
Strings:
Strings
String slices
Immutability
String functions and methods
Strings:
String is defined as sequence of characters represented in
quotation marks
1. single quotes (' ')
2. double quotes (" ")
3. triple quotes(“”” “”””)
An individual character in a string is accessed using a index.
The index should always be an integer (positive or negative).
A index starts from 0 to n-1.
Strings are immutable i.e. the contents of the string cannot be
changed after it is created.
Python does not support character data type. A string of size
1 can be treated as characters.
Operations on string:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Member ship
Indexing:
>>>a=”HELLO”
>>>print(a[0])
>>>H
>>>print(a[-1])
>>>O
Positive indexing helps in accessing the string from the
beginning
Negative subscript helps in accessing the string from
the end.
Slicing:
Print[0:4] – HELL
Print[ :3] – HEL
Print[0: ]- HELLO
Stringname.method()
a=”happy birthday”
here, a is the string name.