Control structures in Python

Control structures in Python

For control structures, Python possesses the control statements typical of high-level languages

Variable statement

Variables in Python are not assigned a predefined type

In Python the type of variables depends on the value they contain at all times

Therefore it performs an automatic conversion of types

Python recognizes the following types of values:

  • Numbers
  • Boolean values

    true and false

  • Strings
  • Lists

    data type that associates values with a numeric value by putting the contents of the list in square brackets, separating each of the elements by a comma

  • Tuples

    data type that associates values with a numeric value by putting the contents of the list in parentheses, separating each of the elements by a comma

    They have the peculiarity of being immutable

  • Dictionaries

    type of data that associates a value with a key and allows you to search for those keys

  • Objects

    Created by the programmer or pre-defined by the language

As it is a language with dynamic typing, it is not necessary to tell the interpreter its type of variable, but it is he who decides the type that will assign

The names with which we can name variables in Python follow several rules:

  • Names always start by letter
  • Within the name you can use any letter or number within the utf-8 encoding
  • Python reserved words, which are used in your code syntax, cannot be used

    For more information on these names, see the Python documentation

Note that Python is case sensitive, so two variables with the same name but containing an uppercase letter will be understood as two distinct variables

For example Variable is different of variable

There are also certain conventions to reduce errors and improve readability

To avoid errors it is recommended not to use certain characters:

  • Do not use neither l (l lowercase) neither I (i uppercase), because depending on the type of font used they may be confused with each other or the number 1
  • Do not use Or (o uppercase) because depending on the type of font used it may lead to confusion with the number 0

In order to promote readability, the following suggestions are made:

  • variablename is unreadable, because it's all together and can make it difficult to read
  • variableName is a little more readable, when applying uppercase letter when starting word
  • variable_name is the one that brings the best readability, when applying underscore at word start, which is the most recommended way

The if sentence

The if sentence has the form:

If the condition is true, instruction 1 will be executed; otherwise it is executed if there is instructution 2

The use of the sentence else is optional, so brackets have been used in the definition

If omitted, the instruction block will only be considered when the condition is true

An instruction block is a set of tab statements after the statement if, when jumping from line after the symbol :

They are mandatory as they tell the interpreter that the instruction block belongs to the sentence if

If omitted and the condition was true, the following instruction will be executed

Otherwise, the subsequent instruction will be executed, independent of the condition state

In this way, the omission of tabulations after the sentence if will allow us to write everything on a single line after the symbol :

The elif sentence

We can also use sentences if anities by sentencing elif

Sentences elif work just like a sentence if

But they will only be executed in case the condition of the sentence if was false

We can optionally add a statement else in the end but will only be executed if all the above conditions were false

The while sentence

The while sentence has the form

If the condition is met, the sentence or instruction block is executed and the process is repeated until the condition is no longer met

The for sentence

This loop has a syntax very similar to the for-each Java

Iterates a variable var over all the properties of an object obj that is passed to it

Thus, for each value of var is is executed, the judgments of the loop

Therefore, the loop will have as many iterations as the object's properties, and in each iteration the variable will have the value of the corresponding object's property with that iteration

Its syntax is:

Iterators

In Python there are different structures that are sets of elements, they are called collections

These types of structures are iterable, that is, they can be traversed element by element

Some variable types that are iterable are:

  • Character string (str)
  • List (list)
  • Tuple (tuple)
  • Dictionary (dict)

Its use is useful for traversing collection items such as lists, tuples, or objects

In addition, many times we want to repeat a loop a certain number of times

The function of this can be useful for this range(n)

This function generates an iterable ranging from 0 to n – 1

The break sentence

The break sentence can be placed within a loop or anities loops

When you execute the break sentence exits the loop more internal

To all effects, the break sentence acts as a jump to the sentence following the loop in which it runs

The continue sentence

The continue sentence does not leave the loop but causes the next iteration to run

In the loop while the execution of the continue causes the program flow to jump to the condition

In the for loop the continue execution causes the increment expression to run, and then continue normally with the condition

That is, running the continue prevents the rest of the loop body from running