Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Numbers & Arithmetic | Basic Syntax
Introduction to Python with AI

bookNumbers & Arithmetic

Python works with two main number types:

  • Integers (int) β€” whole numbers like 5, -12, or 0;
  • Floats (float) β€” numbers with decimals like 3.14 or -2.5.

Python determines the type based on how the number is written.

Note
Example Prompts
  • Show how each arithmetic operation works in Python.
  • What is the order of operations in Python? Explain in code how precedence works.
  • Show in code how does Python handle int vs float in arithmetic operations?

Arithmetic Operations in Python

Python includes all standard math operations with clear syntax:

Addition

+ adds values. Mixing int and float produces a float.

Subtraction

- subtracts the right-hand value from the left. Works with positives and negatives.

Multiplication

* multiplies values. If a float is involved, the result is a float.

Division

/ divides and always returns a float, even if the result is whole (e.g., 8 / 2).

Floor Division

// divides and rounds down toward negative infinity. Result type depends on operands.

Modulo

% gives the remainder of a division. Works with positive, negative, and floats.

Exponentiation

** raises a number to a power. Also works with fractional exponents for roots.

Operator precedence (PEMDAS)

Python follows standard rules to decide which operation comes first:

  1. Parentheses;
  2. Exponentiation;
  3. Multiplication / Division / Floor Division / Modulo;
  4. Addition / Subtraction.

Integers vs floats in operations

When performing arithmetic:

  • Division always returns a float;
  • Mixing int and float promotes the result to float;
  • Use // to get a rounded-down integer result.

Summary

  • Python includes support for int and float types;
  • Arithmetic operators: +, -, *, /, //, %, **;
  • / returns floats, // gives floor-divided integers;
  • Operator precedence follows PEMDAS.

Try It Yourself

  1. Write an expression that uses +, -, *, /, and parentheses;
  2. Predict the result before running it;
  3. Run it in Python and compare with your prediction.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookNumbers & Arithmetic

Swipe to show menu

Python works with two main number types:

  • Integers (int) β€” whole numbers like 5, -12, or 0;
  • Floats (float) β€” numbers with decimals like 3.14 or -2.5.

Python determines the type based on how the number is written.

Note
Example Prompts
  • Show how each arithmetic operation works in Python.
  • What is the order of operations in Python? Explain in code how precedence works.
  • Show in code how does Python handle int vs float in arithmetic operations?

Arithmetic Operations in Python

Python includes all standard math operations with clear syntax:

Addition

+ adds values. Mixing int and float produces a float.

Subtraction

- subtracts the right-hand value from the left. Works with positives and negatives.

Multiplication

* multiplies values. If a float is involved, the result is a float.

Division

/ divides and always returns a float, even if the result is whole (e.g., 8 / 2).

Floor Division

// divides and rounds down toward negative infinity. Result type depends on operands.

Modulo

% gives the remainder of a division. Works with positive, negative, and floats.

Exponentiation

** raises a number to a power. Also works with fractional exponents for roots.

Operator precedence (PEMDAS)

Python follows standard rules to decide which operation comes first:

  1. Parentheses;
  2. Exponentiation;
  3. Multiplication / Division / Floor Division / Modulo;
  4. Addition / Subtraction.

Integers vs floats in operations

When performing arithmetic:

  • Division always returns a float;
  • Mixing int and float promotes the result to float;
  • Use // to get a rounded-down integer result.

Summary

  • Python includes support for int and float types;
  • Arithmetic operators: +, -, *, /, //, %, **;
  • / returns floats, // gives floor-divided integers;
  • Operator precedence follows PEMDAS.

Try It Yourself

  1. Write an expression that uses +, -, *, /, and parentheses;
  2. Predict the result before running it;
  3. Run it in Python and compare with your prediction.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
some-alt