Learning Python from scratch #01: comments, indentation, variables and functions

In Python, like in almost all languages, comments can be of two types: on a single line or distributed across multiple lines. A single line comment is inserted with the symbol ‘#‘ while a multiline comment begins and ends with the sequence ‘ “””‘ (triple quotes).
Python comments

The thing that differentiates Python from all the other high-level languages is the fact that it uses the ‘indentation to identify logical blocks of code to execute. For example, in a classic if-else statement, the blocks of code to be executed for each of the two branches depending on the validity of the condition, which in the other languages are typically identified by grouping the instructions within a pair of braces {}, in Python are identified simply by using the code indentation.
Python conditional blocks

In the image above, in addition to the definition of the blocks by the indentation, we can see other two things:
– The definition of an integer variable ‘i’
– The use of the symbol ‘:’ (colon) in the if-else constructs

Variables in Python do not need to be declared and do not require their type to be explicitly defined. The assignment of a value to a variable name is the actual declaration and initialization of the variable that is then ready to use.
In the following figure we create and then print the value of three variables, an integer, a string and a floating point number, without explicitly stating their type:

Python variables

The restrictions on the variables names in Python are similar to those of almost all other languages, namely:

– They can only begin with a letter or with the ‘_’ (underscore) character
– They can contain letters, numbers and underscore
– “Keywords“, so the reserved words of the language, cannot be used

Python keywords are:

and      continue  else      for      import    not      raise 
assert   def       except    from     in        or       return 
break    del       exec      global   is        pass     try 
class    elif      finally   if       lambda    print    while 

Let’s see now how to define a function.
To define a function in Python the keyword def is used followed by the name of the function, by the parameters list if any and by the ‘:’ (colon) character after which begin the instructions that represent the function body.
For example:

def sum (n1, n2): 
	print("Result=", n1+n2)

The following image shows the definition of a function and its invocation with the necessary parameters.
Python functions

A function can also be defined with the optional parameters . These are parameters for which we specify a default value and for this reason is not necessary to supply them in the function call.
The following image shows a new version of the “sum” function in which the second parameter is defined as optional by the assignment of the default value 5. As we can see, in this case, the function could be invoked in two ways: providing both the parameters or just one of them. In the second case, for the variable n2 the default value is used, so the output value we get is 15.

""" declaration of sum function
It takes in input two values to sum and it prints the result
The second parameter is optional
If the value is not provided it takes the default value
defined during the function declaration """
def somma(n1, n2=5):
    print("Result =",n1+n2)

#two variables definition
i = 3
n = 10

#the function is invoked with 2 parameters

#the function is invoked with just one parameter

Python functions optional parameters

In this case the function sum print directly the result but does not return any value to the caller. It also possible to define functions that return a value. In the function definition does not change anything, in the sense that it is not necessary to specify that the function returns a value of some kind. It’s enough to make the function returning the desired value using the return statement, followed by the value to be returned.
We can modify the “sum” function so that, instead of directly print the result, it returns the value of the sum which will then be printed from the main program:

def somma(n1, n2=5):
	return n1+n2

i = 3
n = 10
ris = somma(i)

Python function return value

Leave a Reply

Your email address will not be published. Required fields are marked *