SlideShare a Scribd company logo
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.
Programming in python - Week 7,8
Programming in python - Week 7,8
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.
Programming in python - Week 7,8
Programming in python - Week 7,8
Homework Questions

More Related Content

What's hot (19)

PPT
JavaScript iteration
Charles Russell
 
PPTX
Control statements in java
Madishetty Prathibha
 
PDF
(3) c sharp introduction_basics_part_ii
Nico Ludwig
 
PPTX
A Proposal to Orchestrate Test Cases
Boni García
 
PDF
CS844 U1 Individual Project
ThienSi Le
 
PPTX
Python decision making_loops part7
Vishal Dutt
 
PDF
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
Andrzej Jóźwiak
 
PDF
Unit testing.pptx [repaired]
Mohammad Asmar
 
PDF
Introduction to golang
Manjitsing Valvi
 
PPT
03 conditions loops
Manzoor ALam
 
PPTX
Java loops for, while and do...while
Jayfee Ramos
 
PDF
Basic constructs i
Manjitsing Valvi
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
java in Aartificial intelligent by virat andodariya
viratandodariya
 
PDF
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
PDF
Write unit test from scratch
Wen-Shih Chao
 
PPTX
Jsp session 11
Anuj Singh Rajput
 
ODP
Php day2010
Giorgio Sironi
 
PPTX
study of java
Prashant Mishra
 
JavaScript iteration
Charles Russell
 
Control statements in java
Madishetty Prathibha
 
(3) c sharp introduction_basics_part_ii
Nico Ludwig
 
A Proposal to Orchestrate Test Cases
Boni García
 
CS844 U1 Individual Project
ThienSi Le
 
Python decision making_loops part7
Vishal Dutt
 
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
Andrzej Jóźwiak
 
Unit testing.pptx [repaired]
Mohammad Asmar
 
Introduction to golang
Manjitsing Valvi
 
03 conditions loops
Manzoor ALam
 
Java loops for, while and do...while
Jayfee Ramos
 
Basic constructs i
Manjitsing Valvi
 
Java Decision Control
Jayfee Ramos
 
java in Aartificial intelligent by virat andodariya
viratandodariya
 
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Write unit test from scratch
Wen-Shih Chao
 
Jsp session 11
Anuj Singh Rajput
 
Php day2010
Giorgio Sironi
 
study of java
Prashant Mishra
 

Similar to Programming in python - Week 7,8 (20)

PPTX
Python files creation read,write Unit 5.pptx
shakthi10
 
PPTX
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
PPTX
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
PPTX
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
PPT
Python File functions
keerthanakommera1
 
PDF
GE3151_PSPP_UNIT_5_Notes
Guru Nanak Technical Institutions
 
PDF
Python_AdvancedUnit - 3.pdf about the python
gpsign134
 
PPT
Chapter01_Python.ppt
PigPug1
 
PPTX
what are python basics.pptx.Join Python training in Chandigarh
asmeerana605
 
PPTX
Introduction to Python for Security Professionals
Andrew McNicol
 
PPTX
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
PDF
First Steps in Python Programming
Dozie Agbo
 
PPTX
01 file handling for class use class pptx
PreeTVithule1
 
PPTX
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
PPT
Python for Engineers and Architects Stud
RaviRamachandraR
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
ODP
Introduction to Python - Training for Kids
Aimee Maree
 
PDF
Pythonintro
Hardik Malhotra
 
PDF
Python Programming Course Presentations
DreamerInfotech
 
PPTX
pythonforaipythonforaipythonforaipython.pptx
mlakshmi28
 
Python files creation read,write Unit 5.pptx
shakthi10
 
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
Python File functions
keerthanakommera1
 
GE3151_PSPP_UNIT_5_Notes
Guru Nanak Technical Institutions
 
Python_AdvancedUnit - 3.pdf about the python
gpsign134
 
Chapter01_Python.ppt
PigPug1
 
what are python basics.pptx.Join Python training in Chandigarh
asmeerana605
 
Introduction to Python for Security Professionals
Andrew McNicol
 
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
First Steps in Python Programming
Dozie Agbo
 
01 file handling for class use class pptx
PreeTVithule1
 
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Python for Engineers and Architects Stud
RaviRamachandraR
 
Introduction to Python - Training for Kids
Aimee Maree
 
Pythonintro
Hardik Malhotra
 
Python Programming Course Presentations
DreamerInfotech
 
pythonforaipythonforaipythonforaipython.pptx
mlakshmi28
 
Ad

More from Priya Nayak (6)

PPTX
Programming in python w6
Priya Nayak
 
PPTX
Programming in python - Week 5
Priya Nayak
 
PPTX
Programming in python - Week 3
Priya Nayak
 
PPTX
Programming in python - Week 2
Priya Nayak
 
PPTX
Programming in python
Priya Nayak
 
PPTX
Git and GitHub
Priya Nayak
 
Programming in python w6
Priya Nayak
 
Programming in python - Week 5
Priya Nayak
 
Programming in python - Week 3
Priya Nayak
 
Programming in python - Week 2
Priya Nayak
 
Programming in python
Priya Nayak
 
Git and GitHub
Priya Nayak
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 

Programming in python - Week 7,8

  • 2. 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.
  • 3. import statement ● Can be used while importing a module. ● Can be used along with from to import specific items from a module.
  • 4. 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.
  • 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 ● Two features to handle any unexpected exceptions: ○ Exception Handling ○ Assertions
  • 9. 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.
  • 10. Standard exceptions ● Exception ● ArithmeticError ● OverflowError ● ZeroDivisionError ● AssertionError ● ImportError ● KeyboardInterrupt ● SyntaxError ● IndentationError ● TypeError ● IndexError ● NameError ● KeyError
  • 11. 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.
  • 12. 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
  • 13. 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)
  • 16. 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
  • 17. A file can be opened in these 3 modes
  • 18. 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.