Python Cheat Sheet.docx

(31 KB) Pobierz

CREATED BY LIAM GIBBINGS

Functions, Methods & Attributes

Numbers

·         math.pi and math.e: Returns values of both pi and exponential const. respectively.

·         math.sqrt(integer1): Returns square root of integer, use cmath.sqrt for negatives.

·         math.floor(integer1): Returns integer rounded down to nearest whole.

·         math.ceil(integer1): Returns integer rounded up to nearest whole.

·         round(integer1, [NumOfDigits]): Rounds integer depending on number of digits specified, default is to nearest whole.

·         range(integer1, integer2, [integer3]): Generates a list of integers starting from integer1 up to but not including integer2. Integer3 specifies step over amount.

 

Strings

·      ‘string1.find(‘string2): Returns index of string2 string in referenced string1.

·      string1.join(list1): Returns string value of list1 but with string1 in between words.

·      string1’.lower/upper(): Returns completely lower/upper case version of string1.

·      ‘string1’.strip(): Returns one string with whitespace removed.

·      ‘string1’.split([‘string2’]): Returns list with string2 removed from string1. If string2 is not specified, white space is assumed.

·      ‘string1’.title(): Returns string with all first letters capitalized.

·      ‘string1’.replace(‘string2’,’string3’): returns string1 but with all instances of string2 replaced with string 3.

·      ‘string1’.count(’string2’): Returns the number of times that string2 appears in string1.

·      ‘string1’.endswith(‘string2’): Returns True or False value if string1 ends with string2. Use ‘string1’.startswith(‘string2’) for the reverse.

·      ’string1’.count(‘string2’): Returns the number of times that string2 appears in string1.

 

Lists

·      In a list (just like a string) you can index a certain point in the list from index 0 to the end using square brackets, you can use negative indexes to reference from the end of a list. You can also use this method to change values in a list individually: list1[0] = ‘string1’. You can use del to delete entries.

·      Just like indexing, you can also slice lists using ‘:’ inside the square brackets: list1[0:8]. You don’t have to specify either value, list1[5:] or list1[-5:]  would reference entries 5 onwards. You can use slicing to assign values to multiple positions at once. You can also use list1[:] to create a copy of the list without affecting the original.

·      list1.append(object1): Adds object1 to the end of list1.

·      list1.count(object1): Returns the number of times that object1 appears in list1.

·      list1.extend(object1): Extends list1 with object1 creating a whole list instead of append which jams a an object inside the list.

·      list1.index(object1): Returns the position from 0 where object1 is located in list1.

·      list1.insert(index1, object2): Inserts object2 at index1.

·      list1.pop([index1]): Deletes index1 and returns it, default is end of list1.

·      list1.remove(object1): Removes the first instance of object1 from list1.

·      list1.reverse(): Reverses the order of list1.

·      list1.sort(): Sorts a list from lowest value to highest, both numbers and letters. Supply “reverse=True” as a parameter for reverse sorting. Supply “key=function1” as a parameter to sort according to a function that returns a number, for example “key=len” would sort the list according to length.

·      zip(list1, list2, list3,…): Puts together several lists returning a new list with several tuples.

·      The sorted(object1) and reversed(object1) functions do the same as reverse and sort but return a value instead of just changing the object.

Dictionaries

·         Dictionaries are declared in curley {} braces, in the form {KEY:VALUE,KEY:VALUE} and can be addressed using the key using dictionary1[KEY], this can also be used to input entries to the dictionary using dictionary1[NEWKEY]  = VALUE.

·         dictionary1.clear(): Clears contents of a source dictionary, all variables referring to dictionary1 become empty dictionaries rather than still referring to the source as with x={}.

·         dictionary1.copy(): Returns exact copy of dictionary1.

·         dictionary1.get(KEY1, [object1]): Returns value of key1 if key1 exists, returns object1 if not. If object1 is not specified, default is None.

·         dictionary1.setdefault(KEY1, [object1]): Returns value of key1 if key1 exists, returns object1 if not. If object1 is not specified, default is None. Also creates entries into dictionary1 if key doesn’t exist using key1 and object1 as key and value respectively.

·         dictionary1.has_key(KEY1): Returns True or False value for whether dictionary1 holds KEY1.

·         dictionary1.items(): Returns each key and value in dictionary1 held in a separate tuple, and all tuples held in a list. Use .iteritems() for an iterator version. Use .keys() and .iterkeys() for keys only. Use .values() and .itervalues() for values only.

·         dictionary1.pop(KEY1): Deletes key and value of key1 and returns it as a tuple.

·         dictionary1.popitem(): Deletes leftmost entry in dictionary1 and returns it. (As I understand)

·         dictionary1.update(dictionary2): updates dictionary1 with values from identical keys in dictionary2.

 

Abstraction

Functions

·         Functions are defined using def *NAME(parameter1,parameter2). They don’t have to return a value (in Python only), to return a value use return *VALUE. to simply break of out the function, use the return statement on its own.

·         You can specify the parameter name of a certain parameter by usi...

Zgłoś jeśli naruszono regulamin