Nested loop in PYTHON

 
Programe is:
(March 28, 2022)
#
#    for loops in PYTHON
#    for i range(n) i =0, 1, 2, 3, ..., n-1
#    for i range(k,n) i=k, k+1, k+2, ..., n-1
#    for i reange(k,n,r) i =k,k+r, k+2r,k+3r,..., k+xr and k+xr<n
for i in range(10):
    print(i)
nr = int(input('Loops numbers is: '))
sum = 0
for i in range(nr):
    sum += i
print('Sums 0, 1, 2, .., ', nr, 'is: ', sum)
#
#  Odd numbers sum , i < n
#
n = nr = int(input('Numbers is: '))
for i in range(1, n, 2):
    print(i)
#
#   for loops în list
#
cars_A = ["BMW", "FORD", "AUDI", "DACIA",  "LAMBORGINI"]
cars_B = ["PORSCHE", "TATA", "VOLVO", "SAAB", "PEUGEOT"]
print( '\n List cars_A is: ')
for i in cars_A:
    print(i, end = ' ')
print( '\n List cars_B is: ')
for i in cars_B:
    print(i, end = ' ')
print( '\n List cars_A + cars_B is: ')
for i in (cars_A + cars_B):
    print(i, end = ' ')
#
#     word decomposition and composition
#
Word = "NABUCODONOSOR"
Len_Word = len(Word)
Characters = [" "]*Len_Word
j = 0
for i in Word:
    Characters[j] = i
    j = j +1
for j in range(Len_Word):
    print(Characters[j])
    j = j +1
j = 0
Word_work = " "
for j in range(Len_Word):
    Word_work = Word_work + Characters[j]
print("recomposed word is:", Word_work)
#
#    for and else
#
for i in cars_B:
  print(i)
else:
  print("List finished!")
#
#    nested loop
#
Matrix = [[1,2,3,4,5],
          [2,3,4,5,6],
          [3,4,5,6,7],
          [4,5,6,7,8]]
No_row = len(Matrix)
No_column = len(Matrix[0])
Sum = 0
i = 0
for i in range(No_row):
    j = 0
    for j in range(No_column):
        Sum += Matrix[i][j]
        j = j + 1
    i = i +1
print('Component sum in list of lis is: ', Sum)
    

Results are:


============== RESTART: /Users/ionivan/Documents/ForLoopsPython.py =============
0
1
2
3
4
5
6
7
8
9
Loops numbers is: 10
Sums 0, 1, 2, ..,  10 is:  45
Numbers is: 19
1
3
5
7
9
11
13
15
17
 List cars_A is: 
BMW FORD AUDI DACIA LAMBORGINI 
 List cars_B is: 
PORSCHE TATA VOLVO SAAB PEUGEOT 
 List cars_A + cars_B is: 
BMW FORD AUDI DACIA LAMBORGINI PORSCHE TATA VOLVO SAAB PEUGEOT N
A
B
U
C
O
D
O
N
O
S
O
R
recomposed word is:  NABUCODONOSOR
PORSCHE
TATA
VOLVO
SAAB
PEUGEOT
List finished!
Component sum in list of lis is:  90

(March 28,2022)


Comments

Popular posts from this blog

Pointers in PYTHON?

Generates M prime numbers