Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991. It is designed with a strong emphasis on code readability, making it one of the easiest languages for beginners to learn while remaining powerful enough for professionals. Python is used everywhere today, from simple scripts to web applications, data analysis, artificial intelligence, and scientific computing.
The name Python comes from the British comedy show "Monty Python's Flying Circus", not from the snake. Python follows a philosophy called "batteries included", meaning it comes with a rich standard library of modules that handle common tasks such as file handling, mathematics, and networking without needing extra installation.
This chapter lays the foundation of programming in Python. We will explore how to install and run Python, the structure of a Python program, the rules for writing identifiers and keywords, and how data is stored using variables. We will also see how Python's interactive mode (IDLE) differs from script mode, and how comments and print statements help us understand and debug code. Every concept introduced here will be used constantly throughout the remaining Python chapters.
Python can be downloaded free from python.org. After installation, the user gets two ways of working with Python:
>>>. This is great for experimenting with short commands and for quick calculations..py extension and then run as a complete program. This is used for real programs with many statements.Example of interactive mode:
>>> 5 + 3
8
>>> print("Hello World")
Hello World
A script file first.py would contain the same statements and is executed using the command python first.py.
The set of all characters that Python can recognise is called its character set. It includes letters (A-Z, a-z), digits (0-9), special symbols (such as +, -, , /, %, #, @), and whitespace (space, tab, newline). Python programs are broken down by the interpreter into small units called tokens*. The five types of tokens are:
if, else, while, for, def, return, import, True, False, None. They cannot be used as identifiers.25, 3.14, "Hello", True.+, -, *, /, =, <.( ) [ ] { } , : ._.@, $, %.age and Age are different identifiers.Valid identifiers: name, _count, roll_no, total2. Invalid identifiers: 2name (starts with digit), my name (contains space), class (keyword), na$me (special symbol).
A variable is a name that refers to a storage location in memory that holds a value. In Python, unlike C or Java, you do not need to declare the type of a variable before assigning a value; the type is inferred automatically from the value assigned. This is called dynamic typing.
name = "Anita"
age = 17
marks = 95.5
is_pass = True
Here name holds a string, age holds an integer, marks holds a float, and is_pass holds a boolean. The assignment operator = stores the value on its right into the variable on its left. Multiple variables can be assigned in one line:
x, y = 10, 20
Python has several built-in data types. The commonly used ones are:
42, -7.3.14, -0.5.a + bj, e.g., 2 + 3j.'hello', "world".True or False.[1, 2, 3].(1, 2, 3).{"name": "Amit"}.{1, 2, 3}.The type() function returns the type of a value:
print(type(42))
print(type(3.14))
print(type("Hello"))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
Python provides a rich set of operators. The main categories are:
+ (addition), - (subtraction), * (multiplication), / (division, always returns float), // (floor division), % (modulus or remainder), ** (exponentiation).==, !=, <, >, <=, >=; they always produce a boolean result.and, or, not.=, +=, -=, *=, /=, %=.is, is not.in, not in.a = 17
b = 5
print(a // b)
print(a % b)
print(a ** 2)
print(a > b)
Output:
3
2
289
True
Here 17 // 5 gives 3 (floor division), 17 % 5 gives 2 (remainder), and 17 ** 2 gives 289.
The input() function reads a line of text from the keyboard. It always returns a string, so if we need a number, we must convert it using int() or float().
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("You will be", age + 1, "next year")
The print() function displays output. By default it separates multiple values with a space and ends with a newline. The sep and end parameters can change this behaviour:
print("a", "b", "c", sep="-", end="!")
Output: a-b-c!
When data moves between variables, functions, and expressions, Python sometimes needs to convert a value from one type to another. This conversion is called type conversion or type casting. It can happen automatically, as when an integer and a float are added together: the expression 5 + 2.5 produces the float 7.5, because Python promotes the integer to a float before performing the addition. This automatic behaviour is called implicit type conversion. However, Python never converts a string to a number implicitly, so writing "5" + 2 would raise a TypeError. When such a conversion is required, the programmer must do it explicitly using functions such as int(), float(), and str().
The input() function always returns a string, which is why so many beginners see unexpected errors when they try to do arithmetic directly on the value entered by the user. For example, if a student types 45 in response to a prompt, input() returns the string "45". Adding 5 to this string with the expression "45" + 5 fails, because Python cannot combine a string and an integer. The correct approach is to wrap the input in int(), as in age = int(input("Enter age: ")). The same logic applies to float() for decimal values. Understanding when conversion happens automatically and when it must be written explicitly removes a whole class of common programming errors, and it is a favourite exam topic.
A good way to consolidate the basics is to write a small program that uses variables, input, arithmetic, and output together. Suppose we want to compute the area of a rectangle. We first read the length and breadth using input(), convert each to a float, multiply them, and finally print the result. The program shows how the three stages of input, processing, and output fit together in the order that Python executes them. Another classic program converts a temperature in Celsius to Fahrenheit using the formula F = (9/5) C + 32. In Python, care is needed with the fraction 9/5: since both 9 and 5 are integers, the division gives the float 1.8, which is exactly what the formula requires. Reading such small programs line by line, predicting the output mentally, and then running them to check the prediction is the most effective way to master the fundamentals of Python and to prepare for output-prediction questions in the examination.
| Token | Description | Example |
|---|---|---|
| Keyword | Reserved word | if, while, def |
| Identifier | Name given by user | age, marks |
| Literal | Constant value | 25, 3.14, "Hi" |
| Operator | Performs operation | +, %, ** |
| Punctuator | Separates code | ( ), ,, : |
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 2 |
9 |
- |
Subtraction | 7 - 2 |
5 |
* |
Multiplication | 7 * 2 |
14 |
/ |
Division | 7 / 2 |
3.5 |
// |
Floor division | 7 // 2 |
3 |
% |
Modulus | 7 % 2 |
1 |
** |
Exponent | 2 ** 3 |
8 |
class = 10, which produces a syntax error.2name.Name and name are different variables./ when floor division // was intended, since / always returns a float.input() without converting it with int(), which causes a type error.= means equality; in Python = assigns while == compares.elif as else if in Python, which is a syntax error.// performs floor division and / always gives a float.% for negative numbers: the result has the sign of the divisor in Python.print() with sep and end.type() output format such as <class 'int'>.int() or float() when numbers are needed.The basis of Python lies in its clean syntax and the simple concepts of tokens, variables, data types, operators, and input-output. Understanding what makes an identifier valid, how data flows through a program, and how operators behave prepares the student for writing meaningful programs. Python's dynamic typing and readable structure make it an ideal first programming language, and the fundamentals covered here are the stepping stones for data handling, control structures, lists, dictionaries, and visualisation that follow in the later chapters.