""" Computes the current in a given circuit and prints the result. Author: Sibel Adali """ def electric_current(voltage,resistance): """ Returns the electric current given voltage and resistance. Assumes that the units are compatible. """ current = float(voltage)/resistance return current ## This is another comment ## Main code starts here. Let's define our variables first. v = 220 # voltage in volts r = 10 # resistance in ohms print v, "volts", r, "ohms" print "results in", electric_current(v,r), "amps of current"
""" Contains functions for converting temperature from fahrenheit to celcius, and from celcius to fahrenheit. Author: Sibel Adali """ def to_celcius(fahrenheit): """ Returns the celcius equivalent of a temperature in fahrenheit. """ return (5.0/9) * (fahrenheit-zero_fahrenheit) def to_fahrenheit(celcius): """ Returns the celcius equivalent of a temperature in fahrenheit. """ return (9.0/5) * celcius + zero_fahrenheit zero_fahrenheit = 32
A collection of Python variables, functions and objects, all stored in a file
Modules allow code to be shared across many different programs.
Before we can use a module, we need to import it:
>>> import module_name
Importing makes the functions in the module available for use.
Python has many, many modules to accomplish many different tasks, ranging from manipulating images to accessing Twitter feeds.
An important example is the math module:
>>> import math
>>> math.sqrt(5)
2.2360679774997898
>>> math.trunc(4.5)
4
>>> math.ceil(4.5)
5.0
>>> math.log(1024,2)
10.0
>>> math.pi
3.1415926535897931
We can get an explanation of what functions and variables are provided in a module using the help function
>>> import math
>>> help(math)
We will work through an number of examples of using the math module in class.
Open Wing IDE, and create a new file.
Cut and paste the temperature conversion code into a file. Save it in a file called temperature.py.
In the python shell, try the following commands:
>>> import temperature
>>> help(temperature)
>>> help(temperature.to_celcius)
>>> temperature.zero_fahrenheit
>>> temperature.to_celcius(-10)
>>> temperature.to_fahrenheit(10)
What is the difference between the math module and our program for temperature conversion?
We can import only a selection of functions and variables:
>>> from math import sqrt,pi
>>> pi
3.141592653589793
>>> sqrt(4)
2.0
Or we can give a new name to the module within our program:
>>> import math as m
>>> m.pi
3.141592653589793
>>> m.sqrt(4)
2.0
Both of these methods helps us distinguish between the function sqrt and the data pi defined in the math module from a function with the same name (if we had one) in our program.
We can also do this (which is NOT recommended!):
>>> from math import *
Now, there is no name difference between the math module functions and ours. It is dangerous, better avoid it.
Type
>>> help(__builtins__)
to see all of the modules Python has built-in. These do not need to be imported.
We can get help on any number of these. We’ll start with int, float and, especially, str.
All variables in Python are objects.
Objects are abstractions:
We know two object types already: int and float. Let us try the following:
>>> radius = 10
>>> help(radius)
You can see now all the oeprations allowed on integers. There are multiple ways to call them.
You can always find out about different types of objects this way. Try the same with a float.
Write a Python module that computes the area of a circle. Your module should use the math module. Remember, the formula is
What happens when we type
import math
math.pi = 3
and then use math.pi?
def entropy(p1,p2): x = float(p1)/(p1+p2) y = -x * math.log(x) - (1-x) * math.log(1-x) return y
import temperature as tempt print 'input: 32 F, expected output: 0 C, returned output:', tempt.to_celcius(32) print 'input: 0 C, expected output: 32 F, returned output:', tempt.to_fahrenheit(0) print 'input: 10 C, expected output: 10 C, returned output:', \ tempt.to_celcius(tempt.to_fahrenheit(10)), 'returned'
Should we change these test cases?
Note that:
the backslash, \, is a way to tell the current statement is continuing in the next line.
expressions. When you have multiple expressions seperated with a comma, print separates them with a space.
Using the math module and the math.sqrt() function, write a module that computes the length of the hypotenuse, given the length of the other edges given by the theorem:
List a number of good test cases for this function. Discuss in class how the function should work for these test cases.
Follow the programming convention of a single comment explaining your program purpose, all your functions and then the main program.
When a piece of code only has variable assignments and function definitions, it can be loaded as a module.
Modules contain a combination of functions, variables, object definitions, and other code, all designed for use in other Python programs and modules
After they are imported, the functions in a module can be executed by a call of the form:
module_name.function_name(arguments)
Comment all your functions to explain their purpose. These can be viewed when you run:
>>> help(module_name.function_name)
Write test code for your functions that prints expected output side by side by the actual output. We will improve this after we learn if statements.
Python has many modules that make it very easy to do many complicated tasks. If you do not believe it, try typing:
>>> import antigravity