Slide 1

Slide 1 text

Lecture 1 Getting started with Python

Slide 2

Slide 2 text

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...

Slide 3

Slide 3 text

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?

Slide 4

Slide 4 text

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...

Slide 5

Slide 5 text

I think so Do you know why?

Slide 6

Slide 6 text

You may not realize this yet You will want more than just "to learn" Python

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

The same logic will apply to this course

Slide 12

Slide 12 text

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 .

Slide 13

Slide 13 text

I will try to show and demonstrate to you Python patterns I will try to teach you how to investigate and understand these patterns

Slide 14

Slide 14 text

Your job will be follow along learn to play these "chords" then on your own go out and learn more

Slide 15

Slide 15 text

Connect the dots 1

Slide 16

Slide 16 text

Connect the dots 2

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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).

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Programming is thinking The program is part of your brain. Solve the problem while programming.

Slide 25

Slide 25 text

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!

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

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 ...

Slide 30

Slide 30 text

Programming Work ow Write -> Run -> Evaluate -> Write -> Run ...

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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.

Slide 34

Slide 34 text

Success with PyCharm

Slide 35

Slide 35 text

Success with Sublime Text

Slide 36

Slide 36 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...

Slide 37

Slide 37 text

What should I expect while programming

Slide 38

Slide 38 text

ERRORS

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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?

Slide 41

Slide 41 text

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