Programming in Python Week-8 Content
Python Modules ● It is a file containing python definitions and statements. ● A module can define functions and variables. ● Grouping related code into module makes it easier to understand. ● The filename is the module name with suffix .py. ● The statements will be executed only once when the module is imported. ● Bunch of modules in a directory is a package.
import statement ● Can be used while importing a module. ● Can be used along with from to import specific items from a module.
Main function ● Not necessary in Python ● If the code file is run directly, __name__ is set to __main__ internally ● If the file is imported as a module, __name__ will be equal to module name instead of __main__ and thus, the code inside main block will not be executed.
In-built string functions ● lower() ● upper() ● capitalize() ● center(length, character) ● count(value, start, end) ● endswith(value, start, end) ● expandtabs(tabsize) ● find(value, start, end) ● format(value1, value2...) ● index(value, start, end) ● islanum() ● isdecimal() ● isidentifier() ● islower() ● isspace() ● istitle() ● isupper() ● join(iterable) ● lstrip(characters) ● partition(value) ● replace(oldvalue, newvalue, count) ● rfind(value, start, end) ● rindex(value, start, end) ● title() ● swapcase() ● splitlines()
Error Handling ● Two features to handle any unexpected exceptions: ○ Exception Handling ○ Assertions
Exception Handling ● When exceptions occur, Python will normally stop execution and generate an error message. ● This can be handled by using try-except-finally block. ● try - block that lets you test a part of the code for errors ● except - let’s you handle the exception ● finally - let’s you execute the code regardless of whether an error occurred or not. ● If the try block raises an error, the except block will be executed. ● You can define as many except blocks as you want along with one try block. ● else block can be used to execute code if no exceptions are raised. ● You can also raise exceptions manually.
Standard exceptions ● Exception ● ArithmeticError ● OverflowError ● ZeroDivisionError ● AssertionError ● ImportError ● KeyboardInterrupt ● SyntaxError ● IndentationError ● TypeError ● IndexError ● NameError ● KeyError
Assertions ● The assert keyword is used when debugging code. ● Assertions are conditions which are always supposed to be true. ● It lets you check if a condition in code is true. ● You can specify messages in case it’s false but the code execution will end.
Configuration files in Python ● Config files help in creating the initial settings for the file. ● Configparser module can help in creation of config files. ● In Python, the configuration files have an extension of .ini ● It’s an inbuilt module and will be downloaded with Python3. ● Consists of multiple sections distinguished by [section_name] ● Each section contains key-value pairs separated by : or = ● You can then write or read the configuration file in the other files
Working with Files in Python ● File is a contiguous set of bytes used to store data. ● File is composed of 3 parts - (Header, Data, EOF) ● For accessing any file, a file path is required - string that represents the location of the file. (Folder Path, File Name and File Extension)
path/to/cats.gif cats.gif
../../animals.csv
Closing & Opening a File ● If you want to work with a file, the first action needed is to open it using open() function. ● Closing a file after the work is done is essential or else the result could be unwanted behavior. ● 2 ways to ensure that the file is closed properly: ○ try-finally block ○ with block ● Using with block, you need to specify a second argument on how do you plan to use the file
A file can be opened in these 3 modes
Reading & Writing opened files ● .read(), .readline(), .readlines() can be used for reading content from a file. ● .write() and .writelines() can be used for writing content to a file.
Homework Questions

Programming in python - Week 7,8

  • 1.
  • 2.
    Python Modules ● Itis a file containing python definitions and statements. ● A module can define functions and variables. ● Grouping related code into module makes it easier to understand. ● The filename is the module name with suffix .py. ● The statements will be executed only once when the module is imported. ● Bunch of modules in a directory is a package.
  • 3.
    import statement ● Canbe used while importing a module. ● Can be used along with from to import specific items from a module.
  • 4.
    Main function ● Notnecessary in Python ● If the code file is run directly, __name__ is set to __main__ internally ● If the file is imported as a module, __name__ will be equal to module name instead of __main__ and thus, the code inside main block will not be executed.
  • 7.
    In-built string functions ●lower() ● upper() ● capitalize() ● center(length, character) ● count(value, start, end) ● endswith(value, start, end) ● expandtabs(tabsize) ● find(value, start, end) ● format(value1, value2...) ● index(value, start, end) ● islanum() ● isdecimal() ● isidentifier() ● islower() ● isspace() ● istitle() ● isupper() ● join(iterable) ● lstrip(characters) ● partition(value) ● replace(oldvalue, newvalue, count) ● rfind(value, start, end) ● rindex(value, start, end) ● title() ● swapcase() ● splitlines()
  • 8.
    Error Handling ● Twofeatures to handle any unexpected exceptions: ○ Exception Handling ○ Assertions
  • 9.
    Exception Handling ● Whenexceptions occur, Python will normally stop execution and generate an error message. ● This can be handled by using try-except-finally block. ● try - block that lets you test a part of the code for errors ● except - let’s you handle the exception ● finally - let’s you execute the code regardless of whether an error occurred or not. ● If the try block raises an error, the except block will be executed. ● You can define as many except blocks as you want along with one try block. ● else block can be used to execute code if no exceptions are raised. ● You can also raise exceptions manually.
  • 10.
    Standard exceptions ● Exception ●ArithmeticError ● OverflowError ● ZeroDivisionError ● AssertionError ● ImportError ● KeyboardInterrupt ● SyntaxError ● IndentationError ● TypeError ● IndexError ● NameError ● KeyError
  • 11.
    Assertions ● The assertkeyword is used when debugging code. ● Assertions are conditions which are always supposed to be true. ● It lets you check if a condition in code is true. ● You can specify messages in case it’s false but the code execution will end.
  • 12.
    Configuration files inPython ● Config files help in creating the initial settings for the file. ● Configparser module can help in creation of config files. ● In Python, the configuration files have an extension of .ini ● It’s an inbuilt module and will be downloaded with Python3. ● Consists of multiple sections distinguished by [section_name] ● Each section contains key-value pairs separated by : or = ● You can then write or read the configuration file in the other files
  • 13.
    Working with Filesin Python ● File is a contiguous set of bytes used to store data. ● File is composed of 3 parts - (Header, Data, EOF) ● For accessing any file, a file path is required - string that represents the location of the file. (Folder Path, File Name and File Extension)
  • 14.
  • 15.
  • 16.
    Closing & Openinga File ● If you want to work with a file, the first action needed is to open it using open() function. ● Closing a file after the work is done is essential or else the result could be unwanted behavior. ● 2 ways to ensure that the file is closed properly: ○ try-finally block ○ with block ● Using with block, you need to specify a second argument on how do you plan to use the file
  • 17.
    A file canbe opened in these 3 modes
  • 18.
    Reading & Writingopened files ● .read(), .readline(), .readlines() can be used for reading content from a file. ● .write() and .writelines() can be used for writing content to a file.
  • 21.