Posts

7. Python - Try / Error Handling

 try:     print(x) except:     print("NO X")

6. Python - Dictionaries

 dictionaries: // like maps map = {'key' = 5} map['key'] map['newkey'] = 55 // adds a new key and value. can also change existing keys Dictionary comprehension: dictionary = [1,2,3,5] test = {element: 1 for element in dictionary} // returns each number and a key and put in dictionary Methods: 'Key' in dictionaryname // returns true or false if key is in dictionary dictionary.keys() // return all keys dictionary.values() // return all values Loop Dictionaries: for key in dictionaries:     print("{} = {}".format(key,dictionaries[key]))

5. Python - Strings

 Strings: // Strings are arrays of characters, except strings are immutable "Print's Dog" Escaping signs: "Print\'s Dog" The table below summarizes some important uses of the backslash character. What you type... What you get example print(example) \' ' 'What\'s up?' What's up? \" " "That's \"cool\"" That's "cool" \\ \ "Look, a mountain: /\\" Look, a mountain: /\ \n "1\n2 3" 1 2 3 Auto new lines: print(""" HELLO """) Print and End: // print ends with auto newline unless specify end= print("TEST", end = "HELLO") String Methods: string.upper() // uppercase string.lower() // lowercase string.index("substring") // searches for the index where substring starts string.startswith() string.endswith() string.split('optionalseparator') // splits a string into a array of words using whitespace as separator strin...

4. Python - Loops

For loop: Loop through list for element in listname:          print(element, end=' ') // print element and a space Loop through string: for element in stringname:          print(element, end=' ') Normal I Loops: for i in range(100):          do something 100 times While Loops: while panda:          print(1) List comprehensions: (short way to write list loops) squares = [n**2 for n in range(10)] // puts each value after a loop into a list called squares. // loop powers each n by 2 // LEFT = return value, MIDDLE = loop range, RIGHT = condition short_planet = [planet for planet in planets if len(planet)<6] // loop planet through planets list and see if its shorter than 6 characters and return if true test = [True if n>givennumber else False for n in List] // a if b else c // takes in a list and a given number ALT syntax: [     planet.upper() + '!'      for plan...

3. Python - Lists

 Lists: listexample = ["hello", 35, 'A'] listexample[0] // returns "hello" listexample[-1] // returns 'A' at the end Slicing: listexample[0:3] // 0,1, and 2 index listexample[:3] // same as above listexample[1:-1] // exclude first and last listexample[1:] // skip first and go to end of list Functions: len(listhere) // length of list sorted(listhere) // sort ABC sum(listhere) // sums list max(listhere) // max min(listhere) // min List Methods: listname.append("NEWITEM") listname.pop() // returns and removes last item of list listname.index("ITEMTOSEARCH") // returns index of item "ITEM" in listname // returns false or true if item is in list or not Tuples: // like list but immutable and uses parenthesis instead tupletest = (1,2,2,2)

2. Python - Basics

Commenting: # hello ## hello If/Else: if x==0:          do elif x < 0:          do else:          do Logics:     & // and     ^ // exclusive OR, one or other but not both     += // increment     -= // decrement

1. Python - Functions

Function Syntax def functionname(args,args):          do something def functionname(arg,arg):          do something          return Functions with Optional Args: def functionname(VAR="hello"):          print("HEY",VAR) // calling functionname(VAR="NOTHELLO) or functionname("NOTHELLO") overrides the VAR Docstrings // descriptions to explain function def functionname():     """this is a description.     >>> test     """ // use help(functionname) to get description Placeholder line def functionname():          pass // placeholder