0% found this document useful (0 votes)
3 views26 pages

3A_Python Programming Class 3

This document covers Python programming concepts related to iteration using 'while' and 'for' loops, as well as string manipulation. It explains multiple assignment, flow of execution in loops, control flow statements like break and continue, and string operations such as indexing, length, traversal, and concatenation. The document provides examples and outputs to illustrate these concepts effectively.

Uploaded by

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

3A_Python Programming Class 3

This document covers Python programming concepts related to iteration using 'while' and 'for' loops, as well as string manipulation. It explains multiple assignment, flow of execution in loops, control flow statements like break and continue, and string operations such as indexing, length, traversal, and concatenation. The document provides examples and outputs to illustrate these concepts effectively.

Uploaded by

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

Python Programming

Class-3
Iteration(while, for) & Strings
Multiple Assignment
• Make more than one assignment to the same variable.
• A new assignment makes an existing variable refer to a new value
(and stop referring to the old value).
• a=5
• print (a)
• a=7
• print (a)
>>> 5
>>> 7
While statement
• To automate repetitive tasks.
• Iteration is so common in program, Python provides several
language features to make it easier.
• The first feature we are going to look at is “ while” statement
n =4 Output:
while n > 0: 4
print (n) ARE
n = n-1 3
print("ARE") If n<=0 (False ARE
Condition) 2
Print(“END”)
ARE
1
ARE
END
While statement
• “While n is greater than 0, continue displaying the value of n and
then reducing the value of n by 1. When you get to 0, display the
word “END”
• OUTPUT:
Output:
4
ARE
3
ARE
2
ARE
1
ARE
END
Flow of Execution: while
• The flow of execution for a while statement:
1. Evaluate the condition, yielding 0 or 1.
2.If the condition is true (1), execute each of the statements in the
body and then go back to step 1.
3. If the condition is false (0), exit the while statement and continue
execution at the next statement.

This type of flow is called a loop


Infinite loop
The body of the loop should change the value of one or more
variables so that eventually the condition becomes false and the
loop terminates.
Otherwise, the loop will repeat forever, which is called an infinite
loop.
In the previous program,
While n!=8:
Makes infinite loop
Tabular Form Output:
1.0 1.0 * 7 = 7.0
x = 1.0 2.0 2.0 * 7 = 14.0
3.0 3.0 * 7 = 21.0
while x < 11.0: 4.0 4.0 * 7 = 28.0
print (x, "\t", x, "* 7 =", x*7) 5.0 5.0 * 7 = 35.0
6.0 6.0 * 7 = 42.0
x = x + 1.0 7.0 7.0 * 7 = 49.0
8.0 8.0 * 7 = 56.0
9.0 9.0 * 7 = 63.0
10.0 10.0 * 7=70 .0
.

Control Flow Statement-Break, Continue,else

The break Statement


✓break: Exits the loop immediately, even if the loop's condition is still true.
i=1 • Output:
while i <= 10:
if i == 5: 1
break 2
print(i)
i += 1 3
Print(“end”) 4
Print(i)
End
5
Continue
continue: Skips the rest of the code inside the loop for the current iteration and
proceeds with the next iteration.
• Output:
i=1 2
while i <= 7:
i += 1
3
if i == 5: 4
continue 6
print(i)
print(“end”) 7
print(i) 8
end
8
Else statement in while loop
• The else statement: Specifies a block of code to be executed
when the loop is finished Output:
i=1 1
while i <= 5: 2
print(i) 3
i += 1 4
else: 5
print("Loop completed successfully.") Loop completed successfully
“for” loop in python programming
✓The for loop in Python is used to iterate over a sequence (such as a list, tuple,
dictionary, set, or string) and execute a block of code for each item in the
sequence.
✓The for loop is very versatile and can be used with various data structures.

Syntax of a For Loop


for variable in sequence:
# code block

Iterating Over a List:

fruits = ['apple', 'banana', 'cherry'] Output:


for i in fruits: apple
print(i) banana
cherry
Using the range() Function
The range() function generates a sequence of numbers, which is often used with for loops

for i in range(5): • Output:


print(i)
0
1
2
3
4
Range()-starting point and end point
• for i in range(1, 6): • Output:
• print(i) 1
2
3
4
5
Range()-step value
for i in range(0, 10, 2): • Output:
print(i) 0
2
4
6
8
Strings
• Compound data type
• Three data types: int, float, and string. Strings are qualitatively
different from the other two because they are made up of smaller
pieces— characters.
• Types that comprise smaller pieces are called compound data
types.
• We can access its elements.
• The bracket operator selects a single character from a string.
fruit = "banana"
letter = fruit[1]
print (letter)
>>> a

b is the 0th letter (“zero-eth”) of "banana",


a is the 1th letter (“one-eth”),
and n is the 2th (“two-eth”) letter.
The expression in brackets is called an index.
• Alternatively, we can use negative indices, which count backward
from the end of the string.
• The expression fruit[-1] yields the last letter,
• fruit[-2] yields the second to last, and so on.
Length
• The “len” function returns the number of characters in a string:
• fruit = "banana"
• len(fruit)
• >>> 6
Traversal
• A lot of computations involve processing a string one character at
a time. Often they start at the beginning, select each character in
turn, do something to it, and continue until the end.
• This pattern of processing is called a traversal.
• One way to encode a traversal is with a while statement
While loop in strings
index = 0 • Output:
fruit = "banana“ b
while index < len(fruit): a
letter=fruit[index] n
print(letter) a
index = index+1 n
a
For loop in strings
fruit = "banana“ • Output:
for char in fruit: b
print (char)
a
n
a
n
a
String Concatenation
• Output:
prefixes = "JKLMNOPQ" Jack
suffix = "ack" Kack
Lack
for letter in prefixes:
Mack
print (letter + suffix) Nack
Oack
Pack
Qack
String slices
• A segment of a string is called a slice.
• Selecting a slice is similar to selecting a character
• s = "Peter, Paul, and Mary“
• print (s[0:5]) • Output:
• print (s[7:11]) Peter
• print (s[17:21]) Paul
Mary
• If you omit the first index (before the colon), the slice starts at the
beginning of the string.
• If you omit the second index, the slice goes to the end of the
string.
fruit = "banana“
fruit[:3]
>>>’ban’ - Ans
fruit[3:]
>>>’ana’ - Ans

You might also like