Skip to main content
Back

Programming with Numbers in Python: Study Notes

Study Guide - Smart Notes

Tailored notes based on your materials, expanded with key definitions, examples, and context.

Programming with Numbers in Python

Introduction

Numbers and character strings are fundamental data types in Python. They serve as the basic building blocks for constructing more complex data structures and programs. This section introduces how to work with numbers in Python, including writing simple programs that utilize numeric data.

Numbers in Python

Numeric Literals

In programming, numbers are referred to as numeric literals. Python supports two main types of numbers:

  • int: Whole numbers without a decimal point (e.g., 6, -6, 0).

  • float: Numbers with a decimal point or in exponential notation (e.g., 0.5, 1E6, 2.96E-2).

Table: Number Literals in Python

Number

Type

Comment

6

int

An integer has no fractional part.

-6

int

Integers can be negative.

0

int

Zero is an integer.

0.5

float

A number with a fractional part has type float.

1.0

float

An integer with a fractional part .0 has type float.

1E6

float

A number in exponential notation: or 1000000. Numbers in exponential notation always have type float.

2.96E-2

float

Negative exponent:

100,000

Error

Do not use a comma as a decimal separator.

3 1/2

Error

Do not use fractions; use decimal notation: 3.5.

Arithmetic Operators

Python supports standard arithmetic operations:

  • Addition (+)

  • Subtraction (-)

  • Multiplication (*)

  • Division (/)

  • Exponentiation (**)

The result of a division is always a float. For other operations, the result is a float if either operand is a float; otherwise, it is an int.

Table: Arithmetic Operators in Python

Mathematical Notation

Meaning

Python Notation

a × b

a times b

a * b

an

a to the nth power

a ** n

The print Function

The print function is used to display numbers and results on the monitor. It can display the value of variables or the result of evaluated expressions. Multiple values can be displayed in a single print statement.

  • Example:

print(3 + 2, 3 - 2, 3 * 2) print(8 / 2, 8 ** 2, 2 * (3 + 4)) # Output: 5 1 6 4.0 64 14

Variables in Python

Definition and Assignment

A variable is a named storage location in a computer program. Variables are defined by assigning them a name and an initial value using an assignment statement.

  • To define a variable: variable_name = value

  • Assignment statements evaluate the expression on the right and assign the result to the variable on the left.

Example: Calculating distance traveled:

speed = 50 timeElapsed = 14 distance = speed * timeElapsed print(distance) # Output: 700

Variable Naming Rules

  • Must begin with a letter or underscore (_).

  • Can only contain letters, numbers, and underscores.

  • Case-sensitive: amount and Amount are different variables.

  • Should use descriptive names (e.g., rateOfChange, timeElapsed).

  • Cannot use reserved words (keywords) as variable names.

Reserved Words (Keywords)

Python has 33 reserved words that cannot be used as variable names. These words have special meanings in the language (e.g., if, else, for, while).

Constants

A constant is a variable whose value should not change after its initial assignment. By convention, constants are written in all uppercase letters (e.g., BOTTLE_VOLUME = 2.0).

  • Using named constants makes code more readable and maintainable.

Comments

Comments are used to document code and explain its purpose. They are ignored by the Python interpreter and are written using the # symbol.

  • Example: # This is a comment

Integer Division and Modulus Operators

Integer Division (//) and Modulus (%)

Python provides two additional operators for integer arithmetic:

  • Integer division (//): Returns the integer quotient of a division.

  • Modulus (%): Returns the remainder of a division.

Table: Integer Division and Modulus Examples

Expression

Value

19 // 5

3

19 % 5

4

0 % 2

0

5 % 7

5

Example: Converting inches to feet and inches:

totalInches = 41 feet = totalInches // 12 inches = totalInches % 12 print(feet, inches) # Output: 3 5

Built-in Functions for Numbers

Common Built-in Functions

  • abs(x): Returns the absolute value of x.

  • int(x): Converts x to an integer by truncating the decimal part.

  • round(x, n): Rounds x to n decimal places (if n is omitted, rounds to the nearest integer).

Table: Built-in Function Examples

Expression

Value

abs(-3)

3

int(2.7)

2

int(-2.7)

-2

round(2.7)

3

round(2.317, 2)

2.32

round(2.317, 1)

2.3

Note: Function names are case-sensitive (e.g., round not Round).

Order of Operations and Parentheses

Python follows standard mathematical order of operations (precedence):

  1. Parentheses (innermost first)

  2. Exponentiation (**)

  3. Multiplication (*), Division (/ and //), Modulus (%)

  4. Addition (+), Subtraction (-)

Use parentheses to clarify complex expressions and ensure correct evaluation order.

Errors in Python

Syntax Errors

Syntax errors occur when Python cannot understand the code due to incorrect structure or punctuation. These are detected before the program runs.

  • Examples: Extra parenthesis, using reserved words as variable names, using semicolons instead of commas.

Runtime Errors

Runtime errors occur during program execution, such as dividing by zero or using an undefined variable.

  • Example: result = 5 / 0 (division by zero)

Logic (Semantic) Errors

Logic errors occur when a program runs but produces incorrect results due to a mistake in the algorithm or formula.

  • Example: average = firstNum + secondNum / 2 (should be average = (firstNum + secondNum) / 2)

Variables and Memory

When a variable is assigned a new value, Python creates a new memory location for the new value. The old value may be abandoned if no variable refers to it.

Augmented Assignment Operators

Python provides augmented assignment operators to simplify updating variable values:

  • +=: Increment by a value (x += 1 is equivalent to x = x + 1).

  • -=: Decrement by a value.

  • *=: Multiply by a value.

  • /=: Divide by a value.

Example:

num = 6 num += 1 # num is now 7 num -= 2 # num is now 5 num *= 3 # num is now 15 num /= 5 # num is now 3.0

Additional Notes

  • Variable names are sometimes called identifiers.

  • Numeric expressions can include literals, variables, and operators.

  • Numeric literals should not contain commas, dollar signs, or percent signs.

  • Python rounds numbers that are exactly halfway between two integers to the nearest even number (e.g., round(2.5) is 2, round(3.5) is 4).

  • Scientific notation in Python uses the form bEr, where b is a number and r is an integer exponent.

  • Built-in functions are part of the Python language; user-defined functions can be created as needed.

Pearson Logo

Study Prep