Difference between revisions of "Python reference"

Line 47: Line 47:
  
 
round(math.sin(math.radians(30)), 1)
 
round(math.sin(math.radians(30)), 1)
 +
 +
 +
----
  
  
Line 56: Line 59:
  
 
2.0
 
2.0
 +
 +
 +
----
  
  
Line 73: Line 79:
  
 
0.30102999566398114
 
0.30102999566398114
 +
 +
 +
----
 +
  
 
The last thing we will look at in the math module is the concept of e. In order to calculate e in Python, you need to make the call to the function exp(), as follows:
 
The last thing we will look at in the math module is the concept of e. In order to calculate e in Python, you need to make the call to the function exp(), as follows:

Revision as of 07:08, 1 October 2010

This page is intended to serve as a brief, beginners reference to the Python programming language.


Let's figure out the sine of a 30-degree angle in Python!

It should be 1/2 or 0.5.

Just like in trig class, it's sin. So evaluate sin(30) should give us the answer, right?

Actually not. It doesn't seem to know what sin means. The error says that 'sin' is not defined.

Python does know about sin. But it's not part of the system when you start Python up. Why can't Python just load all possible functions at startup?

If we loaded every possible function, we'd overwhelm your computer's memory! There'd be no room for your actual program. So we'll only bring in most functions when we say that we need them.

Functions are grouped into files called modules. For example, all the trigonometric functions are in a module called math.

So to use sin, we need to tell Python to bring the math module into the shell.

Here's the statement we need: import math Note the orange color of import. That shows it is a reserved word.

Now Python knows about this module, and we can use it! Go ahead and figure out sin(30) by calling the name of the module:

OK, to use a function from a module (after importing the module), give the name of the module, then a period (.), then the name and any parameters.

The call for the sin function would be:

math.sin(30) so give it a try. Remember the result should be 0.5.

-0.9880316240928618?? 

This does not give us 0.5. Why is that? The reason is that the sin() function expects to receive its parameter in a unit called radians (and we gave it in degrees). How do we fix this?

There's a function called radians() that takes a value in degrees and converts it to radians. That's exactly what we want.

So how do we use it? This is really cool.

math.sin(math.radians(30))

Remember to read it inside out (just like nested math functions f(g(x))). Take 30, convert it to radians, then take the sine of that angle.

That gets us really close to 0.5 (we discussed why it was off in class last week). How do we push it the rest of the way?

How about round? Can you add that to the call?

round(math.sin(math.radians(30)), 1)




Let's see how we would use the math module to calculate the square root of a number:

import math

math.sqrt(4)

2.0




Let's see how we would use the math module to calculate the log of a number:

import math

math.log(2)

0.6931471805599453

The way the log() function works as shown above is to calculate the natural log of a number. If we wanted to specify the base of the number, you would just add it after the 2 with a comma. For example, if I wanted to calculate the log (base 10) of 2, I would type the following:

import math

math.log(2, 10)

0.30102999566398114




The last thing we will look at in the math module is the concept of e. In order to calculate e in Python, you need to make the call to the function exp(), as follows:

import math

math.exp(2)

7.38905609893065

This function returned e raised to the second power.