Python 3 - General programming - Statements

In Python 3 we cannot increment a variable directly with "++" before or after this variable.

We have to add + 1 on the variable to increment it.
That's a bit annoying but life is life.

In this tutorial we are going to see examples of the Python 3 statements.

While statement

We use the while statement to iterate through a number. An int for example.

Code

i = 0
end = 10

while i < end:
    print(i)
    i = i + 1

Result

0
1
2
3
4
5
6
7
8
9


For statement

We use the for statement to iterate through something that have data such a string (has characters), an or a list (have items) for example.

Code with a string

i = 0
myString = "Hello World!"

for i in myString:
    print(i)

Result with a string

H
e
l
l
o
 
W
o
r
l
d
!

Code with a list

i = 0
myList = ["Hello", 8, "World", 99]

for i in myList:
    print(i)

Result with a list

Hello
8
World
99

Easy but useful. angel

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.