I have executed three different for loop operations in Jupyter notebook. Code is given below
First for loop
b = {}
temp_1 = []
for x in range(0,4):
y = [4,5]
temp_1.append(x*y)
b[x] = temp_1
print("\n First for loop output \n",b)
Second for loop
b = {}
temp_1 = []
for x in range(0,4):
for y in range(4,6):
temp_1.append(x*y)
b[x] = temp_1
print("\n 2nd for loop output \n",b)
Third for loop
b = {}
temp_1 = []
for x in [0,1,2,3]:
for y in [4,5]:
temp_1.append(x*y)
b[x] = temp_1
print("\n 3rd for loop output \n",b)
Output of First for loop output
{0: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 1: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 2: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]], 3: [[], [4, 5], [4, 5, 4, 5], [4, 5, 4, 5, 4, 5]]}
2nd for loop output
{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}
3rd for loop output
{0: [0, 0, 4, 5, 8, 10, 12, 15], 1: [0, 0, 4, 5, 8, 10, 12, 15], 2: [0, 0, 4, 5, 8, 10, 12, 15], 3: [0, 0, 4, 5, 8, 10, 12, 15]}
Q1: I am beginner to Python. I have been doing simple coding in MATLAB C and Arduino UNO controller. Literally, I am failing to understand how for loop works in Python. Particularly, what is happening in First for loop and second and third.
Q2: I wanted the output something like {0:[0,0], 1:[4,5],2:[8,10],3:[12,15]}. Could you please help me with proper code which can fetch me this output.