Python_Lists_Loops

#Ex: Iterating Through a List of fruits in the list ["apple", "banana", "cherry"]
1. using For loop using range and len functions.
2. without range and len functions

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



While loop:
index = 0
while index <= len(fruits):
print(colors[index]) index = index + 1
For loop Type#1:
for fruit in range(len(fruits)+1):
print(fruit)
________________________________________________________________________________________________________________________

#Ex: Iterating Through a List of numbers in the list numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]
1. using For loop using range and len functions.
2. without range and len functions

numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]

For loop Type#1:
for i in range(len(numbers)+1):
    print(f"Index {i}: {numbers[i]}")

For loop Type#2:
for i in numbers:
print(f"Index {i}: {numbers[i]}")

While loop:
index = 0
while index <= len(numbers):
print(colors[index]) index = index + 1
________________________________________________________________________________________________________________________

#Ex: Iterating Through a List of colors in the list ["red", "green", "blue"]
1. using For loop using range and len functions.
2. without range and len functions
3. using enumerate()

Output:
Color at index 0 is red
Color at index 1 is green
Color at index 2 is blue


colors = ["red", "green", "blue"]

While loop:
index = 0
while index <= len(colors):
   print(colors[index])
   index = index + 1
For loop Type#1:
for i in range(len(colors)):
print(f"Index {i}: {colors[i]}")

For loop Type#2:
for i in colors:
print(f"Index {i}: {colors[i]}")

For loop Type#3:
for index, color in enumerate(colors):
print(f"Color at index {index} is {color}")

________________________________________________________________________________________________________________________

#Ex: Iterating Through a List of numbers and Squaring Each Element in a List.
numbers = [1, 2, 3, 4, 5]
1. using For loop using range and len functions.
2. without range and len functions
3. using enumerate()

Output:
[1, 4, 9, 16, 25]

numbers = [1, 2, 3, 4, 5]
squared = []
While loop:
index = 0
while index <= len(numbers):
squared_number= (numbers[index] ** 2) squared.append(squared_number) index = index + 1
print(squared)
For loop Type#1:
for i in range(len(numbers)):
   squared_number= (numbers[index] ** 2)
   squared.append(squared_number)

print(squared)
For loop Type#2:
for num in numbers:
squared.append(num ** 2)
print(squared)

________________________________________________________________________________________________________________________

#Ex: Iterating Through a List of numbers and Squaring Each Element in the SAME List.
    numbers = [1, 2, 3, 4, 5]
1. using For loop using range and len functions.
2. without range and len functions
3. using enumerate()

Output:
[1, 4, 9, 16, 25]

numbers = [1, 2, 3, 4, 5]

While loop:
index = 0
while index <= len(numbers):
squared_number= (numbers[index] ** 2) numbers[index]= squared_number
index = index + 1
print(numbers)
For loop Type#1:
for i in range(len(numbers)):
   squared_number= (numbers[index] ** 2)
   numbers[index]= squared_number

print(numbers)
For loop Type#2:
for num in numbers:
squared_number= (num ** 2) numbers[index]= squared_number
print(numbers)
________________________________________________________________________________________________________________________

#Ex: Filter Even Numbers from a List [1, 2, 3, 4, 5, 6,7,8,9,10] and assign them into a different list
1. using For loop using range and len functions.
2. without range and len functions
3. using List Comprehension (Alternative to for Loop)

Output:
[2, 4, 6,8,10]

numbers = [1, 2, 3, 4, 5, 6,7,8,9,10]
evens = [] odds = [] While loop: index = 0 while index <= len(numbers):
   if num % 2 == 0:
      evens.append(numbers[index])
   else:
      odds.append(numbers[index])
print(evens) print(odds)
For loop Type#1:
for i in range(len(numbers)):
   num = numbers[i]
   if num % 2 == 0:
     evens.append(num)
   else:
      odds.append(num)
print(evens)
print(odds)

For loop Type#2:
for num in numbers:   
   if num % 2 == 0:
     evens.append(num)
   else:
      odds.append(num)
print(evens) print(odds)

For loop Type#3:
evens = [num for num in numbers if num % 2 == 0]
print(evens)

________________________________________________________________________________________________________________________

#Ex: Write a program to print the list of pyramid with list ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]
ONE
ONE TWO
ONE TWO THREE
ONE TWO THREE FOUR
ONE TWO THREE FOUR FIVE

number_lst = ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]

