Free Study Material

Python Fundamentals

Introduction

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.

Concept

Python is a high-level, interpreted programming language:

  • High-level — you write instructions close to plain English (print("Hello")) instead of the machine's raw binary instructions.
  • Interpreted — Python's interpreter reads and runs your code line by line, rather than translating the whole program into machine code first.

Python code can be run in two different execution modes:

  • Interactive Mode — you type one statement at a time at Python's >>> prompt, and it runs immediately, showing the result right away. Good for quickly testing a single line.
  • Script Mode — you write a complete program in a file (saved with a .py extension) and run the whole file at once, top to bottom.

Worked Example

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.

Explanation

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.

Summary

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.

The Power of Python

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.

Interactive Mode (REPL)

Python executes code line-by-line instantly.

Pros: Fast for testing commands.
Cons: Code is NOT saved.

>>> print("Hi")
Hi
Script Mode (.py)

Write the entire program in a file and then run it.

Pros: Can save and edit later.
Cons: Requires an extra step to run.

# saved as hello.py
name = "Aman"
print(name)

The Character Set

  • Letters: A-Z and a-z
  • Digits: 0-9
  • Special Symbols: _ + - * / ( ) [ ] { } % # @ etc.
  • Whitespaces: Blank space, Tab, Newline (Crucial in Python!)

Introduction

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.

Concept

Tokens

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:

  • Keywords — reserved words with a fixed, special meaning (if, else, for, while, import, True, False, None). A keyword can never be used as a variable name.
  • Identifiers — names you choose yourself, for variables, functions, and more.
  • Literals — fixed values written directly in the code (10, 3.14, "hello", True).
  • Operators — symbols that perform an operation (+, -, =, ==).
  • Punctuators — symbols that structure the code ((, ), :, ,).

Identifier Naming Rules

An identifier must:

  • start with a letter or an underscore (_) — never a digit.
  • contain only letters, digits, and underscores after that.
  • not be a keyword.
  • be used consistently — Python is case-sensitive, so marks and Marks are two different names.

Variables

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.

Worked Example

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 = 91 would raise a SyntaxError — an identifier can never start with a digit, only contain one after the first character.

Explanation

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.

Summary

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.

Tokens: The Basic Units

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.

1. Keywords

Reserved words with special meaning. You cannot use them as variable names.

if, else, for, while, True, None
2. Identifiers

Names given to variables, functions, or objects. They must start with a letter or underscore.

my_var, user_age, _temp
3. Literals

Fixed values or "raw data" stored in a variable.

10, 3.14, "Hello", ['a', 'b']
4. Operators

Symbols that perform computations on values.

+, -, *, /, %, ==, and
5. Punctuators

Symbols used to organize coding structures and clarify the flow.

' , : , [ , ] , { , } , ( , ) , # , @

The Concept of Assignment

age = 25

L-Value (Location Value)

The expression that appears on the left side. It represents the memory location (the container).

R-Value (Read Value)

The expression that appears on the right side. It represents the data being stored.

Want the full course?

Unlock all chapters, video lectures, and SQL practice exercises.

Get Full Access