Coventry University Logo
4061CEM - Programming and Algorithms 1

Operators

Dr Ian Cornelius

Hello

  • Learning Objectives:
    1. Understand the different operators built-in to Python
    2. Demonstrate the ability to declare variables and using a variety of operators

What is an Operator?

  • An operator is a character that represents an action of some sort
  • They are used for performing operations on variables and values (otherwise known as operands)
  • Python has a collection of operators built-in:
    • Arithmetic
    • Assignment
    • Comparison
    • Logical
    • Identity
    • Membership

Arithmetic and Assignment Operators (1)

  • These operators are used with numeric values to perform mathematical operations

Equals (“=”)

  • The equal assignment operator is used to assign a value (or another variable) to a variable
x = 1
y = 2
z = x

z = 1

Arithmetic and Assignment Operators (2)

Addition (“+ or +=”)

  • When presented with two values or variables will add them together
x = 1 + 2
y = 3
z = x + y
z += x

x = 3

z = 6 (\(1^{st}\) Declaration)

z = 9 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (3)

Subtraction (“- or -=”)

  • When presented with two values or variables will subtract them from one another
x = 1 - 2
y = 3
z = x - y
x -= y

x = -1 (\(1^{st}\) Declaration)

z = -4

x = -4 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (4)

Division (“/ or /=”)

  • When presented with two values or variable will divide them from one another
x = 9 / 3
y = 3
z = x / y
x /= y
z /= x

x = 3.0 (\(1^{st}\) Declaration)

z = 1.0 (\(1^{st}\) Declaration)

x = 1.0 (\(2^{nd}\) Declaration)

z = 1.0 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (5)

