Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Examples
  • Now you try it!
  1. Language Basics

Lesson 6. Arithmetic Operators

The arithmetic operators in Python should be just as familiar to you as they were in grade school. However, there are three unusual operators that we need to talk about real quick.

The first strange operator is integer division which is performed by a double forward slash between the numerator and denominator. Integer division rounds down to the nearest integer value.

Modulo, which is performed by the percent sign between the numerator and denominator, returns the remainder value from a division operation.

Exponent you should be familiar with. The only odd thing about it is exponent operations are usually performed by ^ in other languages. In this case, it is performed with two asterisks.

Examples

Basic Math Operations

I want to point out two things. The first is the print statements.

Do you see how each variable is preceded by str? That is because those variables are either integers or floats. If we want to smoosh a number together with a string, we have to cast the number as a string and that is what the str method does.

The second thing is the value of the division operation. If you are sharp you will see that we divided two integer values. Despite the fact that the result was an integer, the data type of division resulted in a float.

x = 10
y = 5

addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
integer_division = x // y  
modulo = x % y
exponent = x ** y

print('The result of addition was ' + str(addition))
print('The result of subtraction was ' + str(subtraction))
print('The result of multiplication was ' + str(multiplication))
print('The result of division was ' + str(division))
print('The result of integer_division was ' + str(integer_division))
print('The result of modulo was ' + str(modulo))
print('The result of exponent was ' + str(exponent)

Now you try it!

Don't copy and past. Type the code yourself!

PreviousLesson 5. String ConcatenationNextLesson 7. Making Decisions

Last updated 3 years ago