Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
132 views

01 Getting Started With Python

This document provides an introduction to learning Python programming through analogies to learning the ukulele. It suggests learning Python patterns first by writing simple programs without fully understanding the concepts, similar to learning musical chords without understanding music theory. This iterative approach of learning by doing and gradually increasing understanding is presented as more effective than only learning concepts. The document encourages getting started by writing a simple "Hello World" program and exploring errors as an important part of the learning process.

Uploaded by

muscarisnaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

01 Getting Started With Python

This document provides an introduction to learning Python programming through analogies to learning the ukulele. It suggests learning Python patterns first by writing simple programs without fully understanding the concepts, similar to learning musical chords without understanding music theory. This iterative approach of learning by doing and gradually increasing understanding is presented as more effective than only learning concepts. The document encourages getting started by writing a simple "Hello World" program and exploring errors as an important part of the learning process.

Uploaded by

muscarisnaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Lecture 1

Getting started with Python


How to Learn Python
A few months ago I decided to learn to play the
Ukulele.

I've never played a strumming based instrument


before. It struck me how many analogies to Python
programming I've observed.

Let's make learning Python like learning the Uke

We will rely on analogies to playing the Ukulele...


Here is an analogy
Here are some Uke chords. A , Am , F , G

They all sound nice

I can learn to play some songs with them. Do I need


to understand what they are made of?
What if we learn Python via patterns
Here is a typical le reading pattern

for line in open("data.txt"):


words = line.split()
print(words)

I will tell you that this code will do the following:

1. iterates over a le, then on each line


2. will break each line into words
3. print a list of the words found in step 2

Is it a good idea to teach Python this way...


I think so
Do you know why?
You may not realize this yet
You will want more than just "to
learn" Python
You want to apply Python!
It is very likely that you want to perform more
complicated analyses.

Read specially formatted les, perform analytical or


numercial computations, access a database, generate
a plot, ccreate a website ... etc

numerical python -> numpy

data science python -> pandas

web development -> django

visualization -> matplotlib

bioinformatics -> biopython


How do we use Python libraries?

You to apply patterns.

With numpy how to select all numbers larger than x?

# Generate numbers [ 1, 2, 3, ... 10 ]


x = numpy.arange(10)

# This is the selection pattern.


y = x [ x > 5 ]

This is called boolean indexing. You can use it before


you fully understand how it works.

You will make more progress if you start learning


patterns from the beginning.
Back to our analogy
Here are some two chords. G , G7

First I learned these without caring. I played songs


with them.

Then it go me thinking - why are these called the way


they are. Why do they sound slightly differently.
Experience with musical patterns
Once I learned my patterns I got interested in
learning more. Musical scales, tones, steps.

Today I know that major chords are built out of: 1st,
3rd and 5th notes of the scale.

So called seven chords contain the 1st, 3rd, 5th and


7th notes of the scale.

The G chord represents the scale that starts on G.

... and so on ... I had more fun learning it this way as I


already knew how to use the G chord
The same logic will apply to this
course
Eventually I will tell you what this is.
for line in open("data.txt"):
words = line.split()
print(words)

It is fascinating how simple and how advanced even


this simple example is.

It encompasses everything that makes Python such


an awesome language.

By the end of the course you will understand that


open is an iterator that yields a string class that has
a split method that returns a list class .
I will try to show and demonstrate
to you Python patterns
I will try to teach you how to
investigate and understand these
patterns
Your job will be follow along
learn to play these "chords"
then on your own go out
and learn more
Connect the dots 1
Connect the dots 2
In what way is Python like a Uke?
Has few rules (simple).
Tolerant of errors.
Less painful while practicing.
Allows for a lot of freedom.
Takes less effort to make progress.
What makes Python a good programming
language?

Simple enough - but not overly generic.


High utility - but not overly specialized.
Integrates well into science and web development.

There are hundreds of programming languages:


Why Python?

Python 's popularity -> survival of the ttest.

Among the best languages to get things done quickly


and ef ciently.
When is Python NOT a good choice?

Execution speed is critically important.


For writing "native" programs that need to
integrate with an operating system closely.
Writing programs that rely on GUI (graphical user
interfaces).

Possible to integrate other languages into Python .

Example: C can be embedded into a Python program.


Getting ready
Get your "instrument" and learn how to "hold it".
There are common sense guidelines.
And there different alternatives within them.
See what ts your perspective and philosophy
better.
What is Python Programming?
Python is a so-called "scripting language".

The Program: a le that contains Python


instructions.
Running The Program: having Python read and
execute the le.

Python programs have the .py extension (but are


regular text les).
Programming Philosophies
1. Carefully plan and design set of instructions,
ensure that each is correct, then run the program.

2. Initiate a dialog with the computer , where you


quickly execute and evaluate instructions, and
iteratively nd the correct solution.

Most people use a mixture of both, and you'll nd


yourself attracted to one of the approaches.

I prefer the second style.


What does the dialog look like?
As your write your program you will run it repeatedly
after adding any new content.

Iterative process.
Programming is thinking
The program is part of your brain.
Solve the problem while programming.
Getting Physical

Your body needs to learn how to use the tools that


allow you to program.

Hand-eye-mind coordination: + +

The way you do something affects the way you


think about solutions!
Get Python
Recommendation:

Anaconda (Python with scienti c libraries). See links


to the download.

Alternatives:

1. Get Miniconda (a smaller version of Anaconda)


2. Of cial Python distribution

If you already have miniconda keep using that.


Say NO to "interactive" Python
Python console
IPython console
Jupyter web interface

Inef cient ways to get started. They are the wrong


kind of "easy".

Promote a linear way of thinking

You must learn to think iteratively - continuously


re ne your ideas.
Interactive Python
A naive idea that keeps getting reinvented.

It would be nice if it worked... It would be so


convenient... Just type some commands and go.

But programming is never a linear process.

Interactivity does more harm that good - it slows


down your understanding how programming is done.
One somewhat useful application of interactivity

Help on very simple objects. Within Python you can


type:

>>> help(str)

prints:

class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encodin
| errors is specified, then the object must expose a data buff
| that will be decoded using the given encoding and error hand
...
Programming Work ow

Write -> Run -> Evaluate -> Write -> Run ...
A Programmer's Editor
Programming is about writing commands into a le,
running the code, repeating the process.

It is essential to streamline that process as much as


possible. Your editor should be able to:

1. Show you the number for each line in the le


2. Color code your instructions (helps recognize
errors)
3. Execute your program with Python and show you
the output.
I PyCharm

Other choices: Sublime Text, Notepad++, etc.


First Program: Hello World!
Python programs have the extension .py .

Write the following and save it as hello.py :

print ("Hello World!")

First task

Set up your computer so that you can run this code


right from your editor.
Success with PyCharm
Success with Sublime Text
Reality check
It may take you a while to get your computer set up.

It is a one-time challenge - but can be very-very


annoying:

Where is my Python?
Why won't it run my program?
Why does this still not work?

Persevere, ask around, Google it. Ask someone...


What should I expect while
programming
ERRORS
Embrace the Errors
Making errors is OK.

All programmers make errors all the time

If you are not making errors you can't be


programming.

Your skill is measured in how quickly you identify the


source of your errors.

Machines are stupid. Need babysitting


Explore the Errors
Remove or add a single character in the program:

print("Hello World!"

print("Hello World!)

print(Hello World!")

Then rerun. What do you see? Are the errors the


same? Are some errors more informative?

Could you locate the error based on the message?


Now lets get you going.
Set up your editor and get ready to
program!

You might also like