Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Python

Introduction to Python

Python programming

Istvan Albert

April 27, 2018
Tweet

More Decks by Istvan Albert

Other Decks in Programming

Transcript

  1. 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...
  2. 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?
  3. 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...
  4. 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
  5. 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.
  6. 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.
  7. 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
  8. 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 .
  9. I will try to show and demonstrate to you Python

    patterns I will try to teach you how to investigate and understand these patterns
  10. Your job will be follow along learn to play these

    "chords" then on your own go out and learn more
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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).
  16. 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.
  17. What does the dialog look like? As your write your

    program you will run it repeatedly after adding any new content. Iterative process.
  18. Programming is thinking The program is part of your brain.

    Solve the problem while programming.
  19. 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!
  20. 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.
  21. 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.
  22. 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.
  23. 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 ...
  24. 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.
  25. 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.
  26. 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...
  27. 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
  28. 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?