【Python Super Primer】Let's Calculate with Programming! A Thorough Guide to Arithmetic Operations #2

Welcome to your Python journey! If you've ever thought programming is all about complex algorithms and cryptic commands, let me share a secret: it can start with something as familiar as doing math! In fact, Python is an incredibly powerful calculator right out of the box.

In this post, we're going to explore how to perform basic calculations using Python. We'll cover addition, subtraction, multiplication, and division, plus a few other handy "arithmetic operators" that will make your programming life easier. Don't worry, it's much simpler than it sounds!

Ready to make Python your new favorite calculator? Let's dive in!


Python's Interactive Mode: Your Friendly Calculator

Remember how we set up Python? One of the coolest ways to start playing with Python is using its "interactive mode." This is like having a direct conversation with Python.

1. Open your Command Prompt (on Windows) or Terminal (on macOS/Linux). 2. Type python (or py on some Windows systems) and press Enter.

You should see something like this (the version numbers might differ):

Python 3.11.4 (main, Jun  7 2023, 10:13:11) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

That >>> is Python's way of saying, "I'm ready for your command!" You can type your calculations directly after it and press Enter to see the result. Let's try it!


The Basic Four: Addition, Subtraction, Multiplication, and Division

These are the bread and butter of most calculations.

1. Addition (+)

To add numbers, use the plus sign (+).

>>> 3 + 5
8
>>> 100 + 25
125

See? Python instantly gives you the answer!

2. Subtraction (-)

For subtraction, use the minus sign (-).

>>> 10 - 4
6
>>> 50 - 15
35

3. Multiplication (*)

To multiply, use the asterisk (*) – it's usually Shift+8 on your keyboard.

>>> 7 * 3
21
>>> 4 * 12
48

4. Division (/)

For division, use the forward slash (/).

>>> 10 / 2
5.0
>>> 7 / 2
3.5
>>> 100 / 4
25.0

Important Note: Did you notice something interesting about the division results? They have a decimal point (like 5.0 or 3.5) even if the result is a whole number. This is because Python's standard division / always gives you what's called a "floating-point number" (a number with a decimal part), which is super precise.


A Quick Look at Numbers: Integers and Floats

In Python (and many programming languages), we generally deal with two main types of numbers:

  • Integers: These are whole numbers, like -3, 0, 5, 100.
  • Floating-point numbers (or "floats"): These are numbers with a decimal point, like -0.5, 3.14, 5.0, 25.0.

As we saw, using / for division will always result in a float. This is usually what you want for accuracy. But sometimes, you might only want the whole number part of a division, or perhaps the remainder. Python has special operators for that too!


More Useful Arithmetic Operators

Let's explore some other handy tools for your Python calculation toolkit.

1. Floor Division (//) - Getting the Whole Number Part

If you want to divide and get only the whole number part, rounding *down* to the nearest whole number, you use "floor division" with two forward slashes (//).

>>> 7 // 2
3
>>> 10 // 3
3
>>> 16 // 5
3

Notice how 7 // 2 gives 3, not 3.5. It just drops the decimal part after dividing.

2. Modulo (%) - Finding the Remainder

The modulo operator (%), often called the "remainder operator," gives you the remainder after a division.

>>> 7 % 2
1

(Because 7 divided by 2 is 3 with a remainder of 1)

>>> 10 % 3
1

(Because 10 divided by 3 is 3 with a remainder of 1)

>>> 12 % 5
2

(Because 12 divided by 5 is 2 with a remainder of 2)

This is super useful for things like checking if a number is even or odd. If number % 2 is 0, the number is even! If it's 1, the number is odd.

>>> 4 % 2  # Even number
0
>>> 5 % 2  # Odd number
1

3. Exponentiation (**) - Powers Of!

To raise a number to the power of another, use two asterisks (**).

>>> 2 ** 3  # This means 2 to the power of 3 (2 * 2 * 2)
8
>>> 5 ** 2  # This means 5 to the power of 2 (5 * 5, or 5 squared)
25
>>> 10 ** 4 # 10 to the power of 4
10000

Order of Operations (Operator Precedence)

Just like in math class, Python follows an order of operations (often remembered by acronyms like PEMDAS/BODMAS: Parentheses/Brackets, Exponents, Multiplication/Division, Addition/Subtraction).

For example:

>>> 2 + 3 * 4

Python will do the multiplication (3 * 4) first, which is 12, and then add 2. So the result is 14.

>>> 2 + 3 * 4
14

If you want to change the order, you can use parentheses (), just like in regular math:

>>> (2 + 3) * 4

Here, Python calculates (2 + 3) first, which is 5, and then multiplies by 4. So the result is 20.

>>> (2 + 3) * 4
20

When in doubt, use parentheses to make your intentions clear! It makes your code easier to read and less prone to errors.


Putting It All Together: A Tiny Example

Let's say you buy 3 apples at $0.50 each and 2 bananas at $0.75 each. What's the total cost?

You can calculate this in Python:

>>> (3 * 0.50) + (2 * 0.75)
3.0

The total cost is $3.00. See how you can combine these operators to solve simple problems?


You're Calculating with Code!

Congratulations! You've just learned how to use Python for basic arithmetic:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Floor Division (//)
  • Modulo/Remainder (%)
  • Exponentiation (**)

You also learned about integers, floats, and how Python handles the order of operations. These are fundamental building blocks in Python and programming in general. The more you practice, the more natural it will become.

Try out some calculations of your own in the Python interactive mode. What happens if you divide by zero? (Spoiler: Python will give you an error, which is a normal part of programming!)

In our next post, we'll explore how to store these numbers and results in "variables," making our programs even more powerful!

Next #3

Post Index


コメント

このブログの人気の投稿

Post Index

【Introduction to Python Standard Library Part 3】The Standard for Data Exchange! Handle JSON Freely with the json Module #13

Your First Step into Python: A Beginner-Friendly Installation Guide for Windows #0