Python Programming Language
Python Programming Language
Python Overview
Python Data Types
Python Control Structures
Python Input\output
Python Functions
Python File Handling
Python Exception Handling
Python is a high-level programming language which is:
Tuples are faster than lists and protect your data against accidental changes
to these data.
The rules for tuple indices are the same as for lists and they have the same
operations, functions as well.
To write a tuple containing a single value, you have to include a comma, even
though there is only one value. e.g. t = (3, )
Python's dictionaries are kind of hash table type which consist of key-value
pairs of unordered elements.
• Keys : must be immutable data types ,usually numbers or strings.
Python Dictionaries are mutable objects that can change their values.
Dictionary‟s values can be assigned and accessed using square braces ([])
with a key to obtain its value.
Read Lines:
• You can return one line by using the readline() method.
Close Files
• Close the file when you are finish with it
Delete a File : To delete a file, you must import the OS module, and run its os.remove() function:
import os
os.remove("demofile.txt")
Check if File exist
To avoid getting an error, you might want to check if the file exists before you try to delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Delete Folder
To delete an entire folder, use the os.rmdir() method:
import os
os.rmdir("myfolder")
Note: You can only remove empty folders.
The try block lets you test a block of code for errors.
The finally block lets you execute code, regardless of the result of the try-
and except blocks.
When an error occurs, or exception as we call it, Python will normally stop
and generate an error message.