Beginner’s Guide to Python Programming

This code can create a loop:

for i in range(15):

print(“e”)

This will output 15 e’s.

Note that there is a space before print, which is the standard indentation in Python. Four spaces indicate that the code is within the loop. If you want to remove the code from the loop, you can delete those four spaces. If you enter a colon and then press enter, you will find that it automatically indents four spaces for you.

The ‘for’ is a keyword, ‘i’ is a variable, ‘in’ is a keyword, and ‘range’ means the range.

15 is the number of iterations for the loop.

Each time the loop runs, ‘i’ will increase by one, starting with ‘i’ defaulting to zero. If you add

10,

that is, add a comma after 10 and then 15.

At this point, ‘i’ will start from 10. You can try adding print(i) inside the loop.

You might say, isn’t print unable to output a letter or text without quotes?

Now we are using print to output a variable.

Now every time you create a new line, it will automatically indent four spaces,

indicating that the code you are writing is within the for loop.

If you want to exit the loop, just press

Backspace once

to delete the four spaces.

In the future, we will publish a dedicated article explaining the for loop.

Let’s talk about conditional statements.

if condition:

content to execute

This ‘if’ means ‘if’. If the condition is true,

then the content to execute will be executed.

Let’s demonstrate

for i in range(10):

if i==6:

print(i)

You might say, shouldn’t there be only one equals sign?

This is a conditional statement; let me explain the format.

To check if one number is greater than another, use the greater than sign. To check if it is less than another number, use the less than sign.

However, to check for equality, you need to use two equals signs.

Traceback

(showing error information)

This is an error message. If you do not use two equals signs, this will occur. However, there is another situation where a pop-up appears showing the error information.

Also, if you use greater than or equal to or less than or equal to,

note that greater than or equal to uses a greater than sign followed by an equals sign, and less than or equal to uses a less than sign followed by an equals sign. Note that these symbols must be in English mode.

To indicate not equal, use an exclamation mark followed by an equals sign.

If you want to see these,

we will later publish a symbol reference chart.

Now for modules. Let’s first import a random module.

from random import *

The asterisk means all.

‘From’ means from.

‘Random’ is the English name of the random module.

‘Import’ means to import.

Today we will only learn this form of import.

Putting it all together means importing all functionalities from the random module.

Now we use the randint function,

enter in the editor:

print(randint(10,20))

Using Run module,

you can generate a random number between 10 and 20.

Next time we will talk about IDs.

Leave a Comment