Python Basics
Python Introduction
Why Python is everywhere, how it runs your code, indentation, comments, print(), and your first programs.
Python was created by Guido van Rossum and released in 1991. It was designed to be readable: code looks almost like English, and there is usually one obvious way to do something. Today Python is the language of data science, machine learning, automation, web back-ends, scripting and competitive programming.
print("Hello, Python!")
print("2 + 3 =", 2 + 3)
print("Python" * 3)Hello, Python! 2 + 3 = 5 PythonPythonPython
Compare that with C: no #include, no main, no semicolons, no types to declare. The program simply runs from top to bottom.
How Python runs your code
C is compiled ahead of time into machine code. Python is interpreted: the python program reads your .py file, compiles it to bytecode, and the Python virtual machine executes that bytecode line by line.
| C | Python | |
|---|---|---|
| Typing | static: int x |
dynamic: x = 5, types checked at run time |
| Execution | compiled to machine code | interpreted (bytecode + virtual machine) |
| Speed | very fast | slower, but fast enough for most tasks |
| Memory | manual (malloc/free) |
automatic (garbage collected) |
| Blocks | { } braces |
indentation |
| Lines of code for a task | more | far fewer |
python3 hello.py # run a file
python3 # start the interactive shell (REPL)In the interactive shell, each line runs as soon as you press Enter — great for experiments:
>>> 7 * 6
42
>>> "kalvium".upper()
'KALVIUM'Indentation is syntax
Python uses indentation (4 spaces by convention) to mark blocks. Wrong indentation is an error, not just bad style.
marks = 82
if marks >= 40:
print("Pass") # inside the if block
print("Well done!")
print("Result printed") # back outside the blockPass Well done! Result printed
Comments
# A single-line comment starts with a hash.
"""
A string on its own line is often used as a
multi-line comment or documentation (a docstring).
"""print() in detail
print("A", "B", "C") # separated by spaces
print("A", "B", "C", sep="-") # custom separator
print("no newline", end=" ... ") # custom ending
print("continued")
name, score = "Asha", 93.456
print(f"{name} scored {score:.1f}%") # f-string with formatting
print("Tab\tand\nnewline")A B C A-B-C no newline ... continued Asha scored 93.5% Tab and newline
f"..." strings (f-strings) put expressions inside { }. They are the modern way to format output and you will use them constantly.
Python is everywhere
| Area | Popular tools |
|---|---|
| Data analysis | pandas, NumPy, Jupyter |
| Machine learning / AI | scikit-learn, PyTorch, TensorFlow |
| Web back-ends | Django, Flask, FastAPI |
| Automation & scripting | os, pathlib, requests, Selenium |
| Competitive programming | built-in big integers, lists, dicts |
Test yourself
4 questions · pick an answer to check it instantly
- 1
How does Python mark the body of an
ifstatement?The indented lines after the colon form the block. - 2
What does
print("a", "b", sep="")print?An empty separator joins the values directly. - 3
Which statement about Python is true?
Python is dynamically typed and interpreted (via bytecode). - 4
What is printed by
print(f"{7 / 2:.2f}")?The format.2fshows exactly two decimal places.