This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Operators



Operators work with one or more objects and can perform tasks such as math, comparison, and inspection.

Math

There are standard operators for arithmetic:

Math Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
// Integer Division
% Modulus Division
** Exponent

Additionally, you can modify a named value and assign the output of an operator to the name in one line with inline assignment operators.

	>>> a_number = 1
>>> a_number += 1
>>> a_number
2
>>> a_number *= 8
>>> a_number
16
>>> a_number **= 2
>>> a_number
256
>>> a_number /= 2
>>> a_number
128
>>> a_number %= 3
>>> a_number
2
>>> 3.123 // a_number
1

Assignment

  1. Run Python commands to add, subtract, multiply and divide numbers from the Python interpreter command line.
  2. Create simple Python script program to perform numeric calculations and output results (e.g. calculating the area of a rectangle or circle).

Further Reading

Task Discussion


  • pannix said:

    Calculate the surface area and the volume of a sphere based on a given radius:

     

    http://pastebin.com/EFhE4Vsa

    on May 8, 2012, 11:34 a.m.
  • qiu6406 said:

    on May 5, 2012, 7:26 a.m.
  • davidnamy said:

     

    #Calcular el area de un triangulo
     
    b = input("Base:")
    a = input("Altura:")
     
    area= (b*a) /2
     
    print "El area del triangulo es: " + str(area)
    on April 23, 2012, 9:39 a.m.
  • Asier Iturralde Sarasola said:

    Python script that calculates the area of a rectangle:

    Python script that calculates the area of a circle:

    on April 12, 2012, 1:43 p.m.
  • Nicolas Figueroa said:

    on March 18, 2012, 7:29 p.m.
  • sp_key said:

    Hi all,

    According to http://docs.python.org/library/operator.html operator.add(x, y) is equivalent to the expression x+y however, when I try this I get a 'not defined error'.

    Could you please advice?

    Do I need to declare operator as a variable?

    Thanx

    on Feb. 29, 2012, 7:06 p.m.

    thayne said:

    did you import the operator module?

    and what version of python are you using?

    on March 18, 2012, 11:13 p.m. in reply to sp_key
  • blanco_fam said:

    # Author: blanco_fam
    # Calculate volumen of a sphere

    radius=input("Enter Radius: ")
    pi=3.141592
    # Calculate
    volume = 4/3*pi*float(radius)**3

    print("Volume = ", volume)

    on Feb. 18, 2012, 6:16 a.m.
  • dvdjaco said:

    # #!/usr/bin/python
    #
    # By dvdjaco
    # Exercise 3.2
    #
    # Convert distances in parsecs to yards (you never know...)

    d_parsec = float(raw_input('Please enter the distance in parsecs: '))
    print 'The distance in yards is ' + str(d_parsec*3.374538037e+16)

    on Feb. 14, 2012, 12:39 p.m.
  • pierg75 said:

      #!/usr/bin/python

     
    pi=3.14159265
    rad=int(raw_input('Insert the circle radius: '))
    print('The area of the circle is {}'.format(repr(rad**2*pi)))
    on Feb. 12, 2012, 4:32 p.m.
  • Shannon said:

     

    #filename:streampower.py

    #by Shannon, in Python 3.2
    #calculate stream power potential from flow and head
     
    print ('This program will help you estimate the \nmicrohydro power potential of your small stream.\n')
    flow = float (input ('What is the flow of your stream, in gallons per minute?  '))
    head = float (input ('What is the head (falling height) of your stream, in feet?  '))
     
    power = round (flow * head) / 10
    print ('Your stream can produce about', power, 'watts of power.')

     

    on Feb. 8, 2012, 3:51 p.m.
  • gnuisance said:

    # Author: Gnuisance
    # Program: Operator

    base = float(raw_input('Enter length of triangle base: '))
    height = float(raw_input('Enter length of triangle height: '))
    area = float(0.5 * base * height)
    print 'Area of triangle = ', str(area)
     

    on Jan. 31, 2012, 8:01 a.m.
  • Landon said:

    # Author: Landon Mayo
    # Program: 'Miles Per Hour' to 'Knots' Calculator
    '''
    1 kts = 1.15077945 mph
    '''
    # MPH=miles per hour
    # KTS=Knots
    # Get MPH to be converted to KTS
    
    mph=raw_input("Enter Speed in Miles Per Hour: ")
    knot=float(1.15077945)
    print float(mph)/knot, "Knots Per Hour"
    on Jan. 12, 2012, 2:30 a.m.