Python

You can make a python file run by adding this to the script:

#!/usr/bin/python

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:

test_file = open("test.txt", "wb")

This file has instances you can acces:

test_file.mode 

Then there are also functions you can access, and to access them you use parenthesis.

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

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:

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 False. 1 or any other number evaluates to True.

Lists

Lists are one of the fundamental datatypes. You can create lists by doing the following:

this_is_a_list = ['Cup', 'Watch', 'Scissors', 'Iphone']

You can index the list:

print(this_is_a_list[0]) 
 
# This prints everything including 0 up to but not including 3.
print(this_is_a_list[0:3])

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.

this_is_a_tuple(3,1,4,1,5,9)

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.

7 different operators.

+ - * / % ** //

Logical Operators

if else elif
== != > >= < <==

You can combine conditionals with or and not.

for loops

for x in range(0,10) for x in this_is_list

while loops

while(x != 0):
  do stuff!

Functions allow us to reuse and make more readable code. In python you don't have to specify return type. Woah!

def addNumber(fNum, lNum):
    sumNum  = fNum + lNum
    return seNum

Python has really strong string support. You can index characters within strings using square brackets

string_thing(0:2)

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

  • isalpha()
  • isalnum()
  • strip()
  • split()

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

Yay, we access files!

It is super easy. Just use the open, close, read, write file functions! Create a variable that is a file, then open it, read it, write it, delete it using an os function, whatever you want man!

Objects have attributes and abilities. We define these objects inside classes that contain attributes and abilities.

Define a class using class Name:

Private attributes are preceeded by two underscores.

There is the self instance that refers to the object instance. Just like the this.

There are also constructors which you call with __init__

There is method overloading, which you do by setting a parameter to None. This makes it optional. Then you can check inside the method if that parameter is None.

There is polymorphism, which allows you to make functions that take in a parent class, and then you can pass in child objects of that parent class.

See the classes.py in the learn directory for examples!!

Did this python - flask tutorial

I used SQLAlchemy python plugin to interface with an SQLite db and also used a migration app to automatically handle db changes.

To debug, you can use PuDB, which is a nice graphical terminal program.

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:

python3 -m venv /path/to/virtualenv/folder
Enter your comment. Wiki syntax is allowed:
 
  • python.txt
  • Last modified: 2019/03/20 20:03
  • (external edit)