> For the complete documentation index, see [llms.txt](https://brindha.gitbook.io/mylearning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brindha.gitbook.io/mylearning/word2code.md).

# Word2Code

### 1. Python Basics

| Instruction             | Code                                        |
| ----------------------- | ------------------------------------------- |
| print hello world       | `print("Hello World")`                      |
| take input and print    | `name = input("Enter name: "); print(name)` |
| add two numbers         | `a,b=5,3; print(a+b)`                       |
| subtract two numbers    | `a,b=5,3; print(a-b)`                       |
| multiply two numbers    | `a,b=5,3; print(a*b)`                       |
| divide two numbers      | `a,b=6,3; print(a/b)`                       |
| swap two variables      | `a,b=1,2; a,b=b,a`                          |
| check even number       | `n=4; print(n%2==0)`                        |
| check odd number        | `n=5; print(n%2!=0)`                        |
| find max of two numbers | `a,b=5,7; print(max(a,b))`                  |

***

### 2. Functions

| Instruction              | Code                                                  |
| ------------------------ | ----------------------------------------------------- |
| create function add      | `def add(a,b): return a+b`                            |
| create function subtract | `def subtract(a,b): return a-b`                       |
| factorial function       | `def fact(n): return 1 if n==0 else n*fact(n-1)`      |
| fibonacci function       | `def fib(n): return n if n<=1 else fib(n-1)+fib(n-2)` |
| lambda add               | `add=lambda a,b:a+b`                                  |

***

### 3. Loops

| Instruction          | Code                                    |
| -------------------- | --------------------------------------- |
| print numbers 1 to 5 | `for i in range(1,6): print(i)`         |
| while loop example   | `i=1; while i<=5: print(i); i+=1`       |
| loop through list    | `nums=[1,2,3]; for n in nums: print(n)` |
| reverse loop         | `for i in range(5,0,-1): print(i)`      |

***

### 4. List Operations

| Instruction        | Code                              |
| ------------------ | --------------------------------- |
| create list        | `nums=[1,2,3]`                    |
| append list        | `nums.append(4)`                  |
| remove item        | `nums.remove(2)`                  |
| sort list          | `nums.sort()`                     |
| list comprehension | `squares=[x*x for x in range(5)]` |

***

### 5. Dictionary

| Instruction       | Code                      |
| ----------------- | ------------------------- |
| create dictionary | `d={"name":"A","age":20}` |
| access value      | `print(d["name"])`        |
| add key           | `d["city"]="Chennai"`     |

***

### 6. File Handling

| Instruction   | Code                                            |
| ------------- | ----------------------------------------------- |
| write to file | `with open("a.txt","w") as f: f.write("hello")` |
| read file     | `with open("a.txt","r") as f: print(f.read())`  |
| append file   | `with open("a.txt","a") as f: f.write("more")`  |

***

### 7. FastAPI

| Instruction          | Code                                         |
| -------------------- | -------------------------------------------- |
| basic fastapi app    | `from fastapi import FastAPI; app=FastAPI()` |
| create get endpoint  | `@app.get("/items")`                         |
| create post endpoint | `@app.post("/data")`                         |
| run fastapi          | `uvicorn main:app --reload`                  |

***

### 8. MongoDB

| Instruction     | Code                                                    |
| --------------- | ------------------------------------------------------- |
| connect mongodb | `MongoClient("mongodb://localhost:27017/")`             |
| insert document | `db.users.insert_one({"name":"A"})`                     |
| find documents  | `list(db.users.find())`                                 |
| update document | `db.users.update_one({"name":"A"},{"$set":{"age":20}})` |
| delete document | `db.users.delete_one({"name":"A"})`                     |

***

### 9. SQL

| Instruction     | Code                                            |
| --------------- | ----------------------------------------------- |
| create table    | `CREATE TABLE users(id INT, name VARCHAR(50));` |
| insert data     | `INSERT INTO users VALUES (1,'A');`             |
| select all      | `SELECT * FROM users;`                          |
| where condition | `SELECT * FROM users WHERE id=1;`               |
| update query    | `UPDATE users SET name='B' WHERE id=1;`         |
| delete query    | `DELETE FROM users WHERE id=1;`                 |

***

### 10. LangChain

| Instruction     | Code                                                             |       |
| --------------- | ---------------------------------------------------------------- | ----- |
| initialize llm  | `llm = OpenAI()`                                                 |       |
| invoke llm      | `llm.invoke("Hello")`                                            |       |
| prompt template | `PromptTemplate(input_variables=["name"], template="Hi {name}")` |       |
| chain execution | \`chain = prompt                                                 | llm\` |

***

### 11. HTTP Requests

| Instruction  | Code                                     |
| ------------ | ---------------------------------------- |
| get request  | `requests.get("https://api.github.com")` |
| post request | `requests.post("url", json={"a":1})`     |

***

### 12. JSON

| Instruction | Code                    |
| ----------- | ----------------------- |
| load json   | `json.loads('{"a":1}')` |
| dump json   | `json.dumps({"a":1})`   |

***

### 13. Error Handling

| Instruction     | Code                                |
| --------------- | ----------------------------------- |
| try except      | `try: x=1/0 except: print("error")` |
| raise exception | `raise Exception("error")`          |

***

### 14. OOP

| Instruction  | Code                       |
| ------------ | -------------------------- |
| create class | `class A: pass`            |
| constructor  | `def __init__(self): pass` |
| inheritance  | `class B(A): pass`         |

***

### 15. Async

| Instruction    | Code                             |
| -------------- | -------------------------------- |
| async function | `async def hello(): return "hi"` |

***

### 16. Utilities

| Instruction   | Code                   |
| ------------- | ---------------------- |
| random number | `random.randint(1,10)` |
| current time  | `datetime.now()`       |
| list files    | `os.listdir()`         |

***

### 17. CLI

| Instruction      | Code                            |
| ---------------- | ------------------------------- |
| argparse example | `parser.add_argument("--name")` |

***

### 18. Threading

| Instruction       | Code                             |
| ----------------- | -------------------------------- |
| threading example | `threading.Thread(target=print)` |

***

### 19. Advanced Python

| Instruction | Code                          |
| ----------- | ----------------------------- |
| decorator   | `def deco(func): return func` |
| generator   | `def gen(): yield 1`          |
