Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
198 lines (142 sloc) 6.52 KB

Lesson 1: The basics

Defining our tools

Python is a high-level coding language. There are many languages (Ruby, Javascript, PHP), but Python is nice because you can use it to quickly build projects large and small. It has a massive community that builds and maintains libraries of code that extends python's core functionality. You can run python code as executable files or in the Python shell.

The command line, accessible in Mac through Terminal or PC through PowerShell, is an interface designed to execute code without a graphical user interface. It's extremely powerful and can be used to chew through complex tasks much faster than through traditional point-and-click software.

Sublime is a text-editor with nice capabalilites like syntax highlighting which can give you clues about the formatting of your code specific to the language you're using. We'll be using Sublime for this workshop, but feel free to explore other text editor options.

GitHub is a Web-based service built on git, an industry standard system used to manage code and software. It's centered around the idea of version control, allowing multiple collaborators to contribute to a code base. Project managers can accept or reject these changes, branch the code in different directions or revert to old versions in case of disaster.

An API, or application programming interface, is a system that allows you to easily interact with a third-party's data (or your own, if it's an in-house API). They are typically designed so you can programatically query for specific types of data through requests structured like URLs. For this workshop, we'll be using a Python library that makes using the Twitter API really easy.

A server is just a computer that's going to do whatever we say. It might be remote, like ones we use through Amazon Web Services, or it could be local on our own machine. Running a remote server for code is nice because it doesn't require you to keep your personal or work machine on all the time.

Got all that?

Brent's got it.

Basic command line

At its core, the command line allows you to navigate the file structure of your computer, creating, moving and deleting like you would with a regular interface. Here's some basic stuff (Note: the dollar sign just demonstrates the prompt, so don't include it in the command.)

Show me the present working directory

$ pwd

Show me the files and folders in the current directory

$ ls

Change the current directory to "Documents"

$ cd ~/Documents

Make a new directory called "code-workshop"

$ mkdir code-workshop

Clear the interface!

$ clear

Use the up and down arrows to scroll through previous commands.

Brent's got it.

Let's get Pythoning!

Now that we've made a shiny new directory, let's do some stuff with it.

mkvirtualenv code-workshop

Now we're working in a virtual environment, which keeps everything nice and tidy on our machine so we don't break stuff.

Fire up the Python shell

python

Python can do math, so try it out!

10 + 10

Python can do variable assignment!

x = 10
y = 'millennials amirite?'
print x, y

Python can do logical tests!

if x > 10:
	print "X is more than 10!"
elif x == 11:
	print "X is 11!"
else:
	print "idk is it 10?"

Python can do loops!

for x in range(1,10):
	print x
x = 1
while x < 15:
	print "x is",x
	x += 1

Python data types

Python has five basic types of data: numbers, strings, lists, tuples and dictionaries. We won't cover them all extensively, so here's some good background.

Numbers are more complicated than they sound, because computers treat numbers differently. Try:

1/2

And:

1.0/2

Strings are text objects that have a number of built-in functions. Think of them literally as a string of characters. You can find the length...

myVar = "I am so cool!"
len(myVar)

or add or subtract values.

newVar = myVar[0:8] + "NOT" + myVar[7:]
print newVar

Lists are really common, and essentially consist of a collection of values that can be numbers, strings, even other lists!

myList = [1,2,3,4,'YAY',6,['7','LISTCEPTION']]
print myList
print myList[1]
print myList[6]
print myList[6][1]

You can also loop through lists.

list_to_test = [1,2,3,4,5]
for x in list_to_test:
	if x != 3:
		print x,"is not 3"
	else:
		print x,"is 3"

Dictionaries are neat, because they're like lists based on the concept of "key-value pairs." Think of it like an object with certain assignable attributes.

my_truck = {'make':'Nissan','cab_type':'crew','color':'silver','model':'Frontier'}
print my_truck
print 'Tyler bought a',my_truck['make']+' '+my_truck['model']+'. It is',my_truck['color']+'.'

You can also use methods specific to the dictionary object, like:

print my_truck.keys()
print my_truck.values()

Brent's got it.

Running Python scripts

Even these small bits of code can be extremely powerful, so let's try combining them into a python script and do some real damage. Exit the shell.

exit()

Then right click and download the following file into your project folder. Then open the following file in Sublime.

To run the file, enter the following command:

python hello-world.py

Stuck? Have a question about how to do something in Python? Google it! There are so many Python users out there, you're bound to find a good answer.

Brent's got it.

Remember: The best coders know when to ask others for help!

Further reading

>>> Next lesson