Adding in Python

From OpenCircuits
Revision as of 09:18, 6 January 2017 by Russ hensel (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Adding in Python is more interesting than you might think because it is not limited to adding numbers. Lots of things, but not all things can be added. Lets try this simple with example code in the same style as: Python Example Code

Adding Some Different Things

Copy the code and run. The explanations I think you need are in the code as comments.


""""
ex_adding.py   examples of adding in Python

"""

# ----------------------------------------
def   ex_1():
    print """
    ================ ex_1(): Add some numbers ===============
    """
    # we will put our numbers in variables
    a    = 3
    b    = 7.
    print "a + b =", a + b

#ex_1()

# ----------------------------------------
def   ex_2():
    print """
    ================ ex_2(): You can add some strings ===============
    """
    a          = "Hello"
    a_space    = " "
    b          = "Universe"
    print "a + a_space + b =", a + a_space + b

#ex_2()

# ----------------------------------------
def   ex_3():
    print """
    ================ ex_3(): You can add some lists ===============
    """
    a          = [1, 2, 3, 99.,]

    b          = [ "Hello ", "Universe", "World", "You" ]
    print "a + b =", a + b

ex_3()

But Not Everything Works

Put some failing examples here.....

More

What can be done with adding also works to some extent with other operations like multiplication. It also may work with some functions. Understanding this can simplify your approach to programming. If you want to know more look into "operator overloading" and........