While loops

--Originally published at Programming Fundaments

The “while” statement that repeatedly executes a certain statement as long as a given statement is true

while expression:
   statement(s)

The while loop, is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Andddd you can create and infinite loop by creating a condition that is never “false”. This is often used by client/server programming, because it needs to run always. Example:

var = 1
while var == 1 :  # This constructs an infinite loop
   num = raw_input("Enter a number  :")
   print "You entered: ", num

Nesting of conditional statements

--Originally published at Programming Fundaments

A nested construct is one where you have a “if, elif, else” statement inside an “if, elif, else”. It just a combination of the last couple of blogs, but a little more complex. Example

#!/usr/bin/python

var = 100
if var < 200:
   print "Expression value is less than 200"
   if var == 150:
      print "Which is 150"
   elif var == 100:
      print "Which is 100"
   elif var == 50:
      print "Which is 50"
elif var < 50:
   print "Expression value is less than 50"
else:
   print "Could not find true expression"

print "Good bye!"

And it should result in something like this:

Expression value is less than 200
Which is 100
Good bye!

Not so complex really….

Examples and more from: https://www.tutorialspoint.com/python/nested_if_statements_in_python.htm


Else, Elif

--Originally published at Programming Fundaments

An “else” conditional is dependent of an “if” statment, it usually executes if the “if” statement goes to 0 or is false.

#!/usr/bin/python

var1 = 100
if var1:
   print "1 - Got a true expression value"
   print var1
else:
   print "1 - Got a false expression value"
   print var1

var2 = 0
if var2:
   print "2 - Got a true expression value"
   print var2
else:
   print "2 - Got a false expression value"
   print var2

print "Good bye!"

Resulting in this:

1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!

The “elif” conditional allows you to check multiple times for a “true” statement inside your block of code and execute one as soon as one turn “true” Example:

var = 100
if var == 200:
   print "1 - Got a true expression value"
   print var
elif var == 150:
   print "2 - Got a true expression value"
   print var
elif var == 100:
   print "3 - Got a true expression value"
   print var
else:
   print "4 - Got a false expression value"
   print var

print "Good bye!"

Examples and more from: https://www.tutorialspoint.com/python/python_if_else.htm


If conditional

--Originally published at Programming Fundaments

In python 3 you have to use conditionals to tell the program to make a desition. The fisrt conditional we are gonig to useis “if” example:

If it rains tomorrow, I will do the following:
    - tidy up the cellar 
    - paint the walls
    - If there is some time left, I will 
          - do my tax declaration
Otherwise, I will do the following:
    - go swimming
go to the cinema with my wife in the evening

as simple as that, basically you are making the program to make a desicion depeding of the variable and parameters you input:

if (raining_tomorrow) {
    tidy_up_the_cellar(); 
    paint_the_walls();
    if (time_left) 
          do_taxes();
}

Examples and more from: http://www.python-course.eu/python3_conditional_statements.php


“El IQ no lo es todo”

--Originally published at Programming Fundaments

So… I know this isn´t a regular programming blog but whatever…

Over the last weeks there has been a lot of controversy within the Tecnologico de Monterrey (my college). A few weeks ago my college released the next publicity

iq

The traduction is “The IQ isn’t everything”. Obviously, many people, mostly the ones in “artistic careers”, were veryyyyyyyy angry.

It’s a tough hit towards some career within the institution, it must feel awful that your own college doesn’t take your career in a serious tone.

I don’t want to make a lot from this, probably because it wasn’t directed at my career, but my point is: Think before you make a stupid mistake like this one


Importing and using modules/ libraries

--Originally published at Programming Fundaments

To use a moodule is necesary to have a text editor creating your cade in the text editor, creating a script, and the files create a module, this can be exported to the python program using the command import(). When ypu have your strings saved , it must be saved with .py at the end the you can use the next command

>>> import example_function

As this does not import the text of the function but the function itsel creatin a whole function with only a couple words in the python interpreter

For more information an examples check out: https://docs.python.org/3/tutorial/modules.html


Creating Functions

--Originally published at Programming Fundaments

To create a function  after you call it, you must set the parameters list, or the arguments, and after you call the function you must use the command return(). For a better explanation here is an example:

def fahrenheit(T_in_celsius):
    """ returns the temperature in degrees Fahrenheit """
    return (T_in_celsius * 9 / 5) + 32

for t in (22.6, 25.8, 27.3, 29.8):
    print(t, ": ", fahrenheit(t))

In this function you are defining the parameters list as T_in_celsius, and defining  the t as a list of numbers, which are you celsius numbers, ands it will be converted in fahrenheit, and you will use the return comand to make it repeat the steps with all of the umbers of the list, as you can observe in the next part:

22.6 :  72.68
25.8 :  78.44
27.3 :  81.14
29.8 :  85.64

And thats how you create a function, as you can see its knda difficult in the beginning but after a couple trials and errors you can master this in no time

More information and examples at: http://www.python-course.eu/python3_functions.php


Calling functions

--Originally published at Programming Fundaments

A function, in programming, is used to implement mathematical functions (duh), they are known as subroutines, routines, procedures, methods, etc. A function in Python is defined by a def statement an the normal syntax is wroten as this:

def function-name(Parameter list):
    statements, i.e. the function body

That usually how you write a function in the next blog we are going to go over the function througly

Examples and more information at: http://www.python-course.eu/python3_functions.php


Basic user input

--Originally published at Programming Fundaments

In this post we are going to talk about the function input(). This funtion is basically used to create strings. With this you can create a casting or oval funtion just like this:

name = input("Frank")
print("Nice to meet you " + name + "!")
age = input("42")
print("So, you are are already " + age + " years old, " + name + "!")

And when you run it it should run like this.

$ python input_test.py 
What's your name? "Frank"
Nice to meet you Frank!
Your age? 42
So, you are are already 42 years old, Frank!

For more information check the link down below

Examples from: http://www.python-course.eu/python3_input.php

 

Examples