The document provides an overview of Python methods related to dictionaries, strings, and file handling, including their syntax and examples of usage. It covers various methods for manipulating dictionaries and strings, such as accessing keys, values, and performing string operations like conversion and checking characteristics. Additionally, it outlines file and directory operations using the os module, detailing methods for handling files and directories effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views16 pages
Python Assi
The document provides an overview of Python methods related to dictionaries, strings, and file handling, including their syntax and examples of usage. It covers various methods for manipulating dictionaries and strings, such as accessing keys, values, and performing string operations like conversion and checking characteristics. Additionally, it outlines file and directory operations using the os module, detailing methods for handling files and directories effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
Python assignment
(Methods of (dictionaries ,strings and file handling), their operation, syntax,
example)
1) Dictionaries: -
Method syntax working example
name Keys dict. keys() Here we access a method View object of the keys in the format of Dict_keys([k1,k2,k3] )
Values dict.values() Here we again
method access the View object of the values in the format of dict_values([v1,v2,v 3])
Items dict.items() Here we again
method access the View object of the items in the format of dict _values([i1,i2,i 3]) Set dict.setdefaul In this method in default t() particular let’s method say we are to fit a new item into the dictionary that does not exist we may do it in this manner: Get dict.get() A = {i1,i2} method If i3 does not exist we, do it like a[‘name’] = value but if the same thing is to be done and you don’t want the value of item to change then use “set default” method which doesn’t change the value for a successive attempt of set default attempt.
2) Strings: -
Method syntax working example
name
Lower str.lower() This
method converts the string to lower case that is all alphabets in the string get converted to their correspondi ng lower case this is applicable for alphabetic strings as well as alphanumeri c strings and not for numeric strings.
Upper str.upper() This converts
method the string to upper case that is all alphabets in the string get converted to their corresponding upper case this is applicable for alphabetic strings as well as alphanumeric strings and not for numeric strings. Is lower str.islower() Here this method method is used to check whether the given string is a lower case string or not by returning Boolean values such as true or false and this also is applicable for alphanumeric and alphabetic strings.
Is upper str.isupper() Here this
method method is used to check whether the given string is a upper case string or not by returning Boolean values such as true or false and this also is applicable for alphanumeric and alphabetic strings.
Is alpha str.isalpha() This is the
method method used to check whether the string assigned is pure alphabetic meaning only alphabet’s caps or lower case doesn’t matter and returns a Boolean value true or false
Is alnum str.isalnum() This checks
method whether the given string is a mixture of alphabets and numbers or not and returns a Boolean value true or false.
Is str.isdecimal() This is meant
decimal to check method whether the string is a possible combination of digits from 0 to 9
Is space str.isspace() This method
method is meant to check whether the string is a space or not or Is title str.istitle() Checks method whether the string if single the beginning character is capitalized or not if a combination of string like sentence where the alphabetic substrings are separated by space then whether the beginning letter of every alphabetic substring is capitalised or not by returning a Boolean value like true or false.
Starts str.startswith() This method
with is built to method check whether the string starts with That particular substring or not by returning a true or false Boolean value. Ends str.endswith() This method with is similar to method the above one its to check or confirm whether the string has that that particular ending substring or not by returning a true or false Boolean value.
Split str.split() This particular
method method is to create a list of strings where this list of strings are formed by splitting the main string str by deleting that particular character . Join Str.join() Str join method creates a string out of character’s present in collection datatype like list or tuple where this a meaning of indexing and not data types like set here the elements don’t have indexing because they are unordered. Though they don’t give error the result is not useful or desired one. Rjust str.rjust() and In this method module we str.ljust() and ljust observe that method the string is the string is shifted to the right extreme that is the right quote or left extreme or left quote by specifying the number of places as integer. Centre str.centre() This method method always aligns the string u specify about or almost in the centre along with some character’s such that this is equal to the length of the final string specified.
The lstrip str.lstrip() in this
method we Method. can remove substrings from the left of the main string and return a new string because this string is a immutable datatype it also includes any escape sequence character’s also.
The str.rstrip() in this
rstrip method we method can remove substrings from the right of the main string and return a new string because this string is a immutable datatype it also includes any sequence character’s also.
Strip Str.strip() in this
method. method we can remove substrings from either the left of the main string or the right of the main string and return a new string because this string is a immutable data type this also includes any escape sequence character as well as white spaces.
Pyper Import pyper The pyperclip
clip clip module has method copy() and paste() pyperclip.copy functions that () can send text to and receive text Pyperclip.past from your e computer’s clipboard. () 3)File or directory operations / file or directory handling operations: -
I. Folder handling or folder related operations (o.s module operations
after importing os): -
Method Syntax working example
name
The os.getcwd() This method
getcwd is used to method get the current working directory meaning in which folder is this file currently runing
The chdir os.chdir() This method
method is used to change the directory from the current working directory to other
The os.makedirs() Hwre we
makedir use this method. module to create a fresh directory having freshly created folders interconnect ed to each other
The listdir os.listdir() This gives a
method list of files which is mixture of files from all the directories in the file path
O s.path module methods
The os.path.abspath This method
abspath () is meant to method convert the relative path into the absolute path
The is abs os.path.isabs() This method
path returns a method Boolean value true or false whether the [path is absolute or not
The rel os.path.relpath() This method
path is meant to method convert the absolute path into the relative path The base os.path.basena This tells the name me () basename method which is the file name.
The dir os.path.dirname This tells the
name () folder or method directory name which is basically the actual path.
The path os.path.split() This
split basically method going to split the path at “\\” and returns the list of folders and files.
The os.path.getsize This gets
getsize () the size of method the file in the path in bytes
The exist os.path.exists () This method
method is used to check wether the path is valid or whether it exists in the frst place. The is dir os.path.isdir () will return method True if the path argument exists and is a folder and will return False otherwise.
The is file os.path.isfile () will return
method True if the path argument exists and is a file and will return False otherwise.
Path os.path.join () It joins a list
joining of folders method and file by “\ or path \” to give constructin the path of g method the file.
II. File handling operations: -
Method Syntax working example
name
Open open() This
method opens a file and transfers the contents to a object or variable Read fh.read() called as method file handle or file object
Readline fh.readline() Returns a
method line form the file its repitative the more you use it more line you get from the file
Readlines fh.readlines() Returns a
method list of lines from the given text file.
Write fh.write() This is
method used to write a string on to a file
Write fh.writelines( This Is
lines ) takes a method list of strings as an argument and writes those strings onto a file. Close fh.close() This is method used to close a file to save the changes you have made.