Problem Solving With Python: Program 4

The problem statement:

Program 3 introduced an operator that increments the value of a variable by a desired amount, namely, the += operator. Recall that the use of this operator allowed us to replace this expression variable = variable + 1 with the following concise expression variable += 1. Because this operator first computes the sum of the existing variable value with the desired constant value (e.g. 1) and then assigns the sum to the same variable this operator is called a compound assignment operator. This is not the only compound assignment operator available in python that can be used to write concise code.

Given the following Python program, the task is to refactor the code to make it more concise with the use of compound assignment operators where applicable.

Given program:

def given_code():
    variable_1 = 10
    variable_2 = 10
    variable_3 = 10
    variable_4 = 10
    variable_5 = 10
    variable_6 = 10
    variable_7 = 10

    variable_1 = variable_1 + 3
    variable_2 = variable_2 - 3
    variable_3 = variable_3 * 3
    variable_4 = variable_4 ** 3
    variable_5 = variable_5 / 3
    variable_6 = variable_6 // 3
    variable_7 = variable_7 % 3

    print("Before Compound Operation Use:")
    for variable_i in [variable_1, variable_2, variable_3, variable_4, variable_5, variable_6, variable_7]:
        print("    Variable:", variable_i)


The following is the expected approximate output of program 4:

Before Compound Operations:
  • Variable: 13
  • Variable: 7
  • Variable: 30
  • Variable: 3.3333333333333335
  • Variable: 3
  • Variable: 1
After Compound Operations:
  • Variable: 13
  • Variable: 7
  • Variable: 30
  • Variable: 1000
  • Variable: 3.3333333333333335
  • Variable: 3


The Solution:

def refactored_code():
    variable_1 = 10
    variable_2 = 10
    variable_3 = 10
    variable_4 = 10
    variable_5 = 10
    variable_6 = 10
    variable_7 = 10

    variable_1 += 3
    variable_2 -= 3
    variable_3 *= 3
    variable_4 **= 3
    variable_5 /= 3
    variable_6 //= 3
    variable_7 %= 3

    print("After Compound Operation Use:")
    for variable_i in [variable_1, variable_2, variable_3, variable_4, variable_5, variable_6]:
        print("    Variable:", variable_i)


def program_4():
    given_code()
    refactored_code()


if __name__ == '__main__':
    program_4()


Arithmetic Operators:

  • + is used for addition.
  • - is used for subtraction.
  • * is used for multiplication.
  • ** is used for exponentiation. E.g.,  2**5 = 32 is read as 2 raised to the power of 5 or 2*2*2*2*2 = 32.
  • / is used for division. E.g., 1/2 = 0.5
  • // is used for floor division. E.g., if 8/3 = 2.666... then 8//3 = 2. I.e., the decimal point (.) and everything to the right of it is dropped.
  • % is used for computing the remainder of a division operation. E.g., 8%3 = 2.


Page with links to the entire series can be found here

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.

No comments:

Post a Comment