@duongital

Programming Languages

I use Python and JS most of the time

Python Basics

1. Primitive Data Types

Python uses:

2. Collections

3. Conditionals

if score > 90:
    print("Excellent!")
elif score > 75:
    print("Good job!")
else:
    print("Keep trying!")

4. Switch Case Alternative

Python doesn’t have a built-in switch. You can use if-elif-else or dictionaries:

actions = {
    "start": "Starting...",
    "stop": "Stopping...",
    "pause": "Pausing..."
}
print(actions.get(command, "Unknown command"))

5. Loops

for i in range(5):
    print(i)
i = 0
while i < 5:
    print(i)
    i += 1
for i in range(10):
    if i == 5:
        continue
    if i == 8:
        break
    print(i)

6. Check Type

type(42)         # <class 'int'>
type("hello")    # <class 'str'>
isinstance([], list)  # True

7. Exit Program

exit()
# or
import sys
sys.exit()

JavaScript Basics

1. Primitive Data Types

JavaScript supports:

2. Collections

let user = new Map()
user.set('name', 'Alice')
user.set('age', 25)

JavaScript does not have a built-in tuple type like Python.

3. Conditionals

if (score > 90) {
  console.log('Excellent!')
} else if (score > 75) {
  console.log('Good job!')
} else {
  console.log('Keep trying!')
}

4. Switch Case

switch (day) {
  case 'Monday':
    console.log('Start of the week!')
    break
  case 'Friday':
    console.log('Almost weekend!')
    break
  default:
    console.log('Another day!')
}

5. Loops

for (let i = 0; i < 5; i++) {
  console.log(i)
}
let i = 0
while (i < 5) {
  console.log(i)
  i++
}
for (let i = 0; i < 10; i++) {
  if (i === 5) continue
  if (i === 8) break
  console.log(i)
}

6. Check Type

typeof 42 // "number"
typeof 'hello' // "string"
Array.isArray([]) // true

7. Exit Program (in Node.js)

process.exit(0)

Javascript / Typescript

Count apperance of character in an array:

let m = new Map()
for (const e of arr) {
  m.set(e, (m.get(e) || 0) + 1)
}