Python Tutorial #2 (Basic Arithmetic) Basic Arithmetic With Your Favourite Language, Python: *** Intro In this tutorial, I wont cover anything about installing Python, if you are interested in Python, I am sure you have at least done some outside research and have read of or know of a place where you can get an “how to”of Python installation. So now that you have Python and all of its components installed including IDLE ( Integrated DeveLopment Environment) lets get cracking my nuggas. The fundamental of Python’s arithmetic is very basic , but at the same time provides the stepping stone of doing anything (a fundamental). Being able to program does not require you to have an major in math or any kind of degree or be really smart at it. You just need some basic high school math. If you can work the equation y = mx+b you are more than capable of learning Python. If you can not I would suggest taking an introductory algebra class at your local school or community college. *** Addition and Subtraction Okay, we can start with stating that the makes “>>>” are call the Python prompt. Now that you are in IDLE or the Python command line we can start by teach you some of the basic building block, the arithmetic. Go to your python prompt and enter “1 + 1”, if you get anything other than the answer 2 , you have serious problem. But if you do get “2" good job your are on your way already. Now that you have done something simple try do something a little more complex, like adding negative numbers. If you are having trouble trying think of the numbers as integers along a number line like you had to do in 4th grade, you know. The line with Zero whack slack in the middle and the negative numbers to the left and the positive to the right. So you just tried some thing like “-4500+300”, and you understand how you got it. Please if you have any math questions, please feel free to e-mail me. (pierce127@hotmail.com) I love math. Ok now that you have played a little with some basic addition try subtraction, you can check your answers with a hand held calculator or with the windows calculator provided under “Accessories”. To do subtraction you simply use the “-”, “8-2” should give you “6”. Now you are wondering what about subtracting a negative number , well don’t be scared try it, “6- - 2 ” equals “8”. *** Long Numbers Ok now I know you are wondering “what is the largest number I can get, its got to be infinity”, wrong actually the highest computer able value for Python is actually 2,147,483,647 (two billion and some change). I am not going to dive into how this number is derived if you want to know I am sure you can Google it. Now this two billion and some extra number is called the “maxint” and has residence in the “sys module”, if you wanted to use it you would have to “import sys”, but more about that later. Lets try add two really big numbers: “1 + 999999999999999999 ”, wait a second we cant use that big a number its bigger than the “maxint”, and when we try this Python prints out an error message for us. “OverflowError: integer literal too large” so you are wondering how do you handle really large numbers well its so easy that a red butted monkey could do it. By Tagging a “L” on the end of the long integer it tells python “Hey Python, this is a really large number can you do your thing and just help me with it, I want the actual answer not an error.” So now try that previous line again, but this time add a “L”on the end of the integer, and look you got it. The entire value. Do a couple of these and be creative. The “L” basically means large number, and it is very helpful so practice it. *** Multiplication , Division , and Modulo Ok if you can add and subtract , you can multiply and divide you just need to know what the designated signs used by python for them are. ---Multiplication: “ 2 * 7 ” which equals “14”. The “ * ” is the multiplication sign. Try a couple and check them by hand or with an out side calculator. ---Division: “10 / 2 ” equals “5”. “ / ” is the division sign and is the easiest to be confused with other objects. Try some more of these. Something that is commonly overlooked by all types of coders and script writer is that if you want Python to give you a specific answer when using division yo need to tell it... try this : “10 / 4" , well what did you get “2" wow that’s weird, I always though that “10 / 5 “ equaled “2”. Well what you really need to do to get the right answer is add a “.”(period) in the equation somewhere :for example “10 / 4.” and now suddenly you get “2.5 ”. Just remember if you want an exact answer include an period in the equation. ---Now for Modulo, this might sound like some very complex and difficult function but it is actually very simple. The symbol for modulo is “%”. All modulo does is divide the numbers and print the remainder. For example: “ 1900 % 400” gives you a remainder of “300”, if there isn’t a remainder then the remainder will be printed as “0”. So, “12 % 4 ” gives us an answer of “0”. Easy as pi, I mean pie. This particular function is especially good when it come to dealing with some like leap years, when the user needs to know after a calculation if there is a remainder. Here is a quick leap year demo: Try this in IDLE. code: >>> year = 2003(then hit enter) >>>if year % 4 = = 0: (then hit enter) >>>(tab)print “Wow, it’s a leap year”(enter) >>>else:(enter) >>>(tab)print “Its not a leap year”(enter twice) And it should tell you that the year is not a leap year. Later, we will deal with the if and else, and others (control flow statements). *** Rounding, floor( ), and ceil( ) Floor( ) and ceil( ) are very simple processes , the use of floor( ) and ceil( ) is to round the given value to a integer. See the self explanatory example: code: >>> from math import *(don’t worry about the import deal al that is doing is importing the value of pi) >>> pi 3.1415926535897931 >>> int (pi) 3 >>> floor(pi) 3.0 >>> ceil(pi) 4.0 You could also make your own if you aren’t comfortable with the import function: code: >>> a = 4.56456456 (some random value, I pulled out my butt) >>> int(a) ( int( ), just converts the given value back into an integer) 4 >>> floor(a) ( floor( ), takes the value of “a” and lowers it to an integer) 4.0 >>> ceil(a) ( ceil( ), take the value or “a” and raises it to an integer) 5.0 If you don’t understand this just try a couple on your own and you should get the hang of it. And if you still don’t understand, e-mail me and ill give you may famous basketball hoop example. And while you are at it try a couple of these with negative numbers it is very easy. *** Exponential’s An exponential is to raise a number to a power, this is something that was used everyday in high school. For example 22 , is means you take the number that is the base (the big 2) and you multiply by itself twice, 2 * 2 which would equal but now you are saying what this is just multiplication. Well lets try 25,this tell us to multiply 2 by itself 5 times, basically, 2*2*2*2*2 which equals 32. See easy, but now in python we don’t use superscript, we use the “**” marks. Give it a go try a couple, “2**3” you should get “8”. Now when some one shows you “3e6” this is the same as “3**6”. *** Divmod Divmod is a special function and is difficult to explain without confusing the heck out of someone. Divmod is basically modulo and division packed into a special function. , hence the name Divmod (div, mod)or(divide , modulo) Lets try an example: code: >>> divmod(12,3) (4, 0) For the divide part of the function it does divide “12 / 3”, which give the first part of the answer “4”. The it does the modulo “12 %3 ” which is “0”. Then it take both answers and puts it in (4,0). *** Math Rules There are hundreds of complex math rules, out of those hundreds and thousands I will give you two. ---Rule 1: If all you are doing is addition and subtraction forget about parentheses. ---Rule 2: If you are dealing with any other math operands make sure you use parentheses, in everything. *** In Conclusion To cover all the basic operators/functions in Python here they are: Symbol: ............................... Meaning: 1) + .....................................Addition 2) - .....................................Subtraction/Negative 3) * ....................................Multiplication 4) / .....................................Division 5) % .....................................Modulo 6) ** ....................................Exponent 7) divmod(x,y) .......................Function to return both x / y and x % y Next tutorial will cover Variables and Flow Control, so stay tuned