Docsity

Learning the Pythonic Way, Study notes of English

Partial preview of the text

Download Learning the Pythonic Way and more Study notes English in PDF only on Docsity!

Learning the Pythonic Way

Athula Balachandran Wolf Richter

The Man, The Legend: Zed Shaw

So, he made a web server too.

It's called mongrel2.

Oh, and Learning Python the Hard Way [LPTHW].

and... Programming, Motherfuckers ...

Need I say more?

Let's do this.

One at a time.

All your pseudo-code are belong to Python

Wikipedia: What is Quicksort?

Really? Yes!

quicksort([ 9 , 8 , 4 , 5 , 32 , 64 , 2 , 1 , 0 , 10 , 19 , 27 ]) [ 0 , 1 , 2 , 4 , 5 , 8 , 9 , 10 , 19 , 27 , 32 , 64 ]

Python “comes with batteries”

Python enables rapid prototyping

Give me a dynamic web app NOW

from flask import Flask

app = Flask(name)

@app.route("/")

def hello():

return "Hello World!"

if name == "main":

app.run()

Use the Interpreter

●

Code and experiment interactively

●

Use help()

●

Explore functionality and ideas

●

Then code in your main editor

Just one thing

●

Whitespace matters

●

Defines blocks → C-world thinks { }

●

Use spaces

●

4 spaces per indentation level

●

spaces > tab → just be consistent

●

Really though, generally aids readablity

●

Set your editor preferences ahead of time

Starting a Script and Comments

●

Start with:

#!/usr/bin/env python

●

Then you can chmod +x script.py

●

The #! is a special character combination

●

Tells the OS how to execute a file

●

Comments start with a

●

They go to the end of the line

Math – Business as Usual

●

import math → extra math functions

●

Convert between: int() and float()

●

Convert to string: str()

float( 4 ) / 3

float( 4 / 3 )

int( 4 / 3 .) 1 str( 2 ** 4 ) '16'

Danger: Division from the Future

●

Always read the top of a script first

SyntaxError: from future imports must occur at the beginning of the file

Booleans

●

True/False – actual values

●

Logical Operators

●

and – not && (although & is set and bit and)

●

or – not || (although | is set and bit or)

●

not – not ~ (although ~ is bit not)

● As expected... >>> True and True

True

True or False True not True False not False True