Techmind Infotech Computer Education and Project Training Centre (A Government Approved Institution)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

TECHMIND INFOTECH

Computer Education and Project Training Centre


(A Government Approved Institution)
Sri Ram Towers 2nd Floor, Santhanathapuram 6th Street, Pudukkottai
Cell: 7293 52 7293 Email: techmindinfotech020@gmail.com

PYTHON PROGRAMMING – CLASS 4

ACCESSING LIST USING FOR LOOP

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

ACCESSING CHARACTERS IN A STRING USING FOR LOOP

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:

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

if x == "banana":

break

Output:

apple
banana

Break statement Example 2:

fruits = ["apple", "banana", "cherry"]

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.

fruits = ["apple", "banana", "cherry"]

for x in fruits:

if x == "banana":

continue

print(x)

Output:

apple

cherry
NESTED FOR LOOP

A = ["red", "big", "tasty"]

fruits = ["apple", "banana", "cherry"]

for x in A:

for y in fruits:

print(x, y)

CASTING

Casting in python is therefore done using constructor functions:

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

BREAK IN WHILE LOOP

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:

You might also like