Floor Division (“// or //=”)

  • When presented with two values or variables it will divide them from one another and return the integer value
x = 9.5 // 2
y = 5
z = x // y
z //= y

x = 4.0

z = 0.0 (\(1^{st}\) Declaration)

z = 0.0 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (6)

Multiplication (“* or *=”)

  • When presented with two values or variables it will multiply them together
x = 2 * 4
y = 5
z = x * y
x *= y
z *= x

x = 8 (\(1^{st}\) Declaration)

z = 40 (\(1^{st}\) Declaration)

x = 40 (\(2^{nd}\) Declaration)

z = 1600 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (7)

Exponentiation (“** or **=”)

  • When presented with two values or variables it will raise the one value/variable to the power of the other
x = 2 ** 8
y = 5
z = x ** y
z **= x

x = 256

z = 1099511627776 (\(1^{st}\) Declaration)

z = 35249714121083826571348148398002815464391421343966471060391382605731070276854749365048330296473663862456968155395298373973259049475943113619888338673116133666814706870765271907656205646018608369985558721267670321739031938633833281889192620158426531806923144239269726876399951961191980348023291703472305763782410394589758934585631111078120435303032688818751446435291371357171755632775362932694795076313436687469638004327689390246735321855830610856865924913760826763776003265851716557334210642277343475757799780499021559822412434275087084317293455129570406707590002071704673135527533543217355987568107697577946785796412456048360072965616871024866244650081059068183038134518514222987186837394598019859512993600379236190197576838905080733359989094687008999416247722020061992559931401872357379708488585003666965930609730430774107407494018065365845077094320534700692354400169824131578389153656916754682252425562742895026822086112236185768931940433324078692386463642378029291582384550904012284265277124667452816985659337497580991592510201479766500877427834566619156314388107585743546289067551052434075678195345373363919571323210113622615511765134329627207955793605376892875938357672870881305679305521293359975427801921997534891474090868113467357784359783383091085717100807228425031226776985197364359404683041506613943646666199454899363685801848776729685837803228216113833854742443409221480450232563130417709625320794971672737737385983975520047739978165124906916857931960902407397841536657650378758012409157205939513085324282439290108909069036515430690359963152986587749930516880670326145036987607052961696781556418550966201822821857978020062536824015697620957222738065538832187097409859502669196589025961199448758997373792973191723335549772394878874050854532785922475822836403793986623193174020931432381418437022760412682276382989354839625453241289807108260905134234679130954867570447354549760174691007078528452745027994943853229480544512368831378761119681616719327637308142315105120528704683515182038320225078665313911731749364255621284434304945437214609406008640520972029509955435568094888815701470419410889156523971182172814423274140955428070594328381667048286771972857703435525803544707834567774027206614143419982410109261930698311010857874866840743851472857645330929169548403751084494725893729355450473771059986801058342021902735367627900974872368137838996397379898161454825970910732858202781282973937642847973381838672980693399039429342613001595148968082010016061022316242842367672741265405434553107296623559604413326352140529618171175450657884255099334618722731697920185582437182391397673301168160682516639214706566981465961731374808949131742364752993078326367714117001404210930251538132442219335072672096865184691303027156962439777053707286583949764055151291816402546462452719134797179099210233577596277925646031824172274874084562113440043397395191065473620717104250686040896580928700842593919173283844531470952205600874482302488523867074532907781264990865351844684807012208039108287564534854500486391538876063611476665620230294811468351835374072060530215907909311281816131942219776 (\(2^{nd}\) Declaration)

Arithmetic and Assignment Operators (8)

Modulus (“% or %=”)

  • When presented with two values or variables it will return the remainder of a division calculation
x = 2 % 8
y = 5
z = x % y
x %= y
z %= x

x = 2 (\(1^{st}\) Declaration)

z = 2 (\(1^{st}\) Declaration)

x = 2 (\(2^{nd}\) Declaration)

z = 0 (\(2^{nd}\) Declaration)

Comparison Operators (1)

  • These operators are used to compare two values together

Same As (“==”)

  • This operator is used to check if one variable is the same as another
x = 3
y = 3
answer1 = (x == y)
y = 5
answer2 = (x == y)

answer1 = True

answer2 = False

Comparison Operators (2)

Not Equal (“!=”)

  • This operator is used to check if one variable is not the same as another
x = 3
y = 3
answer1 = (x != y)
y = 5
answer2 = (x != y)

answer1 = False

answer2 = True

Comparison Operators (3)

Greater Than (“>”)

  • This operator is used to check if one variable is greater than the other
x = 3
y = 3
answer1 = (x > y)
y = 1
answer2 = (x > y)

answer1 = False

answer2 = True

Comparison Operators (4)

Greater Than or Equal To (“>=”)

  • This operator is used to check is one variable is greater than or equal to the other
x = 3
y = 3
answer1 = (x >= y)
y = 1
answer2 = (x >= y)

answer1 = True

answer2 = True

Comparison Operators (5)

Less Than (“<”)

  • This operator is used to check if one variable is less than the other
x = 3
y = 3
answer1 = (x < y)
y = 5
answer2 = (x < y)

answer1 = False

answer2 = True

Comparison Operators (6)

Less Than or Equal To (“<=”)

  • This operator is used to check is one variable is less than or equal to the other
x = 3
y = 3
answer1 = (x <= y)
y = 5
answer2 = (x <= y)
x = 10
answer3 = (x <= y)

answer1 = True

answer2 = True

answer3 = False

Logical Operators (1)

  • These operators are used to combine comparison operators together

And (“and”)

  • This operator will return True if both comparison operators are evaluated to true
    • Otherwise, it will return false if one of the comparison operators is evaluated to false
x = 6
answer1 = (x > 5 and x < 10)
answer2 = (x > 7 and x < 10)

answer1 = True

answer2 = False

Logical Operators (2)

Or (“or”)

  • This operator will return True if one of the comparison operators are evaluated to true
x = 6
answer1 = (x > 5 or x < 4)

answer1 = True

Logical Operators (3)

Not (“not”)

  • This operator will return the reverse of the evaluated condition
    • If something is True it will return as False and vice-versa
x = 6
answer1 = (not(x > 5))

answer1 = False

Identity Operators (1)

  • These operators are used to compare objects, but not if they are equal
  • They will compare if two objects are the same, with the same memory location

Is (“is”)

  • This operator will return True if both variables are the same object
x = ["4061CEM", "Programming", "Algorithms"]
y = ["4061CEM", "Programming", "Algorithms"]
z = x
answer1 = (x is z)
answer2 = (x is y)
answer3 = (x == z)
print(f"answer1 = {answer1}\n")

answer1 = True

answer2 = False

answer3 = True

Identity Operators (2)

Is Not (“is not”)

  • This operator will return True if both variables are not the same object
x = ["4061CEM", "Programming", "Algorithms"]
y = ["4061CEM", "Programming", "Algorithms"]
z = x
answer1 = (x is not z)
answer2 = (x is not y)
answer3 = (x != z)

answer1 = False

answer2 = True

answer3 = False

Membership Operators (1)

  • These operators are used to test if a sequence exists within an object

In (“in”)

  • This operator will return True if a specified value is in a sequence
x = ["4061CEM", "Programming", "Algorithms"]
answer1 = ("Algorithms" in x)
answer2 = ("4061" in "4061CEM")

answer1 = True

answer2 = True

Membership Operators (2)

Not In (“not in”)

  • This operator will return True if a specified value is not in the sequence
x = ["4061CEM", "Programming", "Algorithms"]
answer1 = ("Ian Cornelius" not in x)
answer2 = ("Algorithms" not in x)
answer3 = ("4063" not in "4061CEM")

answer1 = True

answer2 = False

answer3 = True

Goodbye

  • Questions?
    • Post them in the Community Page on Aula
  • Contact Details: