Professional Documents
Culture Documents
Techmind Infotech Computer Education and Project Training Centre (A Government Approved Institution)
Techmind Infotech Computer Education and Project Training Centre (A Government Approved Institution)
Techmind Infotech Computer Education and Project Training Centre (A Government Approved Institution)
for x in fruits:
print(x)
for x in "banana":
print(x)
Output:
BREAK STATEMENT
With the break statement we can stop the loop before it has looped through all the items.
Example 1:
for x in fruits:
print(x)
if x == "banana":
break
Output:
apple
banana
for x in fruits:
if x == "banana":
break
print(x)
Output:
Apple
CONTINUE STATEMENT
With the continue statement we can stop the current iteration of the loop, and continue with the
next.
for x in fruits:
if x == "banana":
continue
print(x)
Output:
apple
cherry
NESTED FOR LOOP
for x in A:
for y in fruits:
print(x, y)
CASTING
int() - constructs an integer number from an integer literal, a float literal (by removing all
decimals), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)
Ouput:
3
Example 2:
x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(x)
print(y)
print(z)
print(w)
Output:
1.0
2.8
3.0
4.2
Example 3:
x = str("s1")
y = str(2)
z = str(3.0)
print(x)
print(y)
print(z)
WHILE LOOP
i=1
while i < 6:
print(i)
i += 1
Output:
i=1
while i < 6:
print(i)
if (i == 3):
break
i += 1
Output:
3
Another Example:
i=1
while i < 6:
if (i == 3):
break
print(i)
i += 1
Output: