Use of recursion for repetitive algorithms

--Originally published at angelmendozas

I saw this on: http://kenscourses.com/tc101winter2015/2015/use-of-recursion-for-repetitive-algorithms-6/

Basically, recursion in computer science is a method where the solution to a problem is based on solving smaller instances of the same problem.

Captura de pantalla 2016-11-07 a las 18.41.29.png

Recursion is multiplying n times n-1’s factorial f.e. if you choose 4 then the answer is 4×3! so its 4x3x2x1


When to use what type of repetition in a program

--Originally published at angelmendozas

Depends on the program you are writing, the if loop does or stops the desired task until what you programmed tells the program to do so.

While loop does a task as long as something happens to do that task, after that it stops.

A for loop can be combined with while but for a desired value the program does something.


Validated user input (ensure correct/expected data entry)

--Originally published at angelmendozas

To ensure the expected entry is correct you need to use a nested statement, so the program ensures the user input is exactly how you want it (well…the program).

Captura de pantalla 2016-11-07 a las 17.45.52.png

The program uses float bc there can be a positive or negative number in the input, then allows the user to write something with the input function, then as the comment says, it checks if the number is positive, negative or zero and prints whatever it is depending on the input.


Nesting of conditional statements

--Originally published at Py(t)hon

You may think there is no more you must know of the conditional statement, well… You are wrong, there is another knowledge call nesting that must master. Perhaps you didn’t know that in a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.

There may be a situation when you want to check for another condition after a condition resolves to true, this is what is call nesting, a conditional inside another conditional.

Here is an example:

nesting-1

Here is a youtube video:

That’s all #Pug #Nesting #If #Python #ISC #Tec #TC101

 


Else and Elif

--Originally published at Py(t)hon

Continuing with the course, we have the next step that is the else and elif statement. Let’s start with else, in the else statement is where you are going to put the block of code that you want to execute if the conditional in the if statement turns out to be False or 0.

Here is an example:

else-1

On the other hand, we have the elif expression, that allows you to check for multiples condition for True and as soon as one of then turns out to be True, run.

Here is an example:

elif-1

Here is a video, for the people than didn’t understand me:

#Tec#Python#Else#Elif#ISC#TC101#Pug


If you click on this post…

--Originally published at Py(t)hon

This time we are going to see   the conditional if and how does it work. The conditionals statement performed different actions depending on how the boolean was evaluated false or true. The general form of the “if” statement is the follow.

If BOOLEAN :

STATEMENTS

There are some important things to remember about the if statement when use:

  • The colon (:) is required. It separates the header from the body.
  • The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  • All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.

Here is an example:

if-1

Here is a tutorial if you didn’t get it:

That’s all #Pug#Tec#ISC#If#Python#Basic


Modules and libraries

--Originally published at Py(t)hon

This time we are going to see the Modules or Libraries what are those things, in simple terms they are Python files that contain definitions and statements, this ones can be imported or well created by yourself.

To import one of this modules and use it in your program is very easy, you just type “import” follow by the name of the python file (without the suffix .py). Then when you are going to use it for pi or cos(x) you have to first type the name of the module, then the pi or the cos(x). Here is an example:

modules 1

With the previous function we did with the help of the module math, we are going to use it as a module:

modules2

That’s the basic of modules and libraries, see you next time. Here i leave you a youtube tutorial maybe you understand it better with this video. #Pug#Python#Modules#Libraries#Tc101


Calling & Creating Functions

--Originally published at Py(t)hon

I think is time to start with functions, i’m no experts at this topic so maybe we can learn together, let’s go

First, what is a function? Very simple, is a piece of reusable code that can be call upon whenever you need it. In other words, if you are going to repeat a code several time the best you can do is create a function, so you don’t have to write it all over again and again. Is to make your life easier.

To create a function you have to start with def follow by parenthesis (), inside this parenthesis goes the parameters necessary, if needed of course, after the parenthesis goes a colon (:) and then to close it, use the return function follow by the value statement, if you don’t want it to return anything, just leave a blank space, the system will take it like None.

Here is my example:

def1.png

In this example I can call whenever I want the areacuadro function, that will be all:) #Pug#Tec#TC101#Functions

Here is a webpage where you can find it better explain link

 

 


Input = not so difficult

--Originally published at Py(t)hon

Hi again, today we are going to see how to make an input, it is actually not that hard so i think you won’t have any difficulties.

So this is how it goes:

Something = input ( whatever you want to ask)

Then you can go like:

Print = (“This” + Something + “ is very nice”)

Here is one example:

input1

Inputs are not difficult so don’t waste a lot of time with them, focus on more important things, but if you have difficulties you can always ask Ken.