1.5 Number basics

3 min readjune 24, 2024

Python's number basics lay the foundation for mathematical operations in programming. From simple to calculations, understanding these concepts is crucial for effective coding.

Operators, rules, and data type conversions form the core of numerical manipulation in Python. Mastering these skills enables you to tackle a wide range of computational problems with confidence and .

Number Basics in Python

Arithmetic operators in Python

Top images from around the web for Arithmetic operators in Python
Top images from around the web for Arithmetic operators in Python

Addition: Uses the

[+](https://www.fiveableKeyTerm:+)
operator to sum numbers together (3 + 4 evaluates to 7)

  • Subtraction: Uses the
    -
    operator to find the difference between numbers (7 - 2 evaluates to 5)
  • Multiplication: Uses the
    [*](https://www.fiveableKeyTerm:*)
    operator to multiply numbers together (2 * 5 evaluates to 10)
  • Division: Uses the
    [/](https://www.fiveableKeyTerm:/)
    operator to divide numbers and always returns a number (10 / 2 evaluates to 5.0)
  • : Uses the
    //
    operator to perform division, discarding the fractional part and returning the integer quotient (10 // 3 evaluates to 3)
  • Modulo: Uses the
    %
    operator to calculate the remainder of a division operation (10 % 3 evaluates to 1)
  • Exponentiation: Uses the
    **
    operator to raise a number to a power (2 ** 3 evaluates to 8 and 3 ** 4 evaluates to 81)
  • These operators form the basis of arithmetic operations in Python

Operator precedence for expressions

  • Python follows the standard order of operations (PEMDAS):
    1. Parentheses: Evaluates expressions inside parentheses first ((2 + 3) * 4 evaluates to 20)
    2. Exponentiation: Evaluates exponents next (2 ** 3 * 4 evaluates to 32)
    3. Multiplication and Division: Evaluates multiplication and division from left to right (2 + 3 * 4 evaluates to 14)
    4. Addition and Subtraction: Evaluates addition and subtraction from left to right (2 + 3 - 4 evaluates to 1)
  • Use parentheses to override the default order of operations and control the evaluation order ((3 + 4) * (5 - 2) evaluates to 21)
  • Complex expressions with multiple operators are evaluated according to the precedence rules (2 + 3 * 4 - 5 evaluates to 9)

Integer vs floating-point conversions

  • Integers are whole numbers without a fractional component (42, -7, 0)
  • Floating-point numbers have a fractional component (3.14, -2.5, 1.0)
  • Convert an integer to a floating-point number by:
    • Dividing an integer by another number (10 / 3 evaluates to 3.3333333333333335)
    • Using the
      ###[float](https://www.fiveableKeyTerm:Float)()_0###
      function to explicitly convert an integer (float(5) evaluates to 5.0)
  • Convert a floating-point number to an integer by:
    • Using the
      ###[int](https://www.fiveableKeyTerm:int)()_0###
      function to truncate the fractional part (int(3.14) evaluates to 3)
    • Using the
      [round()](https://www.fiveableKeyTerm:round())
      function to round to the nearest integer (round(3.14) evaluates to 3 and round(3.5) evaluates to 4)
  • Mixing integers and floating-point numbers in arithmetic operations results in a floating-point result (3 + 4.0 evaluates to 7.0)
  • Precision in floating-point arithmetic can be affected by limitations

Numeric Data Types and Representation

  • Python supports various for different computational needs
  • like binary, decimal, and hexadecimal are used to represent values
  • is used for very large or small numbers (e.g., 6.022e23)
  • functions help manage precision in calculations

Key Terms to Review (29)

-: The minus sign, or hyphen, is a mathematical symbol used to represent subtraction. It is a fundamental operator in numerical calculations and expressions, allowing for the reduction of one value from another.
*: The asterisk symbol ('*') is a wildcard character used in various programming languages, including Python, to represent any single character or a sequence of characters. It is a powerful tool that allows for pattern matching and flexible searching within data or text.
/: The forward slash, also known as the division symbol, is a mathematical operator used to indicate division in numerical expressions. It represents the act of dividing one number by another, resulting in a quotient.
+: The plus sign (+) is a mathematical operator used to indicate addition, the process of combining two or more numbers or quantities to find their total. It represents the act of adding one value to another, resulting in a sum or total.
Arithmetic: Arithmetic is the branch of mathematics that deals with the basic operations of numbers, including addition, subtraction, multiplication, and division. It is the fundamental building block for more advanced mathematical concepts and is essential for everyday calculations and problem-solving.
Binary Representation: Binary representation is the way in which numbers are expressed using only two digits, 0 and 1, in the digital world. It is the fundamental building block of computer systems and digital electronics, allowing for the storage and manipulation of data.
Code point: A code point is a numerical value that maps to a specific character in the Unicode standard. It allows for the consistent encoding, representation, and handling of text across different systems.
Complex: In the context of number basics, a complex number is a mathematical entity that combines a real number and an imaginary number. It represents a point in the complex plane and is a fundamental concept in various areas of mathematics, including algebra, calculus, and physics.
F-strings: F-strings, also known as formatted string literals, are a powerful feature in Python that allow for easy and efficient string formatting. They provide a concise way to embed expressions directly within string literals, making it simpler to create dynamic and customizable strings.
Float: A float is a data type in programming that represents a decimal number, allowing for the storage of numbers with a fractional component. Floats are used to handle numerical values that require precision beyond what can be represented by integers.
Float(): The float() function in Python is a built-in function that converts a value to a floating-point number, which is a data type that represents real numbers with decimal points. This function is used to handle numerical data that requires decimal precision, such as measurements, calculations, and scientific computations.
Floating-point: Floating-point is a way of representing real numbers in computer systems. It allows for the representation of a wide range of values, from very small to very large, with a fixed amount of memory. This is particularly important in scientific and mathematical computations where precise representation of numbers is crucial.
Floor division: Floor division is an operation that divides two numbers and returns the largest integer less than or equal to the result. In Python, it is performed using the // operator.
Floor Division: Floor division, also known as integer division, is a mathematical operation that divides two numbers and returns the largest integer that is less than or equal to the result of the division. It is denoted by the // operator in programming languages like Python.
Int: The 'int' data type in Python represents whole numbers or integers, which are numbers without fractional parts. It is a fundamental data type that is used extensively in programming to store and manipulate numeric values.
Int(): The 'int()' function in Python is a built-in function that converts a value to an integer data type. It takes a single argument and returns an integer representation of that value, rounding down to the nearest whole number if necessary.
Integer: An integer is a whole number, either positive, negative, or zero, that does not contain any fractional or decimal components. Integers are fundamental to number systems and play a crucial role in various mathematical and programming concepts.
Integer Division Operator (//): The integer division operator, also known as the floor division operator, performs division between two numbers and returns the integer part of the result, discarding any fractional part. It is a fundamental operation in Python's number basics.
Number Systems: Number systems are the way we represent and work with different types of numbers, such as whole numbers, integers, and real numbers. They provide a structured framework for understanding and manipulating numerical quantities in various contexts, including mathematics, computer science, and everyday life.
Numeric Data Types: Numeric data types in programming languages, such as Python, are used to represent and manipulate numerical values. They provide the foundation for mathematical operations and calculations within a program.
Numeric Literals: Numeric literals are the basic building blocks used to represent numerical values in programming languages like Python. They allow developers to directly input numbers into their code, which can then be used in various mathematical operations and computations.
Precedence: Precedence determines the order in which operations are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence.
Precision: Precision refers to the degree of closeness of a measurement or calculation to the true or accepted value. It is a measure of the repeatability or reproducibility of a result, indicating how consistently a method or process can produce the same outcome under the same conditions.
Round-off error: Round-off error occurs when a floating-point number cannot be represented exactly in binary form, resulting in a small discrepancy. This often affects calculations involving decimal numbers.
Round(): The round() function is a built-in Python function that allows you to round a number to a specified number of decimal places. It is a versatile tool that can be used in various contexts, such as number basics, type conversion, and addressing floating-point errors.
Rounding: Rounding is the process of approximating a numerical value to a more convenient or meaningful number, often with fewer digits. It is a fundamental concept in mathematics and is particularly relevant in the context of number basics and dividing integers.
Scientific Notation: Scientific notation is a way of expressing very large or very small numbers in a compact and standardized format. It involves representing a number as the product of a decimal value between 1 and 10, and a power of 10.
Str.format(): The str.format() method is a powerful tool in Python that allows you to insert values into a string in a more flexible and readable way than traditional string concatenation. It provides a way to format strings by replacing placeholders with specified values, making it a versatile and efficient approach for creating dynamic and customized output.
Type Conversion: Type conversion, also known as type casting, is the process of changing the data type of a value or variable in a programming language. It allows programmers to convert one data type to another, enabling them to perform operations and manipulations that may not be possible with the original data type.
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary