jagomart
digital resources
picture1_Python Pdf 186104 | Python2 Cheat Sheet


 142x       Filetype PDF       File size 0.87 MB       Source: derechodelared.com


File: Python Pdf 186104 | Python2 Cheat Sheet
converting data types string operations python 2 7 essentials useful string functions convert anything to a string newstring str make lowercase a lower a pocket reference guide newstring str 100 ...

icon picture PDF Filetype PDF | Posted on 01 Feb 2023 | 2 years ago
Partial capture of text on file.
 
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 
                                                                                                                                                                                                                                               Converting Data Types 
                                                                String Operations                                                                                                                                                                                                                                                                                                                                                                                                         Python  2.7 
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            Essentials 
 
               Useful String functions:                                                                                                                                                              Convert anything to a string: 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 
                                                                                                                                                                                                     newstring = str() 
               Make lowercase:                                                                                       "A".lower()="a" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   POCKET REFERENCE GUIDE  
 
                                                                                                                                                                                                     newstring = str(100) 
               Make UPPERCASE :                                                                                      "a".upper()="A" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            SANS Institute 
 
                                                                                                                                                                                                      
               Make Title Format:        "hi world".title()="Hi World"                                                                                                                                                                                                                                                                                                                                                                                                                                            
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                www.sans.org/sec573 
                                                                                                                                                                                                     Convert String to Integer: 
               Replace a substring:      "123".replace('2','z')= "1z3" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   http://isc.sans.edu 
 
                                                                                                                                                                                                     newint = int([,base]) 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   Mark Baggett  Twitter: @markbaggett 
               Count occurrences of substring:"1123".count("1")=2 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 
                                                                                                                                                                                                     All of the following assign the 
               Get offset of substring in string:  "123".index("2")=1 
 
                                                                                                                                                                                                     variable ten the integer 10  
               Detect substring in string:       “is” in “fish”  == True 
 
                                                                                                                                                                                                                                                                                                                                                                                                                          3 Methods of Python Execution 
                                                                                                                                                                                                     >>> ten = int(“1010”,2) 
               Encode "a string":    "a string".encode(codec name) 
 
                                                                                                                                                                                                     >>> ten = int(“0010”) 
               Decode a string:       "a string".decode(codec name) 
 
                                                                                                                                                                                                                                                                                                                                                                                             Command line Execution with -c: 
                                                                                                                                                                                                     >>> ten = int(“000A”,16) 
               Example with the ROT13 codec: 
                                                                                                                                                                                                                                                                                                                                                                                             # python –c [“script string”]   
                                                                                                                                                                                                      
               >>> print "RAPBQR-ZR".decode("rot13") 
                                                                                                                                                                                                                                                                                                                                                                                             python –c "print 'Hello World!'"  
                                                                                                                                                                                                     Convert Float to Integer by dropping decimal: 
               ENCODE-ME 
                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                     newint = int() 
                
 
                                                                                                                                                                                                                                                                                                                                                                                             Python Interpreter Script Execution: 
                                                                                                                                                                                                     >>> print int(3.1415) 
               Some String encoders/decoder codec names: 
                                                                                                                                                                                                                                                                                                                                                                                             # cat helloworld.py 
                                                                                                                                                                                                     3 
               Base64, bz2 , hex, rot13, uu, zip, string_escape 
                                                                                                                                                                                                                                                                                                                                                                                             print "Hello World" 
                                                                                                                                                                                                     >>> int(3.6) 
                                                                                                                                                                                                                                                                                                                                                                                             # python helloworld.py 
                                                                                                                                                                                                     3 
               Convert a string to a list (default separator=space):                                                                                                                                                                                                                                                                                                                         Hello World 
                                                                                                                                                                                                      
               newlist = astr.split()                                                                                                                                                                                                                                                                                                                                              
 
                                                                                                                                                                                                     Convert String or number to Float: 
               >>> print "A B C".split() 
                                                                                                                                                                                                                                                                                                                                                                                             Python Interactive Shell: 
                                                                                                                                                                                                     afloat = float() 
               ['A', 'B', 'C']                                                                                                                                                                                                                                                                                                                                                               # python 
 
                                                                                                                                                                                                     >>> print float("1.5") 
               >>> print "A B     C".split()                                                                                                                                                                                                                                                                                                                                                 >>> print "Hello World" 
 
                                                                                                                                                                                                     1.5 
               ['A', 'B', 'C']                                                                                                                                                                                                                                                                                                                                                               Hello World 
 
                                                                                                                                                                                                     >>> print float(1) 
               >>> print "A,B,  ,C".split(",") 
 
                                                                                                                                                                                                     1.0 
               ['A', 'B', '  ', 'C'] 
 
                                                                                                                                                                                                      
               >>> print "WANNA BANANA?".split("AN") 
 
                                                                                                                                                                                                     Convert String Character to ASCII decimal: 
               ['W', 'NA B', '', 'A?'] 
                                                                                                                                                                                                                                                                                                                                                                                                                           Python Command Line Options 
                                                                                                                                                                                                     newint = ord() 
                
 
                                                                                                                                                                                                     >>> print ord("A") 
                                                                                                                                                                                                                                                                                                                                                                                               # python –c “script as string” 
               Convert a list (or other iterable object) to a string: 
                                                                                                                                                                                                     65 
               Join a list together putting string between elements.                                                                                                                                                                                                                                                                                                                                               Execute a string containing a script 
                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                               # python –m  [module args]  
 
               “astring”.join([list]) 
                                                                                                                                                                                                     Convert ASCII decimal to String of length 1: 
               >>> print "".join(['A','B','C'])                                                                                                                                                                                                                                                                                                                                                                    Find module in path and execute as a script 
                                                                                                                                                                                                     newstr = chr() 
               ABC                                                                                                                                                                                                                                                                                                                                                                                                 Example: python –m “SimpleHTTPServer” 
                                                                                                                                                                                                     >>> print chr(65) 
               >>> print ",".join(['A','B','C'])                                                                                                                                                                                                                                                                                                                                               # python –i  
                                                                                                                                                                                                     A 
               A,B,C 
                                                                                                                                                                                                                                                                                                                                                                                                                   Drop to interactive shell after script execution 
                
                                                                                                                                                                                                                                                                                                                                                                                                                    
 
 
 
 
 
 
                                                                                                                                                                        SEC573 PyWars Essentials 
                     Loops Lists & Dictionaries                                                             Misc 
 
 
                                                                                                                                                            Create pyWars Object 
      List essentials:                                                          Adding Comments to code: 
 
                                                                                                                                                            >>> import pyWars 
                                                                                #Comments begin the line with a pound sign  
        Create an empty list:                            newlist=[] 
 
                                                                                                                                                            >>> game= pyWars.exercise() 
                                                                                 
        Assign value at index:               alist[index]= value 
                                                                                                                                                           
                                                                                Defining Functions: 
        Access value at index                           alist[index] 
                                                                                                                                                            Account Mangement 
                                                                                Here is a function called “add”.  It accepts 2 arguments 
        Add item to list:                  alist.append(new item) 
                                                                                                                                                            >>> game.new_acct("username","password") 
                                                                                num1 and num2 and returns their sum. Calling “print 
 
        Insert into list:      alist.insert(at position, new item) 
                                                                                                                                                            >>> game.login("username","password") 
                                                                                add(5,5)”   will print “10” to the screen: 
 
        Count # of an item in list:           alist.count( item ) 
                                                                                                                                                            >>> game.logout() 
                                                                                 
 
        Delete 1 matching item:        alist.remove(del item) 
                                                                                                                                                             
                                                                                def add(num1, num2): 
 
        Remove item at index                   del alist[index]   
                                                                                                                                                            Query a question: 
                                                                                    #code blocks must be indented 
 
                                                                                                                                                            >>> game.question() 
                                                                                    #each space has meaning in python 
 
      Dictionary essentials:                                                                                                                                 
                                                                                    myresult = num1 + num2 
 
        Create an empty dict:                                dic={}                 return myresult                                                         Query the data: 
 
                                                                                                                                                            >>> game.data() 
        Initialize a non-empty dictionary:                                         
 
                                                                                                                                                             
                       dic = { “key1”:”value1”,”key2”:”value2”}                 if then else statements: 
 
                                                                                if :                                                          Submit an answer: 
        Assign a value:                        dic[“key”]=”value” 
 
                                                                                                                                                            >>> game.answer(, 
                                                                                    #code block here will execute  
        Determine if  key exists:           dic.has_key(“key”) 
 
                                                                                                                                                                solverfunc(game.data())) 
                                                                                    #when logic test 1 is True 
        Access value at key:        dic[“key”], dic.get(“key”) 
 
                                                                                elif : 
                                                                                                                                                             
        List of all keys:                                     dic.keys() 
                                                                                    #code block executes logic test 1 is  
                                                                                                                                                                           Logic and Math Operators 
                                                                                                                                                           
        List of all values:                                dic.values() 
                                                                                    #False and logic test 2 is True 
                                                                                                                                                           
        List of (key,value) tuples:                     dic.items()             else: 
 
                                                                                                                                                          Math Operator  Example                        X=7, Y=5 
                                                                                    #code block for else has no test and  
         
 
                                                                                                                                                          Addition              X  + Y                   12 
                                                                                    #executes when if an all elif are False 
      Looping examples: 
 
                                                                                                                                                          Subtraction           X  -  Y                  2 
        For loop 0 thru 9:                     for x in range(10): 
                                                                                           Slicing and Indexing Strings, Lists, etc 
                                                                                                                                                          Multiplication        X  * Y                  35 
        For loop 5 thru 10:                for x in range(5,11): 
                                                                                                                                                          Division              X  / Y                   1 
        For each char in a string:          for char in astring: 
                                                                                Slicing strings and lists: 
                                                                                                                                                          Division              X  / float(Y)            1.4 
 
        For items in list :                              for x in alist: 
                                                                                x[start:stop:step]    x=[4,8,9,3,0]     x=”48930” 
                                                                                                                                                          Exponent              X ** Y                  16807 
        For loop retrieving indexes and values in a list :  
                                                                                x[0]                  4                 ‘4’ 
 
                                                                                                                                                          Modulo                X % Y                   2 
                              for index,value in enumerate(alist): 
                                                                                x[2]                  9                 ‘9’ 
                                                                                                                
                                                                                                                                                          Logic Operator                                 
        For keys in a dict :                 for x in adict.keys(): 
                                                                                x[:3]                 [4,8,9]           ‘489’ 
 
        For items in dict :      for key,value in adict.items():                                                                                          Equality             X == Y                   False 
                                                                                x[3:]                 [3,0]             ‘30’ 
 
        while  do:                                                                                                                            Greater Than         X > Y                    False 
                                                                                x[:-2]                [4,8,9]           ‘489’ 
 
                                                                                                                                                          Less Than            X < Y                    True 
                                                                                x[::2]                [4,9,0]           ‘490’ 
 
      Loop Control statements (for and while): 
                                                                                                                                                          Less or Equal        X <= Y                   True 
                                                                                x[::-1]               [0,3,9,8,4]       ‘03984’ 
 
      Exit loop immediately                                   break 
                                                                                                                                                          Not Equal            X !=Y or X<>Y            True 
                                                                                len(x)                5                 5 
 
      Skip rest of loop and do loop again            continue 
                                                                                                                                                          Other Logical Operators:  AND, OR and NOT 
                                                                                sorted(x)             [0,3,4,8,9]       ['0', '3', '4', '8', '9'] 
 
         
                                                                                                                                                             
                                                                                 
                                                                                                                                                       
The words contained in this file might help you see if this file matches what you are looking for:

...Converting data types string operations python essentials useful functions convert anything to a newstring str make lowercase lower pocket reference guide uppercase upper sans institute title format hi world www org sec integer replace substring z http isc edu newint int mark baggett twitter markbaggett count occurrences of all the following assign get offset in index variable ten detect is fish true methods execution encode codec name decode command line with c example rot...

no reviews yet
Please Login to review.