Pythpn Cheat
Pythpn Cheat
Pythpn Cheat
→Special character in regular expression. in Python are used to match Specfic pettern in
text data *There are dame of the commonly used Special characters in python regular
expression.. *1) ’^’ -- matches the start of a string. 2) $ -- matches the end of the string
(3)’.’matches any single character except newline(4) ‘*’matches zero or more Occurrences
of the preceding character.(5) ‘+’matches one or more Occurrences OF The preceding
character.6)’?’ - matches zero or one occurrences the preceding character..7) ':' - Escape a
special character, allowing it to be used as a regular character. 8)’ []’ matches any charact es
with in the brackets 9) ‘[^]’= matches any character not with in.the brackets. (10)"()" group
character together as a single Unit
These Special characters can be Used with other character & quantifiers By Using there
special Characters in Combination with alphanumeric characters, you can create complex
pattern... to match Specific strings.
3) Explain deadlocks in threads
→A Deadlock in threading Occur when two or more thread are blacked, waiting for each
other to release a resource, but none of them can proceed. Because they are all waiting for
another thread to release a Source. In other words, a deadlock. Occur when there is a
Circular dependency between the threads, & none of them can continue without releasing a
resource that is being held by another thread. To avoid deadlocks in Python, it's important
to Carefully design your code & to manage Shared resources correctly. The includes
ensuring that threads release resources they no longer need & never holds multiple
resources at the Same time you can also Use Synchronization. Primitive like locks &
Semaphores & Condition Variable to help prevent deadlock & ensures Safe access to shared
resources.
Q)write a program to explain the test () & seek ()methods
→Tell ()
Tell () returns the Current position of the file painter from the beginning of the File()
For eg:- "F = Open (" demofile.txt","r")
print (F teu())
Seek ()
If we want to move the File point es to another. Position, we can use the Seek ()method.
For eg:-
F = Open("demofile.txt", "r")
F. Seek (4)
print (f. readline (1)
Q)Explain the difference between readline () & readlines
→in Python readline () & readlines () are two method used for reading input from a file
1)readline ()
readline () is a method that read a single line from a file + return it as a string Each time
readline ()' is called, it reads the next line From the File. If the end of the File is reached.'
readline ()’ return an empty string
eg. with open("File.txt', 'r') as F:
line = f.readline()
while line:
print (line.())
line = F.rendline()
Here readline ()'is Used inside a loop to read. each line of the file & print it to the console.
2) readlines ()
readlines () is a method that read all lines. From a File & return them as a list From a File
&return them as a list of strings Each string in the list represent a line from. the File
Eg :-with Open ('File.txt', 'r') as F·
. lines =F.readlines ().
. For line in lines :
. print( Line strip ())