Introducing the Python Shell.md

By Ryan Wiles

Introduction

The Python Shell implements a REPL (Record-Eval-Print-Loop) Session allowing you to quickly experiment, investigate data structures as well as seeing the output of each statement.

Start the Python Shell

In your terminal, use the following command to start the Python shell.

python3

Next we’ll try a few classic examples to get your feet wet. I’m not going to explain any details about the syntax in the examples yet. We’re going to go into all of those details in a couple of sections.

The Obligatory “Hello World!” Example

print("Hello World!")

Quickly Estimating the Population Growth of Rabbits

Fibonacci was originally investigating how quickly a rabbit population would grow every month. The result of the Fibonacci sequence gives the number of pairs of rabbits at month n. Now, a rational person would note that while it takes into account rabbits being born it doesn’t take into account their expected lifespan. So, for the sake of argument we have to assume that BUNNIES NEVER DIE!

Using a Lambda Expression

fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)

for i in range(10):
  print("fib(" + str(i) + ") = " + str(fib(i)))

Using a Function

def fib(n):
  return n if n < 2 else fib(n-1) + fib(n-2)

for i in range(10):
  print("fib(" + str(i) + ") = " + str(fib(i)))

Exiting the Shell

  quit() or Ctrl-D

© 2018 Ryan Wiles