for a in range(len(number_lst)):
print(number_lst[0:a])

for a in number_lst:
print(number_lst[0:a])

________________________________________________________________________________________________________________________

#Ex: Write a program to print the list of pyramid with list ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]
in the below format

ONE TWO THREE FOUR FIVE
ONE TWO THREE FOUR
ONE TWO THREE
ONE TWO
ONE


number_lst = ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]

for a in range(len(number_lst),0,-1):
print(number_lst[0:a])

________________________________________________________________________________________________________________________

#Ex: Write a program to print the list of pyramid with list ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]
in the below format

ONE 1
TWO 2
THREE 3
FOUR 4
FIVE 5


number_lst = ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]

for a in range(11):
print(number_lst[a],a)

________________________________________________________________________________________________________________________

#Ex: Write a program to print the list of pyramid with list ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]
in the below format
ONE Apples
TWO Bananas
THREE Mangos
FOUR Pears
FIVE Oranges

number_lst = ["ONE", "TWO" , "THREE", "FOUR", "FIVE"]
fruits_lst = ["Apples", "Bananas" , "Mangos", "Pears", "Oranges"]

for a in range(5):
print(number_lst[a],fruits_lst[a])

________________________________________________________________________________________________________________________

## Nested Loops


#Ex: Write a program to print the list of pyramid with list ["Indian", "Australian" , "American"]
and ["Apples", "Bananas" , "Mangoes"] in the below format

Indian Apples, Indian Bananas, Indian Mangoes
Australian Apples, Australian Bananas, Australian Mangoes
American Apples, American Bananas, American Mangoes

fruits_lst = ["Apples", "Bananas" , "Mangos", "Pears", "Oranges"]
country_lst = ["Indian", "Malaysian", "Australian", "American"]

for a in range(len(country_lst)):
for b in range(len(fruits_lst)):
print(country_lst[a] + " ", fruits_lst[b] , end=",")
print()

print("____________________________________________________")

for country in country_lst:
for fruit in fruits_lst:
print(country + " ", fruit , end=",")
print()

________________________________________________________________________________________________________________________


#Ex: Print each adjective for every fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]



for a in range(len(adj)):
for b in range(len(fruits)):
print(adj[a] + " ", fruits[b] , end=",")
print()

print("____________________________________________________")

for x in adj:
for y in fruits:
print(x, y)

------------------------------------------------------------------------------------------------------------------------

#Ex: Write a program to print the multiplication tables from 1 to 5 in the below format


numbers = [1,2,3,4,5]
digits = [1,2,3,4,5,6,7,8,9,10]

for a in range(len(numbers):
for b in range(len(digits)):
print(numbers[a] + " X ", digits[b] , numbers[a] * digits[b])
print()

print("____________________________________________________")

for number in numbers:
for digit in digits:
print(number + "X", digit , number*digit)
print()

________________________________________________________________________________________________________________________


#Ex: Nested with lists
nested_list=[[1,2,3],['a','b','c'],[1.1,2.2,3.3]]
for i in nested_list:
print(i)
for j in i:
print(j)


------------------------------------------------------------------------------------------------------------------------

#Ex:

cars = ["ok", "ok", "faulty", "ok", "faulty", "ok"]
for status in cars:
if status == "faulty":
print("Stopping the production Line")
break
elif status == "ok":
print(f"The car is {status}")
print("Shipping new car")


cars = ["ok", "ok", "ok", "faulty", "ok"]
for status in cars:
if status == "faulty":
print("Stopping the production Line")
break
elif status == "ok":
print(f"The car is {status}")
print("Shipping new car")
else:
print("All cars built successfully")


cars = ["ok", "ok", "faulty", "ok", "faulty", "ok"]
all_succesful = True

for status in cars:
if status == "faulty":
print("Stopping the production Line")
all_succesful = False
break
elif status == "ok":
print(f"The car is {status}")
print("Shipping new car")

if all_succesful:
print("All cars built successfully")

------------------------------------------------------------------------------------------------------------------------


7. Looping Through a List of Lists (Nested List)


matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

for row in matrix:
for num in row:
print(num, end=" ")
print()
Output:

1 2 3
4 5 6
7 8 9

________________________________________________________________________________________________________________________

#Ex: Nested with dictionaries
d = {'name': ['a','b','c'], 'Age': [10,20,30], 'Sal':[1,2,3]}
for i in d.values():
print (i)
for j in i:
print(j)

------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Python_While_Loop

clinical_app