Operators and Object Functions

This lesson will teach you how to use operators and functions to alter objects.

Python Operators

    Operators are symbols we use to designate that some sort of computation is expected. The values that operators work with are called operands, and many operands and operators together is called an expression.

    Take the expression x + 1. Here, x and 1 are the operands and + is the operator.

    There are many different types of operators in Python and you will learn most of them in this course.

    In this lesson, we will look at the Arithmetic operators and assignment operators.

Arithmetic Operators

  • Arithmetic operators simply perform mathematical operations.

    Here are the arithmetic operators in Python:

    Arithmetic Operator Description
    + Adds values on either side of the operator
    - Subtracts right hand operand from left hand operand
    * Multiplies values on either side of the operator
    / Divides left hand operand by right hand operand
    % Divides left hand operand by right hand operand and returns the remainder (this is called modulus, or mod)
    ** Performs exponential (power) calculation on operators

    We can also use the + operator to combine strings. This is called concatenation.

    words = "Hi" + " there"
    word1 = "Good"
    word2 = "bye"
    word3 = word1 + word2
    # Output:
    # Hi there
    # Goodbye

More Concatenation

  • Strings and integers cannot simply be added together. Integers need to become a string before they can be added to another string.

    We use python's built in function str() to convert an integer to a string. This conversion process is called casting.

    If you do not cast, you will get an error called a Type Error.

    myAge = 21
    myString = "My age is: "
    sentence = myString + str(myAge)
    # Output:
    # My age is: 21

Assignment Operators

  • Assignment operators perform a mathematical operation and assign the result to the first operand variable.

    Here are the assignment operators in Python:

    Assignment Operator Description
    = Assigns values from right side operands to left side operand
    += It adds right operand to the left operand and assigns the result to left operand
    -= It subtracts right operand from the left operand and assigns the result to left operand
    *= It multiplies right operand with the left operand and assigns the result to left operand
    /= It divides left operand with the right operand and assigns the result to left operand
    %= It takes modulus using two operands and assigns the result to left operand
    **= Performs exponential (power) calculation on operators and assigns the value to the left operand
  • We can add some code to what we wrote previously to employ this. By using the += operator with myAge and years_elapsed as its operands, we can quickly add the two together.

    myAge = 21
    years_elapsed = 20
    myAge += years_elapsed
    myString = "My age is: "
    sentence = myString + str(myAge)
    # Output:
    # My age is: 41

    You might be wondering why we used += here when we could have just said myAge = 41. Truthfully, we could have. But there are cases where assignment operators are necessary.

    For example, you might start a game with score = 0 and have different point values for different goals. By using the += operator with the score and the amount of points gained as your operands (arranged as score += points), you can increase score by any amount easily.
  • Now you try:

    Create a new python file and experiment with arithmetic operators and assignment operators.

    Use concatenation to add strings together and use casting to concatenate strings and numbers.

    Print your results and run your code to see your results in the shell window.

Objects and Functions

  • An Object in python is a collection of various properties. Those properties may be variables or functions.

    How objects contain variables and functions, and how we can access them, will be explained later in the course. For now, we just need to know that objects can be modified by some functions.

    We use these functions by typing the name of the object followed by a period and then the name of the function.

    In this example, HELLO is an object, and the function lower() can be used from that object to make the string lower case.

    upperCaseHello = "HELLO"
    #this is the object we created
    lowerCaseHello = upperCaseHello.lower()
    #we're creating a new object, lowerCaseHello by
    #using the lower() function on upperCaseHello
    #now we print the lowerCaseHello object

Function Parameters

  • Some functions require extra information to work. We call these parameters.

    For example, print() takes something in the parentheses and prints it out.

    Another example, str() takes a number in the parentheses and converts it into a string.

    The object that you write into the parentheses, or pass into them, is called an argument.

    Remember: You pass an argument to a parameter.

    str1 = "Here is a sentence."
    str2 = str1.replace("is", "was")
    # Output
    # Here is a sentence.
    # Here was a sentence.

Challenge: Mad Libs

Now that you know how to use the replace function, you can use it to create a Mad Libs type program. In Mad Libs, you ask a person to put in a type of word, such as a noun, and you have a pre-made text where that noun will be put.

For example, if you were to ask for a Noun, and the response was Chicken, you would take a pre-made sentence...

To get ready for my trip, I put a ____ in my suitcase.

...and fill it with the noun provided...

To get ready for my trip, I put a Chicken in my suitcase.

...to create a crazy new sentence!

Copy the below code into your python file, with text from an example Mad Lib.

mad_libs_text = "Dear RELATIVE1,\nI am having a(n) ADJECTIVE1 time at camp. The counselour is ADJECTIVE2 and the food is ADJECTIVE3. I met PERSON_NAME and we became ADJECTIVE4 friends. Unfortunately, PERSON_NAME is ADJECTIVE5 and I PAST_TENSE_VERB my BODY_PART so we couldn`t go ING_VERB like everybody else. I need more PLURAL_NOUN and a NOUN sharpener, so please ADVERB VERB1 more when you VERB2 back.\nYour RELATIVE2,\nPERSON_IN_ROOM"

mad_libs_text = mad_libs_text.replace("RELATIVE1", "")
mad_libs_text = mad_libs_text.replace("ADJECTIVE1", "")
mad_libs_text = mad_libs_text.replace("ADJECTIVE2", "")
mad_libs_text = mad_libs_text.replace("ADJECTIVE3", "")
mad_libs_text = mad_libs_text.replace("PERSON_NAME", "")
mad_libs_text = mad_libs_text.replace("ADJECTIVE4", "")
mad_libs_text = mad_libs_text.replace("ADJECTIVE5", "")
mad_libs_text = mad_libs_text.replace("PAST_TENSE_VERB", "")
mad_libs_text = mad_libs_text.replace("BODY_PART", "")
mad_libs_text = mad_libs_text.replace("ING_VERB", "")
mad_libs_text = mad_libs_text.replace("PLURAL_NOUN", "")
mad_libs_text = mad_libs_text.replace("NOUN", "")
mad_libs_text = mad_libs_text.replace("ADVERB", "")
mad_libs_text = mad_libs_text.replace("VERB1", "")
mad_libs_text = mad_libs_text.replace("VERB2", "")
mad_libs_text = mad_libs_text.replace("RELATIVE2", "")
mad_libs_text = mad_libs_text.replace("PERSON_IN_ROOM", "")


Run the code, and you will see that there are blanks in the displayed text. This is because the replace function is replacing the words with blanks.

Add a word to each of the replace functions and then run the code again to see what you get!