Slide 1

Slide 1 text

Lecture 2 How does Python work?

Slide 2

Slide 2 text

Hold your instrument correctly Understand what makes it work

Slide 3

Slide 3 text

What happens here? print("Hello World!")

Slide 4

Slide 4 text

Keeping the dialog going

Slide 5

Slide 5 text

Hello World as it appears Look, it works! It is so "EASY"! I type Hello World! and it prints Hello World!

Slide 6

Slide 6 text

Hello World as it actually is A beast awakens behind a scenes

Slide 7

Slide 7 text

Let's take an X-RAY machine to it Let's see what takes place behind the scenes.

Slide 8

Slide 8 text

First make the program nicer Reorganize it. To understand code break it into individual steps: greeting = "Hello World!" print(greeting) Investigate each step separately.

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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?

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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 .

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

So what happens here? greeting = "Hello World" Your X-RAY Machine uses to "helpers" type and dir print (type(greeting)) print (dir(greeting)) Prints: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

So what is greeting ? It is a class of type str --> Look it up in standard Python Docs:

Slide 22

Slide 22 text

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?

Slide 23

Slide 23 text

Classes have methods. A method is function a class can do With no effort on your part. Sounds good to me

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Methods: Special methods (used to make: ['__add__', '__class__', '__contains__', ... Public methods: 'rstrip', 'split', 'splitlines', 'startswith',

Slide 26

Slide 26 text

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)

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

The gain? You don't have to redo it yourself! Use what is already there

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

Understand what an object IS You'll understand what it DOES

Slide 31

Slide 31 text

If you are stuck ask yourself: Do I understand what each piece IS? No? type , dir , help