Variables & Numbers

Variables in Python

Symbol Names

Variable names in Python can include alphanumerical characters a-z, A-Z, 0-9, and the special character _. Normal variable names must start with a letter or an underscore. By convention, variable names typically start with a lower-case letter, while Class names start with a capital letter and internal variables start with an underscore.

Reserved Keywords

Python has keywords that cannot be used as variable names. The most common ones you’ll encounter in physics programming are:

if, else, for, while, return, and, or, lambda

Note that lambda is particularly relevant as it could naturally appear in physics code, but since it’s reserved for anonymous functions in Python, it cannot be used as a variable name.

Variable Assignment

The assignment operator in Python is =. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.

Assigning a value to a new variable creates the variable:

Although not explicitly specified, a variable does have a type associated with it (e.g., integer, float, string). The type is derived from the value that was assigned to it. To determine the type of a variable, we can use the type function.

If we assign a new value to a variable, its type can change.

If we try to use a variable that has not yet been defined, we get a NameError error.

Number Types

Python supports various number types, including integers, floating-point numbers, and complex numbers. These are some of the basic building blocks of doing arithmetic in any programming language. We will discuss each of these types in more detail.

Comparison of Number Types

Type Example Description Limits Use Cases
int 42 Whole numbers Unlimited precision (bounded by available memory) Counting, indexing
float 3.14159 Decimal numbers Typically ±1.8e308 with 15-17 digits of precision (64-bit) Scientific calculations, prices
complex 2 + 3j Numbers with real and imaginary parts Same as float for both real and imaginary parts Signal processing, electrical engineering
bool True / False Logical values Only two values: True (1) and False (0) Conditional operations, flags

Examples

Integer Representation: Integers are whole numbers without a decimal point.

Binary, Octal, and Hexadecimal: Integers can be represented in different bases:

Floating Point Representation: Numbers with a decimal point are treated as floating-point values.

Maximum Float Value: Python handles large floats, converting them to infinity if they exceed the maximum representable value.

Complex Number Representation: Complex numbers have a real and an imaginary part.

  • Accessors for Complex Numbers:
    • c.real: Real part of the complex number.
    • c.imag: Imaginary part of the complex number.

Complex Conjugate: Use the .conjugate() method to get the complex conjugate.

Operators

Python provides a variety of operators for performing operations on variables and values. Here we’ll cover the most common operators used in scientific programming.

These operators perform basic mathematical operations:

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 5 / 3 1.6666…
// Floor Division 5 // 3 1
% Modulus (remainder) 5 % 3 2
** Exponentiation 5 ** 3 125

These operators are used to compare values:

Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Used to combine conditional statements:

Operator Description Example
and Returns True if both statements are true x > 0 and x < 10
or Returns True if one of the statements is true x < 0 or x > 10
not Reverses the result, returns False if the result is true not(x > 0 and x < 10)

Python provides shorthand operators for updating variables:

Operator Example Equivalent to
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
//= x //= 3 x = x // 3
%= x %= 3 x = x % 3
**= x **= 3 x = x ** 3

Python follows the standard mathematical order of operations (PEMDAS):

  1. Parentheses
  2. Exponentiation (**)
  3. Multiplication and Division (*, /, //, %)
  4. Addition and Subtraction (+, -)

When operators have the same precedence, they are evaluated from left to right.