Learn to code in python

Solved
Anonymous user -  
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   -

Hello,

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

Chatmalin21 Posted messages 1 Status Member 1
 

Hello,

I advise you to look for a Python course to understand the basics.

1
georges97 Posted messages 14550 Registration date   Status Contributor Last intervention   2 912
 

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.

0
georges97 Posted messages 14550 Registration date   Status Contributor Last intervention   2 912
 

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

https://python.sdv.u-paris.fr/cours-python.pdf

0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
 

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!

0
Anonymous user
 

Could you recommend a book for beginners as I have zero background in Python? Despite your explanation, I still can't find the solution :/

0
PierrotLeFou > Anonymous user
 

I don't have a book to suggest but the following site:

https://zestedesavoir.com/tutoriels/2514/un-zeste-de-python/

1
Anonymous user > PierrotLeFou
 

ok thanks, I'll take a look

0
Anonymous user > PierrotLeFou
 

Small question: how many structures exist in Python? What are their names please? Thank you for your help.

0
blux Posted messages 5021 Registration date   Status Moderator Last intervention   3 455 > Anonymous user
 

Hello,

data structures, tests...?

0
PierrotLeFou
 

Are we talking about data structures or program structures?

0
Anonymous user
 

Are while and for loops "structures"?

0
blux Posted messages 5021 Registration date   Status Moderator Last intervention   3 455 > Anonymous user
 

Are while and for loops "structures"?

Yes, they are control structures in the program, like if then else...

0
Anonymous user > blux Posted messages 5021 Registration date   Status Moderator Last intervention  
 

Hello, can you explain what a "code release" is? Thank you :)

0
PierrotLeFou > Anonymous user
 

Your expression is too vague.

Is it how to exit a program or a loop?

Or how to display something (including the code itself).

0
Castoramoi Posted messages 400 Registration date   Status Member Last intervention   22
 

Hello

https://www.w3schools.com/python/default.asp


0
plouf
 

Hello,

There is also a tutorial on the Python website and in French.

https://docs.python.org/fr/3/tutorial/index.html

0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
 

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!

0
Castoramoi Posted messages 400 Registration date   Status Member Last intervention   22
 

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...


0
blux Posted messages 5021 Registration date   Status Moderator Last intervention   3 455
 

.

"for" as long as the condition is not met then we continue the loop...

No, precisely, the "for" loop is something with a predetermined number of occurrences.

What you're talking about would be more of a "do... while".

0
blux Posted messages 5021 Registration date   Status Moderator Last intervention   3 455 > Castoramoi Posted messages 400 Registration date   Status Member Last intervention  
 

Not clean, if it's already planned: do while or repeat until (0-n or 1-n).

0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940 > blux Posted messages 5021 Registration date   Status Moderator Last intervention  
 

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 loop
    for (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

0
Anonymous user > mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention  
 

Hello, thank you for your explanation,

I have to code the Tic Tac Toe game and I'm at the stage where I need to create the cross and circle. Who can help me? Thanks :)

0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940 > Anonymous user
 

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 :-)

0