🐍 Python Basics

Python Essentials & Tokens

पायथन की मूल बातें

⏱ 3 hr3 topicsInteractive
🎯 By the end: You can run Python in both modes, name the kinds of tokens, follow the rules for valid identifiers, and write and run a small program.

This is the moment Computer Science stops being theory and becomes something you do. Python is famously beginner-friendly — it reads almost like English. And you don't need to install anything: there's a real Python playground built into this page. Write code, press Run, see it execute. Let's begin.

1Two ways to run Python — and your first program

Python runs your code in two modes:

  • Interactive mode (the REPL — Read, Evaluate, Print, Loop): you type one line at a time at a prompt (>>>) and Python responds immediately. Great for quick experiments.
  • Script mode: you save many lines in a .py file and run them all together. This is how real programs are written.

The traditional first program prints a message to the screen using the print() function. Try it below — change the text inside the quotes and press Run:

Your first Python program▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Interactive mode (REPL) runs one line at a time at the >>> prompt; script mode runs a whole .py file.
  • Python is interpreted, so it runs line by line and stops at the first error.
  • print() displays output on the screen.

2Tokens: the building blocks of code

Just as a sentence is built from words, a Python statement is built from tokens — the smallest meaningful units. There are a few kinds:

TokenWhat it isExamples
KeywordsReserved words with a fixed meaning — you can't use them as namesif, else, for, while, True, None, def
IdentifiersNames you give to variables, functions, etc.age, total_marks, studentName
LiteralsFixed values written directly in code42, 3.14, "hello", True
OperatorsSymbols that perform operations+, -, *, =, ==

Rules for naming identifiers

  • May contain letters, digits and underscores (_) — but cannot start with a digit.
  • No spaces and no special symbols (like @, -, %).
  • Cannot be a keyword (you can't name a variable for).
  • Python is case-sensitive: age, Age and AGE are three different names.
Literal types to know: numeric (Integer 10, Float 10.5, Complex 3+2j), String ("hi"), Boolean (True/False), and the special literal None (meaning 'no value').
Key points
  • Tokens are the smallest units of code: keywords, identifiers, literals and operators.
  • Keywords (if, for, True, None…) are reserved and can't be used as names.
  • Identifiers can't start with a digit, can't contain spaces/symbols, can't be keywords; Python is case-sensitive.
  • Literal types: numeric (int, float, complex), string, Boolean, and the special None.

3Variables and dynamic typing

A variable is a name that refers to a value. You create one simply by assigning with = — no need to declare its type first:

name = "Asha"
age = 16
height = 1.6

This is called dynamic typing: Python figures out the type from the value you assign, and a variable can even be reassigned to a different type later. You can check a value's type with the type() function.

Run the program below, then experiment — change the values, add a new variable, and print it:

Anything after a # on a line is a comment — Python ignores it. Comments explain your code to humans.
Variables in action▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Create a variable just by assigning with = ; no type declaration needed.
  • Dynamic typing: Python infers the type from the value, and it can change on reassignment.
  • type(x) reports a value's type; a # starts a comment, which Python ignores.

★ Project: an 'About Me' card

In the playground above, write a short program that:

  1. Stores your name (string), age (integer) and a hobby (string) in three variables.
  2. Prints a friendly sentence using all three, e.g. using several print() statements.
  3. Adds a comment explaining what one line does.
  4. Uses type() to print the type of your age variable — confirm it says int.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.