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.
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.
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
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).
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 .
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".
Machine uses to "helpers" type and dir print (type(greeting)) print (dir(greeting)) Prints: <class 'str'> ['__add__', '__class__', '__contains__', '__delattr__', '__dir__
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.
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?
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))
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))
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.