Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python [2019/01/18 13:55] paul [Python] |
python [2019/07/09 17:29] (current) paul [Formatting] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Python ===== | ====== Python ===== | ||
- | ==== Web App ==== | + | You can make a python file run by adding this to the script: |
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | This tells the command line that it is a python script. | ||
+ | |||
+ | There is a distinction between function, and instances. Like for instance, a file | ||
+ | called: | ||
+ | |||
+ | <code python> | ||
+ | test_file = open(" | ||
+ | </ | ||
+ | |||
+ | This file has instances you can acces: | ||
+ | |||
+ | <code python> | ||
+ | test_file.mode | ||
+ | </ | ||
+ | |||
+ | Then there are also functions you can access, and to access them you use parenthesis. | ||
+ | |||
+ | ===== Python Version ===== | ||
+ | Ok so theres a very big distinction between python 2 and 3. I already ran into | ||
+ | issues looking at tutorials and just using the print statement, which apparently | ||
+ | got changed from a statement to a function in version 3. | ||
+ | |||
+ | In my version of Ubuntu 16.04, the default python is 2, and to use 3 i have to do | ||
+ | python3 | ||
+ | |||
+ | ===== Python Types ===== | ||
+ | |||
+ | Numbers, strings, lists, tuples, dictionary. | ||
+ | |||
+ | ==== Strings ==== | ||
+ | |||
+ | Multiple ways of printing strings. | ||
+ | * single quotes | ||
+ | * double quotes | ||
+ | * triple quotes | ||
+ | |||
+ | ==== Numbers ==== | ||
+ | Integers by default. | ||
+ | |||
+ | In python you do not predeclare variables. It is dynamically typed. You don't have to | ||
+ | worry about types. | ||
+ | |||
+ | In python you don't worry about references or pointers. But you do worry about | ||
+ | mutable vs immutable data. | ||
+ | |||
+ | Integers are an immutable type. Strings are also immutable. Lists are mutable. | ||
+ | |||
+ | To make a list you do: | ||
+ | |||
+ | <code python> | ||
+ | x = [0 , 1] | ||
+ | </ | ||
+ | |||
+ | Python puts values in memory and then it creates labels to it. Thats a concept of | ||
+ | variables. Ok cool, so a big ditinction is that data is stored in memory, vs | ||
+ | allocated for by datatypes. | ||
+ | |||
+ | ==== Booleans ==== | ||
+ | |||
+ | 0 evaluates to '' | ||
+ | |||
+ | ==== Lists ==== | ||
+ | |||
+ | Lists are one of the fundamental datatypes. You can create lists by doing the | ||
+ | following: | ||
+ | |||
+ | <code python> | ||
+ | this_is_a_list = [' | ||
+ | </ | ||
+ | |||
+ | You can index the list: | ||
+ | |||
+ | <code python> | ||
+ | print(this_is_a_list[0]) | ||
+ | |||
+ | # This prints everything including 0 up to but not including 3. | ||
+ | print(this_is_a_list[0: | ||
+ | </ | ||
+ | |||
+ | You can have lists inside of lists. | ||
+ | |||
+ | There is a crazy amount of stuff with can do with lists, and python is really | ||
+ | flexible and easy to use. Sort, add, combine lists, what have you. Super quick and | ||
+ | easy. | ||
+ | |||
+ | ==== Tuple ==== | ||
+ | |||
+ | Unlike a list, you can't change a tuple after you create it. | ||
+ | |||
+ | Declared with parenthesis. | ||
+ | |||
+ | <code python> | ||
+ | this_is_a_tuple(3, | ||
+ | </ | ||
+ | |||
+ | ==== Dictionaries ==== | ||
+ | |||
+ | Values that have a unique key for each value that you are storing. Very similar to | ||
+ | lists, but you can't join like you can with a plus sign. | ||
+ | |||
+ | ===== Operators ===== | ||
+ | |||
+ | 7 different operators. | ||
+ | |||
+ | <code python> | ||
+ | + - * / % ** // | ||
+ | </ | ||
+ | |||
+ | ==== Logical Operators ==== | ||
+ | |||
+ | <code python> | ||
+ | if else elif | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | == != > >= < <== | ||
+ | </ | ||
+ | |||
+ | You can combine conditionals with '' | ||
+ | |||
+ | ===== Loops ===== | ||
+ | |||
+ | ==== for loops ==== | ||
+ | |||
+ | for x in range(0, | ||
+ | for x in this_is_list | ||
+ | |||
+ | ==== while loops ==== | ||
+ | |||
+ | <code python> | ||
+ | while(x != 0): | ||
+ | do stuff! | ||
+ | </ | ||
+ | |||
+ | ===== Functions ===== | ||
+ | |||
+ | Functions allow us to reuse and make more readable code. In python you don't have to | ||
+ | specify return type. Woah! | ||
+ | |||
+ | <code python> | ||
+ | def addNumber(fNum, | ||
+ | sumNum | ||
+ | return seNum | ||
+ | </ | ||
+ | |||
+ | ===== Strings ===== | ||
+ | |||
+ | Python has really strong string support. You can index characters | ||
+ | within strings using square brackets | ||
+ | |||
+ | <code python> | ||
+ | string_thing(0: | ||
+ | </ | ||
+ | |||
+ | Theres a bunch of stuff you can with the % operator, the string formatting operator | ||
+ | |||
+ | each string kinda becomes an object that has lots of functions associated with it | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ==== Formatting ==== | ||
+ | |||
+ | If you want to print a neat line of data use the [[https:// | ||
+ | |||
+ | For example: | ||
+ | <code python> | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | This sets a fix width for each field and aligns the test either left '' | ||
+ | |||
+ | ===== Mutability ===== | ||
+ | |||
+ | In python pretty much everything is an object, and these objects are either | ||
+ | mutable or immutable. Mutable means the object and its contants can change and | ||
+ | immutable means that it cannot. Lots of basic data types are immutable, such as | ||
+ | int, float, and string. The crazy thing is that python creates an object to | ||
+ | represent that value that a variable such as an int is set to and then uses the | ||
+ | variable as a tag that points to that object. | ||
+ | |||
+ | Immutable ojects: | ||
+ | * bool | ||
+ | * int | ||
+ | * float | ||
+ | * tuple | ||
+ | * str | ||
+ | |||
+ | Mutable objects: | ||
+ | * list | ||
+ | * set | ||
+ | * dict | ||
+ | |||
+ | |||
+ | ===== File IO ===== | ||
+ | |||
+ | Yay, we access files! | ||
+ | |||
+ | It is super easy. Just use the '' | ||
+ | '' | ||
+ | read it, write it, delete it using an os function, whatever you want man! | ||
+ | |||
+ | ===== OOP ===== | ||
+ | |||
+ | Objects have attributes and abilities. We define these objects inside classes that | ||
+ | contain attributes and abilities. | ||
+ | |||
+ | Define a class using class '' | ||
+ | |||
+ | Private attributes are preceeded by two underscores. | ||
+ | |||
+ | There is the '' | ||
+ | the '' | ||
+ | |||
+ | There are also constructors which you call with '' | ||
+ | |||
+ | There is method overloading, | ||
+ | it optional. Then you can check inside the method if that parameter is None. | ||
+ | |||
+ | There is polymorphism, | ||
+ | class, and then you can pass in child objects of that parent class. | ||
+ | |||
+ | See the '' | ||
+ | |||
+ | ===== Web App ===== | ||
Did this [[https:// | Did this [[https:// | ||
- | ==== Debugging ==== | + | |
+ | I used SQLAlchemy python plugin to interface with an SQLite db and also used a migration app to automatically handle db changes. | ||
+ | |||
+ | ===== Debugging | ||
{{:: | {{:: | ||
To debug, you can use PuDB, which is a nice graphical terminal program. | To debug, you can use PuDB, which is a nice graphical terminal program. | ||
+ | |||
+ | ===== Virtual Environments ===== | ||
+ | |||
+ | A virtual environment creates a python environment stored in a folder of your choosing. This allows you to install all kinds of crap not to your system, but to that virtual environment in that folder. | ||
+ | |||
+ | Virutal environments where included in python3 and to make one you run: | ||
+ | |||
+ | <code bash> | ||
+ | python3 -m venv / | ||
+ | </ | ||
+ | |||
+ | ===== Plane Notes ===== | ||
+ | |||
+ | Did a bunch of review, and now gonna skim through the book! | ||
+ | |||
+ | Fundamental data types: | ||
+ | ints, floats, strings, | ||
+ | |||
+ | Learning how to build strings with single quotes, double quotes and tripple | ||
+ | single/ | ||
+ | |||
+ | Learning how to build lists and index it and slice them with ranges [0:4]. Only | ||
+ | works in legal ranges. A slice will always return a list. an access will return | ||
+ | an element. | ||
+ | |||
+ | Learning about ranges. | ||
+ | |||
+ | Learned about pop, and how you can index a pop, by doing | ||
+ | list_whatever.pop(index) and how theres no push in python because there is an | ||
+ | append, and python wants to be all pure and shit and not have duplicate | ||
+ | functions. | ||
+ | |||
+ | You can assign lists uisng text parallel assignment | ||
+ | <code python> | ||
+ | firstname, lastname = [" | ||
+ | </ | ||
+ | |||
+ | God python is so wierd with shit, like, there' | ||
+ | everywhere. Haha. | ||
+ | |||
+ | Dictionaries have keys and they are defined by curly braces {}. They are | ||
+ | unordered. You can make a dictionary using a sequence of keys. | ||
+ | |||
+ | Learning about the format functions on strings. God damn dude you can do | ||
+ | anything to strings. You use it by calling it on a string and using {0}, {1} ... | ||
+ | positionals to insert formatted string values. | ||
+ | |||
+ | For instance to insert a decimal number from a calc: | ||
+ | |||
+ | <code python> | ||
+ | strNum = "Hi this a the square root of 2: {0: | ||
+ | </ | ||
+ | |||
+ | Omg reg exes. Ok so you can create a pattern by using | ||
+ | pattern = re.compile(' | ||
+ | to split a string. COOOL. | ||
+ | <code python> | ||
+ | pattern = re.compile(", | ||
+ | pattern.split(" | ||
+ | # this will print out [" | ||
+ | </ | ||
+ | |||
+ | You can do so much funky shit with strings. Every string object has a list of | ||
+ | useful functions. | ||
+ | |||
+ | OK tuples time and I'm fuggin DONE. Tuples are like lists, but they' | ||
+ | with parenthesis and they' | ||
+ | they' | ||
+ | |||
+ | They can only be changed by replacement. So you could do something like: | ||
+ | <code python> | ||
+ | t = (1,2,3) | ||
+ | l = list(t) | ||
+ | l.append(" | ||
+ | t = tuple(l) | ||
+ | </ | ||
+ | |||
+ | You can index tuples by using square braces. | ||
+ | |||
+ | Tuples of 1 are not created as tuples, but as objects of their respective | ||
+ | datatype. | ||
+ | |||
+ | Empty types are still tuples. | ||
+ | |||
+ | <code python> | ||
+ | ().__class__ == tuple | ||
+ | </ | ||
+ | |||
+ | Tuples can be embedded! | ||
+ | |||
+ | ===== Methods ===== | ||
+ | |||
+ | To make a function you simply write: | ||
+ | |||
+ | <code python> | ||
+ | def my_function(a, | ||
+ | return a + b | ||
+ | </ | ||
+ | |||
+ | PS: this is how you document a method. | ||
+ | <code python> | ||
+ | ## | ||
+ | # @brief This function does this | ||
+ | # | ||
+ | # @return It returns nothing | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | You can create a method that is in the class scope by adding a self parameter. | ||
+ | |||
+ | Methods that don't return anything return '' | ||
+ | |||
+ | You can have method with default arguments: | ||
+ | |||
+ | <code python> | ||
+ | def method_a(a, | ||
+ | return a + b + c | ||
+ | </ | ||
+ | |||
+ | Functions without self arg are global, even if they are defined in a class. | ||
+ | |||
+ | Empty functions need a pass function but lines after the pass filler still get | ||
+ | called. | ||
+ | |||
+ | A string placed at the top of a function definition is a documentation string | ||
+ | that can be accessed with the '' | ||
+ | |||
+ | ===== Classes ===== | ||
+ | |||
+ | It's very easy to make classes in python. | ||
+ | |||
+ | <code python> | ||
+ | class Rabbit: | ||
+ | def name(self): | ||
+ | return " | ||
+ | def _status(self): | ||
+ | return " | ||
+ | def __password(self): | ||
+ | return ' | ||
+ | </ | ||
+ | |||
+ | ===== Control Statements ===== | ||
+ | |||
+ | The '' | ||
+ | the while loop without running any code following the continue. | ||
+ | |||