Learn to code in python
SolvedHello,
I would like to learn how to use Python. Could you please explain the while and for loops, and how I should "create a program" that displays a multiplication table in the terminal? Thank you.
8 answers
Hello,
A forum is not the ideal medium for the complete learning of a language, as one basic question leads to another, whereas comprehensive and progressive courses exist, particularly on Python.
See:
https://www.python-lycee.com/parcours-apprentissage
https://www.irif.fr/~sangnier/enseignement/16-17/Ip1-Python/IP1-Python-cours-td-1-4.pdf
Hello,
The question you’re asking is a basic one, so I can only advise you to quickly look through a Python course, perhaps this one.
I also strongly recommend that you read this before asking for help with an exercise. It would be good to show what you have started to do and explain what is blocking you.
Now, back to your question. A program is a sequence of instructions. You probably know that you can write a message using the print function. Here’s a very simple Python program:
print("Multiplication Table") Let’s now see how to display a string that depends on two multiples (let’s say a and b). There are several syntaxes in Python:
- The simplest:
a = 2 b = 3 print(a, " * ", b, " = ", a * b)
- The f-strings:
a = 2 b = 3 print(f"{a} * {b} = {a * b}") Both are perfectly valid; you can choose whichever you want.
Now, let’s see how to create a loop. In the case of multiplication tables, the number of iterations is known before entering the loop, so a for loop is more appropriate than a while loop. The advantage of a for loop is that, by design and thanks to certain constraints imposed by Python, it usually prevents you from accidentally creating an infinite loop (unlike while loops). A for loop allows you to update a variable by iterating over an iterable, for example, a sequence of values (called range in Python). In Python:
- range(n) means counting from 0 inclusive to n exclusive, with a step of 1
- range(n0, n) means counting from n0 inclusive to n exclusive, with a step of 1 (make sure n0 <= n)
- range(n1, n2, s) means counting from n1 to n2 exclusive, with a step of s (make sure s is non-zero and that n1 < n2 if s > 0 or that n1 > n2 if s < 0).
Here we want to count from 1 inclusive to 10 exclusive, so what we need is range(1, 10) (which can also be written as range(1, 10, 1)). Here’s a program that displays these values:
for a in range(1, 10): print("a =", a) ... or:
for a in range(1, 10): print(f"a = {a}") Let’s call this variable a. Then you need to multiply a by b, where b is the multiple considered in the table.
You now have all the elements to finish your exercise.
Good luck!
Hello,
There is also a tutorial on the Python website and in French.
https://docs.python.org/fr/3/tutorial/index.html
Hello everyone,
@everyone: If you want to suggest references, please do so in the other discussion thread rather than the one 17159642 opened.
17159642
- I think the message #1 sufficiently addresses the initial problem, but if it's not clear enough, don’t hesitate to ask for clarification.
- Please make sure to stick to this initial question. If you have other questions, start a new discussion (avoid starting new discussion topics as you have done in #3, #14 unless, of course, they are directly related to the discussion).
- I also encourage you to get into the habit of searching for answers yourself before asking questions. Not that we don’t want to answer you, but it’s an exercise we regularly face when developing, so it’s best to get into the habit right from the start.
Happy coding!
I don't program in Python, but as for the while and for instructions, I think it's pretty much the same as in other languages!?
"while" repeat the action as long as the condition is true
"for" as long as the condition is not met, we continue the loop...
Just to clarify
- In Python, a for loop is used to iterate over a variable within a loop according to an iterable.
for i in range(10): print(i)
It is therefore not like in Java, C, or C++, since in these languages the classic for loop considers an initialization, a test to stay in the loop, and a statement to execute at the end of the loopfor (int i = 0; i < 10; i++) { printf("%d\n", i) } - In Python/C/C++/Java, a while loop executes a block of statements if and only if its condition is met. This block is repeated as long as the condition remains true.
i = 0 while i < 10: print(i) i += 1
- In C/C++/Java, a do while loop executes a block of statements at least once and repeats it only if its condition is met. This kind of loop does not exist in Python (one must manage with a while loop).
Good luck
Hello,
- Regarding the tic tac toe, this is a new discussion so please open a new thread.
- Concerning the initial topic, if you have all your answers, please mark the topic as resolved. Otherwise, please specify which points need clarification.
Thank you :-)
Hello,
This is what several of us have advised, to no avail. You don't learn to program by chatting on a forum. It's a fallback option in case of a blockage, after you have grasped the basics of the language.
The dedicated websites (see above) are designed for this purpose and include glossaries and definitions of functions and other keywords as well as examples, sometimes interactive, for each concept.