Nesting of conditional statements

--Originally published at Luis Santana's Blog

This is me right now hehe. giphy

Now, talking about Python and programming. There will be times where you may want to check a condition after another condition solves for true, this means that inside an “if” condition you can have another “if” condition and several “elif” conditions too. You can use this tool to make your code easier to understand and help you finish earlier.

Here you can see an example:

nesting

You can check this link I used to get my information:

https://www.tutorialspoint.com/python/nested_if_statements_in_python.htm


Creation of sytrings and how to use them

--Originally published at Luis Santana's Blog

This blog is really special to me, and I would like to take a minute to explain it why. This post means that I’m half way there and just missing four posts.

2002

In this post I will talk about strings and how to create them. In order to create a string you should enclose your text in quotes, (it is the same if you use single quotes or double quotes). You can see an example below:

string

Check this link if you want to learn more about strings: https://www.tutorialspoint.com/python/python_strings.htm

 


Creation and use of lists/tuples

--Originally published at Luis Santana's Blog

This two are functions of Python. Starting by defining a list. A List is a list of values, each one is numbered starting from zero, you can modify the values of your list, you can substract them if you want and you can add values.

A tuple is very similar to a list but the main difference is that you can’t change your values, that means that with the values you start are the ones you are going to finish with.

How to create them, a tuple is really easy to create, first of all you give a name to your tuple and then after that you give your values.

And to create a list is exactly the same but you need to use square brackets, not parenthesis. Parenthesis for tuples, square brackets for lists. Here you can see an example:

lists

Here’s a link to some info I found really useful http://sthurlow.com/python/lesson06/

 

giphy1


Use of recursion for repetitive algorithms

--Originally published at Luis Santana's Blog

Continuing this long marathon of posts, next topic will be use of recursion for repetitive algorithms. 

According to Lepe in 2015, he says (and I completely agree, there’s wisdom in his words). “Recursion is a way of programming in wich you use the same function in a function”. That means it 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.

200
Here’s the link

 

Here’s a good example, actually the best example I could find:

factorial


“For” loops

--Originally published at Luis Santana's Blog

What’s up lads! It’s been a while since my last post but that will change during this day, because I’ll do in a single day all the posts I didn’t finish in this partial hehe so brace yourselves.

 

635656667301311336741304865_resized_winter-is-coming-meme-generator-brace-yourselves-finals-are-coming-45e03b
Here’s the link

 

So, this post is dedicated to “for” loops, this loop is an iterator based for loop. It steps through the items of lists, tuples, strings, the keys of dictionaries and other iterables. The Python for loop starts with the keyword “for” followed by an arbitrary variable name, which will hold the values of the following sequence object, which is stepped through.

Here’s an example of “for” loops:

for

In this example the “for” loops work fine because you know exactly the amount of teams you have and won’t repeat infinitely.

You can check this link with examples, it helped me a lot


Modules and libraries

--Originally published at Luis Santana's Blog

It may happen that you are writing a very extense code and in order to make it easier for you to work and also for others to understand your code then you should use modules and libraries.  You can import random, which is the one I used in my example, to give you a random value of your values you choose at the beggining.

Here you can see an example

modules


Basic Types

--Originally published at Luis Santana's Blog

Something really cool about Python is the use of their types, because it allows you to play with Python (let’s call it like that).

One of the most common and known types are the following:

  • int

With this one you use numbers, positives or negatives.

  • bool

With this boolean data type you can only have two values, true or false

  • float

With this one you can use decimals in your code

 

It is very important that you specify that you are using int for example, because otherwise Python will go nuts and your code will stop. You must validate correctly your types. Check this link for more info https://docs.python.org/2/library/datatypes.html


Calling functions

--Originally published at Luis Santana's Blog

The first steps before ruling the world, if not the first step, is to learn how to create/call functions.

You should write “def” and then whatever name you want it to be. Don’t forget to end with  : ,  this way you are telling Python that you are done with that line.

Then you can continuo with your code and whenever you need to call your function again it will be a lot easier!

calling


Basic user input (text based)

--Originally published at Luis Santana's Blog

There will be times where you may need to know certain information to continue your code, information that only the user can give you. The best way to do this is by using “input”.

With “input” you will display a text that the user should answer in order to continue, the answer he/she gives will remain as a string and with that you can work with the answer.

Here you can see an example, you can also check this link for more infohttp://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html

 

input