Lesson 281 of 296
Python basics for TypeScript developers
What you will learn
- Running Python and the current version
- Indentation instead of braces
- Variables, types and None
- Numbers, strings and f-strings
- Truthiness and comparisons
- if, for, while and range
- Functions, default values and type hints
On this page
What Python is
Python is a general-purpose language known for short, readable code. It is the main language for AI, machine learning and data work, and is also used for web back ends (Django, FastAPI), scripts and automation. The current version line is Python 3; Python 3.15 was released in October 2026. Python 2 is dead; ignore old Python 2 examples.
You run a file with python3 app.py, or open an interactive shell with python3.
Indentation is the syntax
Python has no braces for blocks. A colon : starts a block and indentation (4 spaces by convention) shows what is inside it. Wrong indentation is an error.
age = 20
if age >= 18:
print("adult")
print("can vote")
print("done")There are no semicolons and no const/let: you just assign.
Variables and types
Python is dynamically typed: a variable can hold any type, and types are checked when the code runs.
| TypeScript | Python |
|---|---|
number | int (whole numbers, no size limit) and float |
string | str |
boolean (true/false) | bool (True/False, capital letters) |
null / undefined | None (only one "nothing" value) |
array | list |
object / Map | dict |
count = 3
price = 9.99
name = "Sam"
active = True
nothing = None
print(type(count), type(price), type(name), type(active), type(nothing))Numbers
print(7 / 2) # 3.5 (/ always gives a float)
print(7 // 2) # 3 (// is whole-number division, rounds down)
print(-7 // 2) # -4 (rounds DOWN, not towards zero)
print(7 % 2) # 1
print(2 ** 10) # 1024 (power)
print(10 ** 30) # big ints just work, no overflow
print(0.1 + 0.2 == 0.3) # False, same floating point problem as JavaScriptTrick question: -7 // 2 is -4 in Python. In TypeScript, Math.trunc(-7 / 2) is -3.
Strings and f-strings
Strings use single or double quotes. f-strings are Python's template literals.
name = "Sam"
score = 91.456
print(f"{name} scored {score:.1f}") # Sam scored 91.5
print(len(name)) # 3
print(name.upper(), name.lower()) # SAM sam
print("a,b,c".split(",")) # ['a', 'b', 'c']
print("-".join(["a", "b", "c"])) # a-b-c
print(" hi ".strip()) # hi
print(name[0], name[-1]) # S m (negative index counts from the end)
print("am" in name) # TrueStrings are immutable: you cannot change one character; you build a new string.
Truthiness and comparisons
These are falsy: False, None, 0, 0.0, "", and empty collections [], {}, set(). Everything else is truthy.
Trick question: an empty list is falsy in Python but truthy in JavaScript.
items = []
if not items:
print("no items") # runs
print(1 == 1.0) # True
print("1" == 1) # False: no type conversion, unlike JavaScript ==
print(None is None) # True: use 'is' for None
print(1 < 5 < 10) # True: comparisons can be chained
print(True and False, True or False, not True) # False True FalsePython uses the words and, or, not instead of &&, ||, !.
if, for, while and range
score = 72
if score >= 80:
grade = "A"
elif score >= 60:
grade = "B"
else:
grade = "C"
print(grade) # B
for i in range(3): # 0, 1, 2
print(i)
for i in range(10, 0, -3): # 10, 7, 4, 1 (start, stop, step; stop is not included)
print(i)
for letter in "hi": # loops over any iterable
print(letter)
n = 3
while n > 0:
n -= 1 # there is no n-- in Python
print(n) # 0
label = "even" if n % 2 == 0 else "odd" # the ternary expression
print(label) # evenFunctions
def greet(name: str, greeting: str = "Hello") -> str:
return f"{greeting}, {name}!"
print(greet("Sam")) # Hello, Sam!
print(greet("Sam", greeting="Hi")) # Hi, Sam! (keyword argument)
def total(*numbers: int) -> int: # *args: any number of positional arguments
return sum(numbers)
print(total(1, 2, 3)) # 6
square = lambda x: x * x # a small one-expression function
print(square(4)) # 16Type hints (name: str, -> str) are like TypeScript types, but Python does not check them when running. Tools such as mypy or pyright check them, the way tsc checks TypeScript.
A function with no return returns None.
How to say it in an interview
"Python uses indentation for blocks, is dynamically typed, and has one empty value, None. Integers have no size limit, / always returns a float and // floors, so -7 // 2 is -4. Empty collections are falsy, == does not convert types, and I use 'is' only for None. I use f-strings for formatting, keyword arguments and defaults in functions, and type hints checked by mypy or pyright, because Python itself ignores them at runtime."
Key words
| Word | Meaning |
|---|---|
| Indentation | Spaces that mark which lines are inside a block |
| None | Python's single "no value" value |
| f-string | A string starting with f that can contain {expressions} |
| // | Floor division: divide and round down |
| Type hint | A type annotation that Python itself does not enforce |
| mypy / pyright | Tools that check Python type hints |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.