[go: up one dir, main page]

0% found this document useful (0 votes)
19 views56 pages

Unit 3 Python

Uploaded by

swarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views56 pages

Unit 3 Python

Uploaded by

swarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Unit 03:

CONTROL STATEMETS AND STRINGS

Conditional (if, alternative (if-else),


chained conditional (if-elif-else). Iteration-
while, for, infinite loop,
break, continue, pass, else.
Strings-String slices, immutability, string
methods and operations.
Decision Making (or) Conditionals (or) Branching The
execution of the program depends upon the condition.

Types of conditional Statement


1. if statement
2. if-else statement
3. if-elif-else statement
CONDITIONALS
Conditional (if):
conditional (if) is used to test a condition, if
the condition is true the statements inside if will
be executed.
Syntax:
Flowchart: conditional (if)
Example: 1. Program to provide flat Rs 500, if the purchase
amount is greater than 2000.

purchase=eval(input(“enter your purchase amount”))


if(purchase>=2000):
purchase=purchase-500
print(“amount to pay", purchase)

Output:
enter your purchase
amount
2500
amount to pay
2000
2. Program to provide bonus mark if the category is sports

m=eval(input(“enter ur mark out of 100”))


c=input(“enter ur categery G/S”)
if(c==”S”):
m=m+5
print(“mark is”,m)

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) :

Examples 1: Find the Given Number is ODD or EVEN


n=eval(input("enter a number"))
if(n%2==0):
print("even number") Output:
else: enter a number:4
even number
print("odd number")
Examples 2: Find the Given Number is POSITIVE OR
NEGATIVE number

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

colour=input("enter colour of light:")


if(colour=="green"):
print("GO")
elif(colour=="yellow"):
print("GET READY")
else:
print("STOP")

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.

Sequence can be a list, strings or tuples


Examples:
1. print nos divisible by 5 not by 10:
2. Program to print fibonacci series.
3. Program to find factors of a given number
4. check the given number is perfect number or not
5. check the no is prime or not
6. Print first n prime numbers
7. Program to print prime numbers in range
print nos divisible by 5 not by 10

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

Enter the number of terms: 6


Fibonacci Series:
01
1
2
3
5
8
find factors of a number

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

Program to print first n prime numbers

Program to print prime numbers in range


Loop Control Structures
BREAK
 Break statements can alter the flow of a loop.
 It terminates the current
 loop and executes the remaining statement outside
the loop.
 If the loop has else statement, that will also gets
terminated and come out of the
loop completely

Syntax:
Flowchart
example

for i in "welcome":
if(i=="c"):
break
print(i)

w
e
l
CONTINUE

It terminates the current iteration and transfer the


control to the next iteration in
the loop.

Syntax:
Flowchart
Example:

for i in "welcome":
if(i=="c"):
continue
print(i)

w
e
l
o
m
e
PASS

 It is used when a statement is required syntactically


but you don’t want any code
to execute.
 It is a null statement, nothing happens when it is
executed.

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

 The Slice[start : stop] operator extracts sub string


from the strings.
 The process of extracting a sub string from a string is
called slicing
Immutability:
 Python strings are “immutable” as they cannot be
changed after they are created.
 Therefore [ ] operator cannot be used on the left side of
an assignment.
print(a)
String built in functions and methods:
A method is a function that “belongs to” an object.

Syntax to access the method

Stringname.method()

a=”happy birthday”
here, a is the string name.

You might also like