Numbers & Arithmetic
Python works with two main number types:
- Integers (
int
) — whole numbers like5
,-12
, or0
; - Floats (
float
) — numbers with decimals like3.14
or-2.5
.
Python determines the type based on how the number is written.
- 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:
- Parentheses;
- Exponentiation;
- Multiplication / Division / Floor Division / Modulo;
- Addition / Subtraction.
Integers vs floats in operations
When performing arithmetic:
- Division always returns a
float
; - Mixing
int
andfloat
promotes the result tofloat
; - Use
//
to get a rounded-down integer result.
Summary
- Python includes support for
int
andfloat
types; - Arithmetic operators:
+
,-
,*
,/
,//
,%
,**
; /
returns floats,//
gives floor-divided integers;- Operator precedence follows PEMDAS.
Try It Yourself
- Write an expression that uses
+
,-
,*
,/
, and parentheses; - Predict the result before running it;
- Run it in Python and compare with your prediction.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 5
Numbers & Arithmetic
Pyyhkäise näyttääksesi valikon
Python works with two main number types:
- Integers (
int
) — whole numbers like5
,-12
, or0
; - Floats (
float
) — numbers with decimals like3.14
or-2.5
.
Python determines the type based on how the number is written.
- 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:
- Parentheses;
- Exponentiation;
- Multiplication / Division / Floor Division / Modulo;
- Addition / Subtraction.
Integers vs floats in operations
When performing arithmetic:
- Division always returns a
float
; - Mixing
int
andfloat
promotes the result tofloat
; - Use
//
to get a rounded-down integer result.
Summary
- Python includes support for
int
andfloat
types; - Arithmetic operators:
+
,-
,*
,/
,//
,%
,**
; /
returns floats,//
gives floor-divided integers;- Operator precedence follows PEMDAS.
Try It Yourself
- Write an expression that uses
+
,-
,*
,/
, and parentheses; - Predict the result before running it;
- Run it in Python and compare with your prediction.
Kiitos palautteestasi!