Use of recursion for repetitive algorithms

--Originally published at Start in the world of the #TC101

Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfils the condition of recursion, we call this function a recursive function.

Termination condition:
A recursive function has to terminate to be used in a program. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not met in the calls.

Example:

 

 

Source: http://www.python-course.eu/recursive_functions.php


When to use what type of repetition in a program

--Originally published at Start in the world of the #TC101

To keep a computer doing useful work we need repetition, looping back over the same block of code again and again.

  • For Loop

The for loop that is used to iterate over elements of a sequence, it is often used when you have a piece of code which you want to repeat “n” number of time

  • While Loop

The while loop tells the computer to do something as long as the condition is met it’s construct consists of a block of code and a condition.

  • Nested Loops

In some script you may want to use nested loops. A nested loop is a loop inside a loop.

  • Break

To break out from a loop, you can use the keyword “break”.

  • Continue

The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.

Source: http://www.pythonforbeginners.com/loops/for-while-and-nested-loops-in-python (You can find here some examples)


Creation and use of Lists/Tuples

--Originally published at Start in the world of the #TC101

Two of the most commonly used built-in data types in Python are the list and the tuple.

Lists and tuples are part of the group of sequence data types—in other words, lists and tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type, including the nothing type defined by the None keyword.

The big difference between lists and tuples is that lists are mutable, however tuples are immutable. Meaning once tuples are created, objects can’t be added or removed, and the order cannot be changed. However, certain objects in tuples can be changed, as we’ll see later.

Creating a Python List or Tuple

Creating a list or tuple is easy, here are some empty ones:

 

*To create non-empty lists or tuples, values are separated by comma

*To create a tuple with only one value, add a trailing comma to the value.

Example:

 

Source: http://pythoncentral.io/python-lists-and-tuples/


Creation and use of strings

--Originally published at Start in the world of the #TC101

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.


Accessing Values in Strings

Python does not support a character type; these are treated as strings of length one, thus also considered a substring.

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.


Updating Strings

You can “update” an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether.


Source: https://www.tutorialspoint.com/python/python_strings.htm


Use of “else” with a conditional (and elif for Python)

--Originally published at Start in the world of the #TC101

It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false, if else.

flowchart_if_else.png

 

if BOOLEAN EXPRESSION:
    STATEMENTS_1        # executed if condition evaluates to True
else:
    STATEMENTS_2       # executed if condition evaluates to False

Example:


Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional

flowchart_chained_conditional.png

Example:

Source: http://www.openbookproject.net/books/bpp4awd/ch04.html


Use of the conditional “if”

--Originally published at Start in the world of the #TC101

 

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly.

A few important things to note about if statements:

  1. The colon (:) is significant and required. It separates the header of the compound statementfrom the body.
  2. The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  3. All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.

Example: