Free Study Material
You've already seen pseudocode translated into real Python code in the last chapter. This chapter starts Python itself, from the very beginning — what kind of language it is, and the two different ways you can actually run it.
Python is a high-level, interpreted programming language:
print("Hello")) instead of the machine's raw binary instructions.Python code can be run in two different execution modes:
>>> prompt, and it runs immediately, showing the result right away. Good for quickly testing a single line..py extension) and run the whole file at once, top to bottom.The exact same line behaves differently depending on the mode.
In Interactive Mode, typing this at the >>> prompt runs it immediately and shows the result on the next line, with no print() needed:
>>> 5 + 3
8
In Script Mode, the same calculation only produces visible output if you explicitly tell Python to display it with print() — otherwise the result is computed and silently discarded:
5 + 3
print(5 + 3)
Running this file prints only 8 — once. The first line, 5 + 3, does compute a value, but Script Mode never shows it automatically the way Interactive Mode does.
Note: This platform's Run button executes your code in Script Mode — a full program, top to bottom. If you want to see a value, you must print() it explicitly; simply writing an expression on its own line, the way Interactive Mode allows, produces no visible output here.
Interactive Mode is built for quick, one-off checks — testing what one line does before using it inside a real program. Script Mode is how every actual program is written and run, including every example in the rest of this course, which is why print() becomes essential from this point on: it is the only way Script Mode shows you anything at all.
Python is a high-level, interpreted language that runs in two modes: Interactive Mode (one line at a time, results shown automatically) and Script Mode (a full .py file run at once, where only explicit print() output is visible) — this course, and this platform's Run button, both use Script Mode.
Python is a high-level, interpreted, and general-purpose language. It is famous for its readability—meaning the code looks very similar to English. This makes it the perfect "first language" for students and a powerful tool for Data Scientists and AI researchers worldwide.
Python executes code line-by-line instantly.
Pros: Fast for testing commands.
Cons: Code is NOT saved.
Write the entire program in a file and then run it.
Pros: Can save and edit later.
Cons: Requires an extra step to run.
Every line of Python code is built from a small number of basic building blocks, and every value it works with has to be stored somewhere to be used later. This topic covers both: tokens and variables.
A token is the smallest individual unit of a Python program — every line of code is made entirely of tokens, one after another. Python has five kinds:
if, else, for, while, import, True, False, None). A keyword can never be used as a variable name.10, 3.14, "hello", True).+, -, =, ==).(, ), :, ,).An identifier must:
_) — never a digit.marks and Marks are two different names.A variable is a name that refers to a value stored in memory. Python variables need no separate type declaration — a variable's type is decided automatically from the value assigned to it, and can change if a new value of a different type is assigned later.
student_name = "Aisha"
marks = 91
_passed = True
print(student_name)
print(marks)
print(_passed)
marks = "ninety-one"
print(marks)
student_name, marks, and _passed are all valid identifiers — _passed is legal because it starts with an underscore, not a digit. marks starts out holding the integer 91, prints 91, then is reassigned a string, "ninety-one" — Python allows this without any error, because a variable's type is never fixed; it simply follows whatever value is currently assigned.
Warning:1marks = 91would raise aSyntaxError— an identifier can never start with a digit, only contain one after the first character.
This flexibility — a variable's type following its current value rather than a fixed declaration — is exactly what "no type declaration needed" means in practice, and it is also why reusing the same variable name for two genuinely different kinds of data (like marks above) is legal but usually a bad habit: it makes code harder to trace, even though Python itself raises no error.
A Python program is built from five kinds of tokens — Keywords, Identifiers, Literals, Operators, and Punctuators. Identifiers (variable names) must start with a letter or underscore, contain only letters/digits/underscores after that, and never be a keyword; a variable's type is decided by whatever value is currently assigned to it, with no separate declaration required.
A Token is the smallest individual unit in a Python program. When you run a script, the Python interpreter first breaks your code into these tokens to understand the structure of your instructions.
Reserved words with special meaning. You cannot use them as variable names.
if, else, for, while, True, None
Names given to variables, functions, or objects. They must start with a letter or underscore.
my_var, user_age, _temp
Fixed values or "raw data" stored in a variable.
10, 3.14, "Hello", ['a', 'b']
Symbols that perform computations on values.
+, -, *, /, %, ==, and
Symbols used to organize coding structures and clarify the flow.
' , : , [ , ] , { , } , ( , ) , # , @
age = 25
The expression that appears on the left side. It represents the memory location (the container).
The expression that appears on the right side. It represents the data being stored.