Python Forum
Explain this code - for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explain this code - for loop
#1
Hi,
I'm doing Hackerrank's challenge "Learn to Code in 30 days" and this is link for the challenge.
However I was trying this :

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input())

    i = 0
    for i in range (1,11):
       for j in range (n):
     print ( i * j ) 
but doesn't work and I found the solution :
#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input())

    print( *['%d x %d = %d'%(n, i, n*i) for i in range(1, 11)], sep="\n" )
My question is that I don't understand the soultion that the only line of code and what are% d and % in Python?
Regards,
RavCoder
Reply
#2
in your first snippet you need to indent the print 2 levels - to be indented one level compared to inner loop.

for i in range(1, 11):
    for j in range(n):
        print (i * j)
few observations:
  • you may want to change the second for loop to start from 1 and include n. Currently it will start from 0 and will not include n
  • you may want to change the print function to display also bot multiples not just the result.

in the second snippet the syntax with d% that you ask about is called string formatting. that is so called old-styled string formatting. there is newer str.format() method and from 3.6+ - also f-strings
Here is the docs for string format syntax
Here is comparison between old-style and str.format() method: https://pyformat.info/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
%d is old style string formatting, it means to format the item as a integer. The items to be formatted are in the tuple on the right side of the % operator.

In your code, you don't need the second for loop. Get rid of that, and in the 'for i' loop print(i * n).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-24-2019, 12:28 PM)ichabod801 Wrote: In your code, you don't need the second for loop.
Oh, I didn't check the assignment. Thought it's a multiplication table for all numbers in [1,n]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
That's what I thought at first, too, but the link seemed safe. And note to RavCOder, this is why you avoid links in forum post. We get our fair share of shady people here, so many of the regulars are very wary about clicking links. That's why it's better if you can explain the problem yourself. Also, not just that it doesn't work, by how it is not working.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Thanks for your advices, but how can I do to print this : " result = n * i " without use string formatting?
I thought of it as a matrix where I put in the first operator loop n and in the second operator i then did the product of both.
I don't know if my reasoning is correct.
P.s Sorry I will remember to explain better my problem I don't know that in this forum there are shady people.
Reply
#7
why do you want to do it without string formatting?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
I thought you could do without, but if they are needed I will do them with string formatting only I didn't want to use that solution because it's not mine.
Reply
#9
You can do most things in several ways and it's up to you what to choose. you can print without string formatting but it's just not so 'nice'

n = 5
for i in range(1, 11):
    print(n, '*', i, '=', n * i)
# using f-strings
for i in range(1, 11):
    print(f'{n} * {i:<2} = {n * i}')
both loops will produce the desired output, however the second one will have = alligned
Output:
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Output:
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Thank you very much, I don't understand though if I was close to the solution according to you, because I'm doing it also to learn how to solve the tasks that are there and increase my logic in the programming.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 711 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 974 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,059 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,059 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,201 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,466 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  What is the run time complexity of this code and please explain? samlee916 2 2,258 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,248 Oct-12-2020, 01:59 PM
Last Post: t4keheart
  Explain range in this code RavCOder 4 2,254 Oct-02-2019, 05:04 PM
Last Post: jefsummers
  Need explain about the code ap111 2 1,914 Sep-12-2019, 03:10 PM
Last Post: luoheng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020