Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

# WAP Program Pritn All The Lements From The Given List

The document discusses for loops in Python. It provides examples of using for loops to iterate through lists, tuples, and strings. The examples demonstrate printing each element, calculating the square of each element, and filtering a list to only include even numbers. For loops allow automating repetitive tasks on each element in an iterable object like a list or string in Python.

Uploaded by

Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

# WAP Program Pritn All The Lements From The Given List

The document discusses for loops in Python. It provides examples of using for loops to iterate through lists, tuples, and strings. The examples demonstrate printing each element, calculating the square of each element, and filtering a list to only include even numbers. For loops allow automating repetitive tasks on each element in an iterable object like a list or string in Python.

Uploaded by

Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/11/2018 Python_Note_loops1

In [1]: # WAP program pritn all the lements from the given list

x = [10, 34, 16, 19, 20]


for ele in x:
print ele

10
34
16
19
20

In [2]: # Note: In python the for loop wil work based on iterator

In [3]: for ele in "Banaglore":


print ele

B
a
n
a
g
l
o
r
e

In [6]: d = (10, 20, 30) # tuple


for ele in d:
print ele

10
20
30

In [8]: # Write a program find suare of each and every element from the given list
x = [2, 4, 6, 8] # List
for ele in x:
print "Suare of element {} is {}".format(ele, ele*ele)

Suare of element 2 is 4
Suare of element 4 is 16
Suare of element 6 is 36
Suare of element 8 is 64

http://localhost:8889/notebooks/Python_Note_loops1.ipynb 1/3
2/11/2018 Python_Note_loops1

In [12]: # Write a program find suare of each and every element from the given list , and p
x = [2, 4, 6, 8]
print "Input: {}".format(x)
y = [] # Empty list for to store output value
for ele in x:
y.append(ele * ele)
#print y
print "Output: {}".format(y)

Input: [2, 4, 6, 8]
Output: [4, 16, 36, 64]

In [13]: # Write a program find suare of each and every element from the given list , and p
x = [2, 4, 6, 8]
print "Input: {}".format(x)
x[0] = x[0] * x[0]
x[1] = x[1] * x[1]
x[2] = x[2] * x[2]
x[3] = x[3] * x[3]
print "Output: {}".format(x)

Input: [2, 4, 6, 8]
Output: [4, 16, 36, 64]

In [17]: # Write a program find suare of each and every element from the given list , and p
x = [2, 4, 6, 8]
print "Input: {}".format(x)
for index in range(0, len(x)):
print "Value at index {} is {}".format(index, x[index])

Input: [2, 4, 6, 8]
Value at index 0 is 2
Value at index 1 is 4
Value at index 2 is 6
Value at index 3 is 8

In [18]: # Write a program find suare of each and every element from the given list , and p
x = [2, 4, 6, 8]
print "Input: {}".format(x)
for index in range(0, len(x)):
x[index] = x[index] * x[index]
print "Output: {}".format(x)

Input: [2, 4, 6, 8]
Output: [4, 16, 36, 64]

http://localhost:8889/notebooks/Python_Note_loops1.ipynb 2/3
2/11/2018 Python_Note_loops1

In [22]: # Write a program find even numbers from the given list
x = [427, 428, 430, 429, 269, 263 , 464, 560]
y = []
for ele in x:
if ele % 2 == 0:
y.append(ele)
print "Output: {}".format(y)

Output: [428, 430, 464, 560]

In [ ]:

http://localhost:8889/notebooks/Python_Note_loops1.ipynb 3/3

You might also like