Minimum and Squares

--Originally published at Programming

Today we are going to implement 2 functions.

To start, we ask the user for three numbers (x, y and z), they are the ones we will use. The first thing we do with our code is import math, then we write the functions.

What will they do?

Minimum: def minimum_three(x, y, z):  returns the value that is smallest of x, y and z.

And squares: def sum_squares(x, y, z):  returns the value of the sum of squares of x, y, z.

After set the function we have to say what we want it returns. In the first case we use min (here the values), and in the second we use ** to raise the numbers to the square and after that + to sum. If you try to do with sum() it doesn’t work (it happens to me), because it only accepts two values and we have 3 now.

captura-de-pantalla-2017-02-02-a-las-10-59-14

Now, let’s work with the main code. We ask for 3 number, we save them like an int.

To do the operations we call our functions (that’s what you can see in a and b, line 10 and 11) and introduce the inputs that the user gave you. Finally, we print the values with a sentence to notice the user what are we doing. This \n is just to skip a line and present the result.

This way it runs, tried with 3, 1 and 6:

captura-de-pantalla-2017-02-02-a-las-11-09-15

?

 


Quiz Week 3

--Originally published at Programming

Here we are going to create a program with two functions:

  • def square_root(x):  // returns the square root of x (float)
  • def cube_root(x): // returns the cube root of x (float)

We should ask the user for a number and then, print the square and cube roots out.

So, here is the code:

captura-de-pantalla-2017-01-30-a-las-20-49-29

First of all, we need to import math that is like call the library to come here (we need it, because it contents the functions). After that we write at the beginnig the functions we want. In this case I didn’t know how to code cube root, but remember that if we raise a number to one third it’s the same, so that’s what I did. We write def and the function. Then, in the next line what do you want it returns.

To continue, we start with our main code. We save what the user will introduce like an int and we are going to call it “x“.What about if the user enters a negative number? Well, I decided to send him a message, but if not, I continue with my programm (that is what we are evaluating in the if-else).

Now, to print the roots, first we have to make the operations, that is what we are doing in the z and y declaration. Finally, we just write a message to announce what we are doing plus the result.

And it’s ready ? It runs like this:

captura-de-pantalla-2017-01-30-a-las-20-51-31


Fun with numbers

--Originally published at Programming

We are going to create a programm that first ask the user for two integer values and then, with these two numbers, calculate the sum, the difference, the product, the division (no decimal point) and the remainder of the division.

Here is the code and how it runs:

captura-de-pantalla-2017-01-21-a-las-20-47-33

Now, let’s explain how it works.

First, we need the two variables as numbers. We have to ask the user for them, so we write a String to display a message. We write the name that we want to give them to identify (for example here, num1 and num2). After that, the symbol = to assign a value and the int() function, because if we forget it, the programm will take it as a String and we couldn’t make math operations with that inputs.

Then, we declarate all the math operations we will perform. The symbols we use are + for sum, – for subtration, * for multiplication, / for division and % for the remainder of the division (the first number between the second). Notice that we have doble // in division, it is because we don’t want decimal points. You could skip this step and just write the operation in the result you are going to print, but I prefer this way.

Finally, we write print() and a String inside quotes, after that a coma and the name of the variable we already done.

And that’s it, now you can run and try your programm. Hope it helps ?

 

If you have doubts about math operations I recommend you this video: Python Programming 1: Maths Operators, and if you want a different explanation of what we did, you could watch this: Python Zero to Hero Lesson 2 variables.

 

 

 


Input and Output

--Originally published at Programming

When you want to introduce data to a programm computer we use an input. Then, this information is processed and you can show the result. This last thing is called output.

A simple example of input is what you type in your keyboard and an example of output is anything you view on your computer monitor.

giphy

Gif from: http://giphy.com/gifs/program-XFEUdfHkPtUxq

 

Now, in python we have something called variables. They are reserved memory locations to store values. We have different kinds of them:

  • Numbers: Data is stored like a numer value.
    • integer: whole numbers
    • float: real numbers with a decimal point
  • String: A series of characters
  • Lists: Values stored, it contains separated items.
  • Tuples: They are like a list, but they cannot be updated.
  • Dictionary: Is like a collection of data type.

You can learn more about it in the following link: https://www.tutorialspoint.com/python/python_variable_types.htm

 

To end this blog, let’s do an example. The user will enter an input and then we are going to show an output.

captura-de-pantalla-2017-01-21-a-las-20-08-04

captura-de-pantalla-2017-01-21-a-las-20-09-18

If you need more help, I could recommend you this video, from Carl Turland: Coding at School: Functions and Variables

 


Setting up Python in Mac

--Originally published at Programming

Well, first of all: What Python is?

Python is a high -level programming, created by Guido van Rossym and first released in 1991. It is an interpretative lenguage too.

Let’s start!

Now, if you own a Mac, we need Homebrew. Open your terminal (you can find this in Launchpad) and copy-paste the link that you will find in this page: brew.sh

captura-de-pantalla-2017-01-21-a-las-13-12-16

Then, key enter to install Homebrew

After the installation, it’s very easy to get python3. Just type “brew install python3” in the terminal and that’s it.

I learned how to do this from this video: Install Homebrew and Python3 Mac OS X. I recommend you to see it ?

We will need an other thing (don’t worry, is the last one). An text editor to create our programms. I will be using Atom, you can download from their official web site: https://atom.io/

captura-de-pantalla-2017-01-21-a-las-13-14-28

To finish this blog, let’s do the usuall output Hello Word. Open your text editor and write your code:

captura-de-pantalla-2017-01-21-a-las-13-17-31

Please save (with the extention .py) and then open your terminal. I will write “cd desktop” because it is where I save it. Type enter and then write “python3″and the name of the file.

And it runs like this:

captura-de-pantalla-2017-01-21-a-las-13-18-45

I hope I has been helpfull ?