Introduction - Python Bootcamp

Constantine Lignos

Contents

  1. Why use Python?
  2. Some challenges in dealing with Python
  3. How Python works
  4. Some basics
    1. Literals
    2. Assignment
    3. Handy built-in functions

Why use Python?

  1. Python is powerful and easy.
    xkcd comic on Python
  2. Python comes with “batteries included.” There are lots of useful and powerful packages in the standard library.
  3. Programs are short, clear, and concise
  4. Whitespace is required, preventing hopelessly unreadable code
  5. There’s one obvious way to do it

Some challenges in dealing with Python

  1. The compiler will not stop you from writing nonsense sense but is actually wrong
  2. Some errors won’t show up unless the actual line of code is run, meaning that you have no idea whether anything works until you test it.

How Python works

  1. Each line of code is parsed into its syntax
  2. The syntax tree is turned into byte code which is executed by the interpreter
  3. Only syntax errors are caught in advance; runtime errors are reported as they happen. For example, the compiler will tell you if you’ve forgotten a colon, but it won’t tell if you’re calling something on a number that requires a string.

Some basics

  1. Start out by running python or an IDE such as IDLE or Spyder to try out typing things in the interpreter.
  2. Typing directly into the interpreter is called a REPL: Read Eval Print Loop. It reads your input line-by-line and prints results.

Literals

The simplest thing you can type is an expression. The simplest expressions are just literals, which evaluate to themselves. Note that Python uses single and double-quotes interchangeably, and doesn’t distinguish between characters and strings like many other languages.

>>> 7
7
>>> 7.5
7.5
>>> "H"
'H'
>>> "Hello world!"
'Hello world!'

Assignment

You don’t have to do much work to assign values to variables. What are called “variables” in most programming languages are technically called “names” in Python. We’ll worry about what that means later.

>>> x = 7
>>> x
7
>>> x = 7.5
>>> x
7.5
>>> x = "H"
>>> x
'H'
>>> x = "Hello world!"
>>> x
'Hello world!'

Note that you don’t have to declare variables in advance and you don’t tell Python what type (string, integer, etc.) a variable will have.

Handy built-in functions

These are very useful when you’re trying to sort out issues in the REPL. You’ll rarely need to use these in real programs.

>>> type(7)
<type 'int'>
>>> type(7.0)
<type 'float'>
>>> type("H")
<type 'str'>
>>> type("Hello world!")
<type 'str'>