1. Rename multiple files
import os
files=["photo1.png","photo2.png","photo3.png"] for i ,f in enumerate(files,start=1): new_name=f"image_{i}.png" print(f"Renamed{f} {new_name}")
Output:
Renamedphoto1.png image_1.png
Renamedphoto2.png image_2.png
Renamedphoto3.png image_3.png
2. Auto Summarize a CSV File
import pandas as pd
data=pd.DataFrame({
'Name':['Alice','Bob','Charlie'],
'Score':[90,85,95],
'Age':[23,25,22]
})
display(data.describe())
Output:
Score Age count 3.0 3.000000 mean 90.0 23.333333 std 5.0 1.527525 min 85.0 22.000000 25% 87.5 22.500000 50% 90.0 23.000000 75% 92.5 24.000000 max 95.0 25.000000
3. Remove duplicate Entries
import pandas as pd
df=pd.DataFrame({
'Name':['Alice','Bob','Alice','David'],
'Score':[90,85,90,88]
})
df_clean=df.drop_duplicates()
display(df_clean)
Output:
Name Score 0 Alice 90 1 Bob 85 3 David 88
4. Display Current time and date automatically
from datetime import datetime
now=datetime.now()
print("Current Date & Time:" , now.strftime("%Y-%m-%d %H:%M:%S"))
Output:
Current Date & Time: 2025-11-04 22:25:22
5. Convert text to pdf
from fpdf import FPDF
pdf=FPDF()
pdf.add_page()
pdf.set_font("Arial",size=12)
pdf.cell(200,10,txt="hello from python",ln=True,align='C')
pdf.output("note.pdf")
print("Pdf saved as note.pdf")
Output:
Pdf saved as note.pdf
6. Search for a word in multiple text files
import glob
keyword="Python"
for file in glob.glob("*.txt"):
with open(file) as f:
if keyword in f.read():
print(f"'{keyword}' found in {file}")
Output:
'Python' found in daily_log.txt
'Python' found in destination_file.txt
7. Generate a random password
import string,random
chars=string.ascii_letters+string.digits+string.punctuation
password=''.join(random.sample(chars,10))
print("Generated password:",password)
Output:
Generated password: U:{t*k,JzK


0 Comments:
Post a Comment