Advance Python
Unlocking the Power of Advanced Python Concepts: A Quick and Easy Guide
Introduction
Hello, and welcome to my blog! My name is Rohit Pimpale, and I'm a second-year IT student passionate about machine learning. I've been documenting my MLops journey through blogs, and today I want to share some of my experiences with advanced Python.
File Handling
So far we have been opening files manually. Now Let's explore how we can open files programmatically.
First Let's know your current directory.
pwd #reviewing your file path
code for opening and closing file
f = open("practice.txt",'+w')
f.write("this is practice file")
f.close()
OS Module
Below are useful os module code
import os
cwd = os.getcwd()
# In your current directory
os.listdir()
#Create a directory
os.mkdir(new_directory)
#Deleting a file
os.remove(file_path)
Shutil Module
The Shutil module is used to move files to different locations.
import shutil
os.chdir('/path/to/desired/directory')
Deleting File
All of these methods can not be reversed so be careful using send2trash module
import send2trash
send2trash.send2trash('practice.txt')
Exception Handling
We use Exception handling when we error is going occur then we state how to handle this error in simple words
try: Run this code except: Execute the code when there is as exception. else: No exception? Run this code finally: Always run this code
def askint():
while True:
try:
val = int(input("Please enter an integer: "))
except:
print("Looks like you did not enter an integer!")
continue
else:
print("Yep that's an integer!")
print(val)
break
finally:
print("Finally, I executed!")
Regular Expression
You might have heard about Regex term which is nothing but a regular expression. Regex allows a user to search for strings using almost any sort of rule they can come up with. So there is text and pattern whenever you want to find or search for these patterns in the text we use regex.
Identifiers for Characters in Patterns
Examples
Functional Programming
Map Function
Map function allows you to "map" a function to an iterable object. Just as how Google map points out to a certain destination to another destination similar to it in map function points list and function.
def square(num):
return num**2
num_list = [2,5,8,9,16]
list(map(square,num_list)
>>>out: [4, 25, 64, 81, 256]
Filter Function
In the filter function, it filter you will get back only the results that would return True when passed to the function.
def prime_number(num):
if num > 1:
# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
pass
break
else:
return num
else:
pass
num_list = [10,11,17,25,35,41,57]
list(filter(prime_number,num_list))
>>>out: [11, 17, 41]
Lambda Expression
The most important, useful expression every interviewer will always ask you what is lambda if you are Python programmer. Lambda is quickly make ad-hoc functions without needing to properly define a function using def.
square = lambda num :num**2
square(16)
>>>Out: 256
#you can use lambda function in map and filter
num_list = [10,11,17,25,35,41,57]
list(map(lambda num : num % 5,num_list))
>>>Out: [0, 1, 2, 0, 0, 1, 2]
Conclusion
In this guide, we have covered some advanced concepts in Python that are essential for any developer to master. By understanding file handling, exception handling, regular expressions, and programming functions in Python, you can write more sophisticated and efficient code.
If you have any questions or want to connect with me, feel free to reach out to me on my LinkedIn profile or on Twitter. I'm always happy to chat about programming and help fellow developers!
Remember, learning and improving your Python skills takes time and dedication, but with the right resources and practice, anyone can become a proficient Python developer.