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

How does Python work

How does Python work

Istvan Albert

April 27, 2018
Tweet

More Decks by Istvan Albert

Other Decks in Programming

Transcript

  1. Hello World as it appears Look, it works! It is

    so "EASY"! I type Hello World! and it prints Hello World!
  2. Let's take an X-RAY machine to it Let's see what

    takes place behind the scenes.
  3. First make the program nicer Reorganize it. To understand code

    break it into individual steps: greeting = "Hello World!" print(greeting) Investigate each step separately.
  4. Using Variables Programs ought to be descriptive: greeting = "Hello

    world" print(greeting) Often greeting is called a variable It may also be called name or identi er.
  5. What does a name refer to? Take the following: a

    = 1 b = a a = 2 print(a, b) What does it print 2,1 or 2,2 ? How was Python made to work?
  6. Python names are references print(a, b) will print 2,1 why?

    a = 1 a ----> 1 b = a b ----> a -----> 1 a = 2 a ----> 2 All python names are references Most of the time this a good thing. Simpli es code. There are some interesting applications and potential pitfalls in more advanced use.
  7. Pick the right names It is your job to pick

    descriptive names: greeting = "Hello world" print(greeting) versus bkxWrtyus_37374hrjw = "Hello World" print(bkxWrtyus_37374hrjw) message and bkxWrtyus_37374hrjw are identi ers (names, variables) we created to refer to something. Obviously one is more readable than the other.
  8. Naming things can be challenging What should I call something?

    The choice of naming can make it easier to understand what the program does. Use simple, logical names and you'll end up writing simple programs. What comes rst
  9. My preference Keep it simple: message , value , line

    , row For collections: messages , values , lines , rows Don't do: unnormalized_fastq_read_counts transcript_level_tpm_values Don't "cram" information into a variable name.
  10. Document the code Instead of long identi er names, make

    your documentation short and to the point. # Unnormalized fastq read counts. counts = [ 1, 2, 3 ] Later you will learn to modularize the code. The counts name should be accessible in a restricted region (scope).
  11. What is a valid name? alphabet + numbers + _:

    foo , foo_123 A few words are reserved for the language itself: for class if def try ... Use all lowercase names in most cases. Use the _ sparingly to separte compound words up_regulated .
  12. You can overwrite existing names It is possible to accidentally

    make use of names that overwrite ("shadow") existing names. This won't work: greeting = "Hello world" print = 1 print(greeting) PyCharm will visually indicate a warning. Look for the warning that uses the word "shadow".
  13. So what happens here? greeting = "Hello World" Your X-RAY

    Machine uses to "helpers" type and dir print (type(greeting)) print (dir(greeting)) Prints: <class 'str'> ['__add__', '__class__', '__contains__', '__delattr__', '__dir__
  14. What do type and dir do? type tells you what

    the class of the object is dir tells you what the attributes and the methods of the class are What is a class? A class is thing that: 1. stores some data 2. can do something with this data The dir function tells us what the object can do.
  15. You won't need to create your own classes At least

    not initially, there is little to gain First learn how to use well designed classes
  16. So what is greeting ? It is a class of

    type str --> <class 'str'> Look it up in standard Python Docs:
  17. String class "instance" greeting = "Hello World" Is equivalent to:

    greeting = str("Hello World") It does it automatically in this case. For other classes you have to call the "constructor" explicitly: str Why would I want a class?
  18. Classes have methods. A method is function a class can

    do With no effort on your part. Sounds good to me
  19. So what methods do I have? It is a bit

    hard to see this way: print (dir(greeting)) You can print it out nicer for method in dir(greeting): print(method) or you can pretty print # This is a nicer printing function. from pprint import pprint pprint(dir(greeting))
  20. Methods: Special methods (used to make: ['__add__', '__class__', '__contains__', ...

    Public methods: 'rstrip', 'split', 'splitlines', 'startswith',
  21. How many methods does str have? print(len(dir(greeting))) prints 77 Whoa,

    what are all these! See the help page. Each method has documentation. help(str)
  22. Howdo I use the methods Among the manyt things a

    str class knows how to format itself in upper case, split by words, count the number of occurances of a character center itself in a width etc. ... print(greeting.upper()) print(greeting.split()) print(greeting.count("o")) print(greeting.center(30))
  23. Keep investigating. The results returned by a methods may be

    a new class greeting = "Hello World!" words = greeting.split() print(type(words)) print(dir(words)) What type does the words have? What methods? Now check the docs.
  24. If you are stuck ask yourself: Do I understand what

    each piece IS? No? type , dir , help