Functions (Quiz3)

--Originally published at My programming class blog

Functions 

For today´s class we had Quiz 3, which consisted on creating two functions to get the square and cubic roots of any number. This was pretty simple:

First as always, you have to open your editor and save your work as "Quiz3.py".

For this exercise we need to use the math library by using the "import math" function before starting our code.

Now, you have to generate your function, this is pretty simple and I found some help on how to do it in the How to think like a computer scientist interactive book (I´ll leave a link at the end of this post).

  • First you must define the function just like this: def square_root (x): 
  • Then you have to ask your computer to return the value or result of that function:return math.sqrt(x) (FOR THE CUBIC ROOT FUNCTION YOU HAVE TO USE THE "MATH.POW")
After defining your functions, you need to make an input into your code so you can type the number you want to use. x= float(input("Type a number:"))

For this final step you just have to give a variable to the functions r= square_root (x)  so you can ask the code to print the results. print ("The square root of",x, "is", r).

Here´s the complete code:
import math

def square_root (x):
    return math.sqrt(x)
def cubic_root (x):
    return math.pow(x,(1/3))


x= float(input("Type a number:"))
r= square_root (x)
c= cubic_root (x)

print ("The square root of",x, "is", r)
print ("The cubic root of",x, "is", c )

The link to the book: http://interactivepython.org/runestone/static/thinkcspy/GeneralIntro/toctree.html

Fun with Numbers

--Originally published at My programming class blog

FUN WITH NUMBERS

For our first project in python 3 we were asked to create a not so sophisticated code that asks for two numbers and automatically gives the sum, difference, product, division, and modulus operator of both numbers.

I´ll give a small explanation on what I did to make this code work and also I´ll share the sources where I found very useful information.

1.-First you must create a new project on the text editor, name it as you wish and remember to type ".py" at the end of the file´s name.



2.- Now that you have opened your text editor, start by declaring variables and making the code ask the numbers the user wants to use. For this, first give the variable a value which has to have the input function so that it lets you type a number o anything in the computer´s terminal.(you can find some more info on how to use the input function in the interactive book "How to think like a computer scientist", it explains every detail of the function in chapter 2.)

3.- It is important that if you want to use decimal numbers, you must use the float function in the next line of your code, just like this:
x= input ("Write a number")
x=float (x)

4.- Now that you´ve done the same thing for the "y" variable, you must declare a third variable which will be related to the answer to de x+y operation. Also remember that if your´re using decimal numbers and you want an exact result of the sum you must keep using the float function.
n= (x + y)
n= float (n)

5.- The 5th step will be to show the result or in other words, print it on the computer´s terminal. This has to be done with the print function.
Continue reading "Fun with Numbers"

FIRST WORDS IN PYTHON 3

--Originally published at My programming class blog

This post is about how to make your first "script" min python3 and to print it in your computer's terminal. (Only for mac users)


We will start with something easy and very common, the famous "hello world" line, but first you need to prepare your computer for this, which means you have to install the right softwares to make this run.

1.- You need to go to home brew (http://brew.sh) and copy the script line that appears on the main page.

2.- Open your Mac´s terminal and paste the script line which should look like this:


(It will start installing some packages and other stuff in your computer, which will be all necessary)

3.- After it finishes installing everything you should be able to type, so you´ll write "brew install python3".


4.- After it has finished, you'll need to go to your browser and look for atom, which is a text editor that will help you make your code. (https://atom.io)

5.- Now that you have atom installed, open it, go to "File" on the left top corner and choose "new file"

6.- Click on save as and name your file "hello.py", save it in your desktop. (we use ".py" to tell the computer that we´ll be writing in python)


7.- Type print ("Hello world") and save the file.

8.- Go to your terminal and type "cd Desktop", this because you were supposed to save the file in your desktop, and with this command the terminal will go to your desktop and look for any files that you have saved there.

9.- Now that you are in your desktop, you have to type "python3 hello.py" and the terminal will print the hello world phrase.