python
A programming language. Using elpy as IDE in Emacs . Beancount has been written in python and i learnt python specifically to use this accounting software.
Initially i used Elpy as IDE in Emacs . Later decided to use inbuilt python mode.
For intial learning, i used Byte of Python book.
After that i used the https://programming-22.mooc.fi/↗ for learning. Could not finish the curriculum, particularly the later stages after the basics. Kept referring to AI tools, finally aborted.
Now on Monday 2025-10-27, starting again to learn.
In the recent attempt, i tried to follow book py4e. here are sample programs and concepts from that book.
Handling directory locations
import os
import sys
# Print the directory where Python is looking for files (the CWD)
print("Current Working Directory:", os.getcwd())
# Get the directory of the current script file
# __file__ is the full path of the script, os.path.dirname gets its directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Change the Current Working Directory to the script's directory
os.chdir(script_dir)
print("New Current Working Directory:", os.getcwd())
# Now it should be /home/prabu/py/py4e
name = input("Enter file: ")
try:
# Python will now look for 'wordlist' inside /home/prabu/py/py4e/
handle = open(name, 'r')
# ... rest of your code
except FileNotFoundError:
print(f"Error: Could not find '{name}' in {os.getcwd()}")
sys.exit(1)
Error Handling
Always use focused and specific error handling for risky operations inside try..except blocks. Program logic that is generally safe can run outside of try blocks.
try:
# 1. Risky operation: File I/O
with open('config.txt', 'r') as f:
config_data = f.read()
except FileNotFoundError:
print("Error: Configuration file not found. Using defaults.")
config_data = "DEFAULT"
except PermissionError:
print("Error: Cannot read the configuration file due to permissions.")
config_data = "DEFAULT"
try:
# 2. Risky operation: Network access
response = requests.get(URL)
response.raise_for_status() # Raise HTTPError for bad responses
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
except requests.exceptions.ConnectionError:
print("Connection failed. Check network.")
# Program logic that is generally safe can run outside of try blocks
process_data(config_data, response.json())
getting list of numbers as input in python
user_input = input(“Enter numbers separated by spaces: “) user_input = “10 20 30”
use one of the below method to create a list:
print(number_list)
lambda functions
A lambda is an anonymous function, often used with higher-order functions like map(), filter(), and sorted().
list comprehension
number_list = [int(x) for x in user_input.split()]
For lambda function, list comprehensions are often faster than map function.
for loop
Equivalent multi-line code using a for loop instead of list comprehension
number_list = []
for x in user_input.split():
number_list.append(int(x))
map function
# Split the input string and convert each item to an integer
number_list = list(map(int, user_input.split()))
© Prabu Anand K 2020-2026