𝗣𝘆𝘁𝗵𝗼𝗻 𝗘𝘀𝘀𝗲𝗻𝘁𝗶𝗮𝗹𝘀 𝗳𝗼𝗿 𝗚𝗲𝗻𝗔𝗜
You know JavaScript and TypeScript. You want to build with LangChain, LangGraph, and GenAI tools. You need Python.
You do not need to learn a new way of thinking. You only need to map Python concepts to what you already know.
Here is your quick translation guide.
Core Syntax
- Variables: No let or const. Use name = "Ajmal". Use UPPERCASE for constants.
- Booleans: true/false becomes True/False.
- Null/Undefined: Use None.
- Strings: Template literals
${name}become f-stringsf"{name}". - Print: console.log() becomes print().
- Indentation: Python uses spaces instead of curly braces {}. This is the most important change.
Data Structures
- Lists: Like JS Arrays. Use fruits.append("date") instead of fruits.push("date").
- Dicts: Like JS Objects. Use person["name"] to access values.
- Slicing: A Python superpower. Use fruits[1:3] to get a specific range.
- Sets: Like JS Sets. Great for removing duplicates.
- Tuples: Like frozen arrays. You cannot change them after creation.
Functional Patterns
- Map: Instead of .map(), use list comprehensions: [n * 2 for n in numbers].
- Filter: Instead of .filter(), use: [n for n in numbers if n > 5].
- Spread: Use *args for positional arguments and **kwargs for named arguments.
Classes and Validation
- Classes: Use self instead of this.
- Pydantic: This is vital for GenAI. It handles data validation. LangChain uses it for everything from tool schemas to LLM outputs.
- Decorators: These are like Higher Order Components in React. They wrap functions to add behavior.
Async and File I/O
- Async: Python uses async/await just like JS. Use asyncio.gather() instead of Promise.all().
- Context Managers: Use the with statement to open files. It handles cleanup automatically so you do not forget to close files.
- Exceptions: try/catch becomes try/except. throw becomes raise.
Environment Management
- Package Manager: npm becomes pip.
- Dependency File: package.json becomes requirements.txt.
- Local Modules: node_modules/ becomes .venv/. Always activate your virtual environment before installing packages.
Master these mappings and you can start building AI agents immediately.
Source: https://dev.to/ajmal_hasan/python-essentials-for-genai-lji