Light Speed#

No perfect way to start in Python exists. But beginning with its operations on numbers is perhaps as close as it gets.

Take a look at the operators Python uses for numbers, and you’ll find they are almost identical to those you already know in math. Some variations exist because you can only do so much on a regular keyboard and a regular keyboard is the origin of many of the tokens used in programming.

Math Operators in Python#

Operator

Meaning

+

add

-

subtract

*

multiply

/

divide

//

floor division

%

modulo

**

exponentiation

Operator Precedence in Mathematical Expressions#

Orders of operation in mathematical expressions follow the pattern already determined in math itself, i.e., you can use the acronym PEMDAS; and the order is

1st Parentheses

2nd Exponents

3rd Multiplication/Division

4th Addition/Subtraction

That is, if you have several integers, combined with more than one type of operator, you follow the order of operation you find in math. Hence:

10 * 2 - 3  == 17
True

If you run the cell above, the result is True, i.e., Python will first multiply the 10 by 2 and then subtract 3. The double equals sign merely asks for a boolean (True or False) response.

Or, take

3 * 4 ** 2
48

Above the 4 is first squared before that value is multiplied by 3.

But even if you don’t remember the order of precedence, you can simply use parentheses as you might do in math:

(10 * 2) - 3 == 17
True

Speed of Light and a Precursor to Variables#

Let’s say you want to to determine how far light travels in centimeters after one nanosecond based on the following values:

  • the speed of light is 299792458 meters per second

  • one meter is 100 centimeters

  • one nanosecond is one billionth (1/1000000000) of a second

Computers today execute billions of steps (or instructions) every second. The computer processor used to create this page has a number of cores, each of which operates at 3.6 GHz. A GHz, or gigahertz, is a billion cycles per second. That means, one core on this computer processes 3,600,000,000 cycles per second. Each instruction is a small step among the many that constitute the entire process that must run for a program.

By running the code below, you can compute how far light travels in centimeters while this computer completes one cycle:

299792458 * 100 * 1/1000000000 * 1/3.6 
8.327568277777777

That is, maybe half the length of a dollar bill.

A processor has to be small to execute quickly the programs it is fed. In fact, were computer processors any larger than the size of a dollar bill, then you couldn’t even send light from one end of the processor to the other before finishing the execution of a single step — one step of perhaps millions — in a computer program. And speed matters.

Variables#

Which, in a way, brings us to one of the other aspects of time limitations, that which is inherent in the human condition.

Look at that expression above giving us the distance light travels during a cycle.

The programmer cannot be expected to keep track of inputs such these or simply type them in anticipation of some problem. He, being human, does not have the time or the memory space. Moreover, he cannot anticipate all the uses to which it might be put. Hence, variables.

Variable Naming Requirements and Conventions#

Variables are names given to expressions or values. Python has strict rules for variables; not many, but strict:

  • they can contain any letter, integer, or the underscore _

  • these can come in any sequence with one caveat: variables cannot begin with an integer

  • they are case sensitive

  • they cannot contain a space

  • they should never, ever be the same as a keyword or built-in type in Python (more on that later)

By convention, longer variable names are in snake_case.

In the cell below values (on the right) are assigned to variables (on the left) using memorable names using the syntax:

some_good_name = someValue

Let’s say we want to determine the length of a nanostick — the distance light travels in a nanosecond. We might do so this way placing values in variable names:

# speed of light in meters per second
speed_of_light = 299792458 
centimeters_per_meter = 100
# a convenience variable
billionth = 1/1000000000

# a 'nanostick' --- or the distance light travels in a nanosecond
nanostick = speed_of_light * billionth * centimeters_per_meter
print(f"Nanostack = {nanostick} cm")
Nanostack = 29.979245800000005 cm

Let’s go back to the cell above in which we just used literal numbers to calculate the how far light travels in centimeters while our 3.6 GHz processor completes one cycle: 299792458 * 100 * 1/1000000000 * 1/3.6

It makes far more sense to put these values in variables. Thus, and relying on the cell immediately above, we can calculate it thus:

# we need a variable for GHz per second
ghz = 3.6 

light_distance_per_cycle = speed_of_light * centimeters_per_meter * billionth / ghz 

print(f'Light travels {light_distance_per_cycle} centimeters per instruction')
Light travels 8.327568277777777 centimeters per instruction