Introduction to Python: Day 1

Joe Parente and Joey Perrott are doing afternoon sessions with the Web Coding Bootcamp. I’m learning a lot and transcribing my notes down below.

Python is an interpreted¹ programming language. It supports multiple programming paradigms, but the instructors mostly focused on the object oriented (OO) style. Everything can be understood as an object. Functions (methods²) and classes define actions and attributes for objects. There are statements (if, for, while, try, etc.) and expressions. Included data types are integers, strings, dictionaries, lists and my favorite tuple³ (mostly because it sounds cute).

I’m approaching this subject after becoming somewhat familiar with javascript so a lot of my learning is a bit of a translation from one programming language to another. Some things are the same like “append” but “prompt” becomes “raw input.” The list of Python reserved words and builtins will be good to memorize. The slicing is a little different from JavaScript Array slice() Method syntatically.

sample-ofa-class

Example of a Function

Class

There was a lot to be learned about classes. Classes have attributes and methods. Def__init__ defines the construction.

“The __init__ method (init for initialise) is called when the object is instantiated. Instantiation is done by (effectively) calling the class.”

Source: Introduction to OOP with Python

Handling Exceptions is the way Python doesn’t break when expected disconnects occur. Using a try block you can catch an error and have the program do something else.

Python works in and with other frameworks and languages:

Databases

There are two types of databases, relational and non-relational⁴. In our first python project, we’ll be working on a quote database using The Python NDB Datastore API.

First, as a class, we created a model (a well defined data structure) with just attributes. Then, we translated that into a syntactically correct model that used Property Classes such as StringProperty.

pseudo-model

A class written in pseudocode

the-beginningofa-database

A class and class method

Best Practices

  • Name variables well (i.e. def Add (firstnumber, secondnumber).
  • When programming, think of user experience along the way (in their example, they used “enumerate” to make the selection from the list easier.
  • Except as narrow of an error as possible in try blocks.
  • Never store a user’s password in a database in plain text.
  • Do not expose your data base to the public.
  • Always document your code (code comments called doc strings). When you comment on methods: say what it does, and talk about the required arguments and what they look like and what it returns.

Footnotes

  1. Compiling vs. interpreting is mostly a matter of how the work of “understanding” the program is divided up between different processes, and the line is a bit blurry these days as languages and products try to offer the best of both worlds.

Source: java – Compiled vs. Interpreted Languages – Stack Overflow

2. Methods are associated with a class or instance, and “standalone functions” are not.

Source: python – Difference between methods and functions – Stack Overflow

3. The point of a tuple is that the i-th slot means something specific. In other words, it’s a index-based (rather than name based) datastructure.

Source: James Tauber : Python Tuples are Not Just Constant Lists

4. Therefore, in my opinion, the main difference is about how do you store the data and the storage level of the relationships between them.

Source: What is the difference between a Relational and Non-Relational Database? – Stack Overflow

Advertisements

About KC

I am Katarina Countiss, a multimedia designer. I like blogs, games, art and technology. I am curious about how things are made.
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s