






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
6.0 FUNCTIONS AND CHARACTERS USED IN PYTHON PROGRAMMING LANGUAGE
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1. %d Converts a signed integer decimal 2. %s Converts String (converts any Python object using str()) 3. %r String (converts any Python object using repr()) 4. \n moves whatever's after it to a new ling 5. """ to display things as they're written in the .txt file 6. {}.format(something) Is the new string.format in Python 3. This is how indexing works: "My first name is {0} and my last name is {1}. You can call me {0}".format("John","Doe"). 7. list_name.append("") appends thing to lists 8. for loops "for x in list_name" ... applies something to every item in a list 9. my_list.sort() sorts a list from lowest to highest, or alphabetical 10.len(my_list) finds the length of a list 11.len[2] = 3 The 2nd term of the list is now equal to 3 12.my_list.insert(4, "cat") Inserts the string "cat" at the 4th position in a list 13.my_list[:] gives you the entire list 14.my_list[0:] gives you the whole list, starting at the 0 position 15.my_list[1:3] gives you the list starting at the 1st position and ending at the 2nd position 16.my_list[-1] gives you the last term in the list 17.%r is used for... debugging and display 18.%s is used for display
19.optparse first command import optparse parser = optparse.OptionParser 20.adding parser options parser.add_option('-n, '--new') 21.after we've added options (in parser) (options, args) = parser.parse_args() 22.github steps
51.argv
53.input() Assumes input is valid python expression, returns evaluated result 54.read()
Convert a string to an integer int(raw_input(> )) x += y ADD AND x = x + y
exponent 5 % 3 Modulo; remainder of the division 5 % 3 = 2 (3 goes into 5 once, remainder 2) 62.split() Method splits a string into separate phrases
84.elif stands for else if. if the first test evaluates to False, continues with the next one 85.else optional. Used after elif to catch other cases not provided for. 86.is tests for object identity 87.not negates a boolean value 88.as if we want to give a module a different alias 89.from for importing a specific variable, class or a function from a module 90.def used to create a new user defined function 91.return exits the function and returns a value 92.lambda creates a new anonymous function 93.global access variables defined outside functions 94.try specifies exception handlers 95.except catches the exception and executes codes 96.finally is always executed in the end. Used to clean up resources. 97.raise create a user defined exception 98.del deletes objects 99.pass does nothing
100. assert used for debugging purposes 101. class - way of producing objects with similar attributes and methods. - used to create new user defined objects - an object is an instance of a class 102. exec executes Python code dynamically
103. yield is used with generators 104. \ backslash () 105. ' Single Quote (') 106. " Double-quote (") 107. \a ASCII Bell - may cause receiving device to emit a bell or warning of some kind 108. \b ASCII Backspace (BS) - Erases last character printed 109. \f ASCII FormFeed (FF) - ASCII Control character. Forces printer to eject current page and continue printing at top of another. 110. \n ASCII LineFeed (LF) - Goes to next line - newline escape 111. \r ASCII Carriage Return (CR) - Resets position to beginning of a line of text 112. \t ASCII Horizontal Tab (TAB) - 8 horizontal spaces; tab 113. \v ASCII Vertical Tab (VT) - 6 vertical lines; 1 inch 114. %d signed integer decimal 115. %i signed integer decimal 116. %o unsigned octal 117. %u unsigned decimal 118. %x Unsigned hexadecimal (lowercase)
Exponent AND. Performs exponential calculation on operators and assigns value to left operand. A* =B ~ A = A *B
135. items() Returns a list of a dict's tuple pairs (key, value) 136. get() Returns a value for the given key. If key is not available, returns default of 'none'. 137. function argument passed in for function parameter function(argument) 138. function parameter Variable name for passed in argment def function(parameter): 139. universal import Imports all functions and variables from a module - Can cause conflicts with user defined functions and vars - Better to import only necessary functions from module import * 140. sort() sorts a list from smallest to greatest "mutable" can be changed after created del keyword deletes key/value pairs from dict 141. .remove() removes items from list 142. range() Returns a list of numbers from start up to (but not including) stop start defaults to 0 and step defaults to 1 range(stop) range(start, stop) range(start, stop, step) Syntax to index 2 nested lists? list[x][y] 143. enumerate() gives an index number to each element in a list
144. zip() Combines two or 3 lists to return all values in for loops tuple An immutable sequence of Python objects - Immutable; can't be changed - Similar to list, but can't be modified - Uses (), ends in ; tuple1 = ('word', 1, False); 145. items() Returns an array of dict key/value pairs 146. keys() Returns an array of dict's keys 147. values() Returns an array of dict's values 148. list comprehension Python rules for creating lists intelligently s = [x for x in range(1:51) if x%2 == 0] [2, 4, 6, 8, 10, 12, 14, 16, etc] 149. list slicing Way to access elements list[start:end:stride] - stride = count by __'s - any term can be omitted, will be set to default - a negative stride progresses through list backwards 150. filter() - filters a list for terms that make the function true filter(function, list) filter(lambda x: x%3 ==0, my_list) - for anonymous (throwaway) functions 151. % Modulus is NOT used as a "percentage" sign in the programming language 152. %d, %i Signed integer decimal 153. %o Signed octal value 154. %x Signed hexidecimal