Python Programming: Math

11 Jan

Follow my 11-year-old adventures with my Raspberry Pi

Today, I read a chapter from “Hello World: Computer Programming for Kids and Other Beginners” about variables and math in Python.  I learned about strings, floats, and integers and that all made sense.  I learned that ‘word’ and “word” mean the same thing, but ‘word” isn’t allowed.

One thing that I didn’t understand is that 3/2 comes out to 1.  I understand that it rounded it off.  But why didn’t it round up to 2?  By the way, to get the result of 1.5, you just have to type “3.0/2”.  At first, I thought that float(3/2), but that just put 1.0.  I don’t understand that either.

I also learned that Python uses the PEMDAS rules that I learned in school.  It does multiplication and division before doing addition and subtraction in an equation.  Like if you have “2+2*2” that will come out to be 6 because it does the 2*2 part first. If I want to do the 2 + 2 first, then I have to type “(2 + 2)*2.

I also learned that symbol for “to the power of” is **.  So, 2 to the 10th power would be written as “2**10”.  Also, if you type “5 % 2” it gives you the remainder of the division problem.  In this case,  “7 % 2” would be 1.  This could be helpful if we needed to see if a number is even or odd.

I learned that Python is going to get rid of the input command and that I should use the int function with raw_input() instead.

I just got to the part where I’m learning how to input text from a website and it was complicated and I’m tired, so I’ll do it some other time.

My dad asked me to come up with a cool project to do with the raspbpi.  What do you suggest?  He thought I should make it light something up or make something move.  But, I like programs that do things inside of the computer.

Advertisements

3 Responses to “Python Programming: Math”

  1. Stephane Quenson January 16, 2013 at 10:00 pm #

    In python 2.7, when you divide one integer by another one, it returns the largest integer which is lower than or equal to the quotient. For example:
    5/4 –> 1.25 –> the integer division returns 1
    199/100 –> 1.99 –> the integer division returns 1
    6/3 –> 2 –> the integer division returns 1
    A result which looks strange:
    (-5)/4 –> -1.25 –> the integer division returns -2
    So, -(5/4) is not equal to (-5)/4 !

    Now the float question. What is happening when you type float(3/2)? Python will first calculate what is in the parenthesis, 3/2 which returns 1, and then it will apply the float transformation on 1, which returns 1.0.

    So, let’s say you have defined variables a and b with the code:
    a = 7
    b = 2
    and you want the exact result of a divided by b?
    If you type:
    a/b
    It returns 3, because both a and b are integer. How to force the division to be exact?
    If you type:
    float(a/b) what do you get?
    Any idea? Answer next time!

    • krystal92586 January 20, 2013 at 6:33 pm #

      Hmm, I swear I tried float(a/b) and float(a)/float(b) and neither of them worked. I just tried both of them in Idle on my laptop and they worked (3.5). When I get back home to my RPi, I’ll have to try it again there. Thanks for the tips, this is much easier.

      • Stephane Quenson January 20, 2013 at 8:59 pm #

        It all depends on the version of python you are using:
        – In python 2.7 (you launch it on the Pi with python), you get a/b = 3 and float(a)/float(b) = 3.5
        – In python 3.2.3 (you launch it with python3), you get a/b = 3.5
        There are a few differences between the two versions, the most visible ones being the integer division, the raw_input statement and the print statement. There is a converter callde 2to3 (http://docs.python.org/2/library/2to3.html) to help in the code conversion.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: