Posts

Posts

Try Except, Raise an exception, String Formatting in Python 3.7

  Python  Try Except:- The  try  block lets you test a block of code for errors. The  except  block lets you handle the error. The  finally  block lets you execute code, regardless of the result of the try- and except blocks. Exception Handling When an error occurs, or exception as we call it, Python will normally stop and generate an error message. These exceptions can be handled using the  try  statement: Example The  try  block will generate an exception, because  x  is not defined: try :    print (x) except :    print ( "An exception occurred" ) Since the try block raises an error, the except block will be executed. Without the try block, the program will crash and raise an error: Example This statement will raise an error, because  x  is not defined: print (x) Many Exceptions You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error: Example Print one message if the try block raises a  NameError  

Python PIP

  Python  PIP:- What is PIP? PIP is a package manager for Python packages, or modules if you like. Note:  If you have Python version 3.4 or later, PIP is included by default. What is a Package? A package contains all the files you need for a module. Modules are Python code libraries you can include in your project. Check if PIP is Installed Navigate your command line to the location of Python's script directory, and type the following: Example Check PIP version: C:\Users\ Your Name \AppData\Local\Programs\Python\Python36-32\Scripts>pip --version Install PIP If you do not have PIP installed, you can download and install it from this page:   https://pypi.org/project/pip/ . Download a Package Downloading a package is very easy. Open the command line interface and tell PIP to download the package you want. Navigate your command line to the location of Python's script directory, and type the following: Example Download a package named "camelcase": C:\Users\ Your Name \A

findall, search, split, sub Function in Python 3.7

  The findall() Function The  findall()  function returns a list containing all matches. Example Print a list of all matches: import  re txt =  "The rain in Spain" x = re.findall( "ai" ,  txt) print (x) The list contains the matches in the order they are found. If no matches are found, an empty list is returned: Example Return an empty list if no match was found: import  re txt =  "The rain in Spain" x = re.findall( "Portugal" ,  txt) print (x) The search() Function The  search()  function searches the string for a match, and returns a  Match object  if there is a match. If there is more than one match, only the first occurrence of the match will be returned: Example Search for the first white-space character in the string: import  re txt =  "The rain in Spain" x = re.search( "\s" ,  txt) print ( "The first white-space character is located in position:" , x.start()) If no matches are found, the value  None  is return

Metacharacters, Special Sequences, Sets in Python 3.7

  Metacharacters Metacharacters are characters with a special meaning: Character Description Example [] A set of characters "[a-m]" \ Signals a special sequence (can also be used to escape special characters) "\d" . Any character (except newline character) "he..o" ^ Starts with "^hello" $ Ends with "world$" * Zero or more occurrences "aix*" + One or more occurrences "aix+" {} Exactly the specified number of occurrences "al{2}" | Either or "falls|stays" () Capture and group     Special Sequences A special sequence is a  \  followed by one of the characters in the list below, and has a special meaning: Character Description Example \A Returns a match if the specified characters are at the beginning of the string "\AThe" \b Returns a match where the specified characters are at the beginning or at the end of a word (the "r" in the beginning is making sure that the string is being t