PYTHON
  • PYTHON v3
  • Python IDE Setup
  • Python Programming
    • Python for ABS
      • Resources
      • Ch1 - Getting Started
      • Ch2 - Types, Variables and Simple I/O
  • Python For Network Engineers-Kirk Beyers
    • Resources
    • Python Fundamentals
  • Python Inststitute (PCAP)
    • Resources
    • Course Introduction
    • Python Essentials (Part 1)
      • Module 1
        • Fundamentals
        • What is Python
        • Starting your work with Python
      • Module 2
        • The Print Function
          • Section Summary
        • Literals
          • Section Summary
        • Operations- Data Manipulation Tools
          • Section Summary
        • Variables
          • Leaving Comments in Code
          • The input () Function
  • 100 Days Of Code
    • Resources
    • What You Have To Do !!
    • 100 DAY's
      • DAY 1: Working with Variables
      • Day 2: Data Types & String Manipulation
      • Day 3: Control Flow and Operators
      • Day 4: Randomisation and Lists
      • Day 5: For Loops, Range & Code Blocks
      • Day 6: Python Functions and Karel
      • Day 7: Hangman
      • SUMMARY
  • {Pirple}
    • Resources
Powered by GitBook
On this page
  • Key Takeaways
  • Exercise 1
  • Exercise 2
  • Exercise 3

Was this helpful?

  1. Python Inststitute (PCAP)
  2. Python Essentials (Part 1)
  3. Module 2
  4. Operations- Data Manipulation Tools

Section Summary

Key Takeaways

  • An EXPRESSION is a combination of values (or variables, operators, calls to functions) which evaluates to a value eg ( 1 + 2 )

  • Operators are special symbols or keywords which are able to operate on the values and perform (mathematical) operations eg the * operator multiplies 2 values x * y

  • Arithmetic operators (+, -, *, /) returns a float if ONE of the values is of the float type

    • % (modulus - divides left operand by right operand and returns the remainder of the operation eg. 5 % 2 = 1

    • ** Exponentiation - left operand raised to the power or right operand eg. 2 * *3 = 8

    • / / Floor/integer division - returns a number resulting from division, but rounded DOWN to the nearest whole number eg 3 // 2.0 = 1.0

  • A unary operator is an operator with only 1 operand eg -1 or +3

  • A binary operator is an operator with 2 operands eg. 4 + 5, or 12 % 5

  • Some operators act before others - the hierarchy of priorities

    • unary + and - have the highest priority

    • then **, then * , / , and % and then the lowest priority: binary + and -

  • Sub-expressions in parenthesis are always calculated first eg 15 - 1 * (5 * (1 + 2)) = 0

  • The exponentiation operator uses right-sided binding eg 2 ** 2 ** 3 = 256

Exercise 1

What is the output of the following snippet? print((2 ** 4), (2 * 4.), (2 * 4))

16 8.0 8

Exercise 2

What is the output of the following snippet? print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))

-0.5 0.5 0 -1

Exercise 3

What is the output of the following snippet? print((2 % -4), (2 % 4), (2 ** 3 ** 2))

2 // -4 = -1 --------> Remember floor division -ive so lowest rounding off!! -1 * -4 = 4 2 - (4) = -2 ----- 2 512

PreviousOperations- Data Manipulation ToolsNextVariables

Last updated 4 years ago

Was this helpful?