Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. It was designed to be simple, readable, and easy to learn, which is why it is one of the most popular languages in the world today. Python is used in web development, data science, artificial intelligence, scientific computing, and education. Its clear syntax, which emphasises indentation and readability, makes it the perfect first programming language for students.
Python is an interpreted language, which means that the code is executed line by line by an interpreter rather than being compiled into machine code first. This makes Python programs slower than compiled languages but much easier to write, debug, and test. Python is also a dynamically typed language, so a variable can hold any type of data without declaring its type beforehand. These features, combined with a huge standard library and an enormous collection of third-party modules, make Python powerful and flexible.
In this chapter we will learn the fundamentals of Python: installing and using the interpreter, writing our first program, variables and data types, operators, input and output, and simple programs using decision-making and loops. We will also understand the concept of indentation, which is unique to Python and controls the structure of the code. This foundation will prepare students for functions and object-oriented programming in the following chapters.
Python has several important features that make it widely loved by programmers.
Python can be installed from the official website python.org. After installation, Python programs can be written in any text editor and executed from the command line using the python command, or written and run in the IDLE environment that comes with Python. IDLE is an integrated development environment with a shell where programs can be typed and executed immediately.
print("Hello, World!")
When this program is executed, it displays the message Hello, World! on the screen. The print() function is used to display output in Python.
A variable is a named location in memory that stores a value. In Python, variables are created simply by assigning a value to a name; no declaration is needed. The = symbol is the assignment operator.
name = "Riya"
age = 15
marks = 92.5
is_pass = True
Python supports the following basic data types:
[10, 20, 30].(1, 2, 3).{"name": "Riya", "age": 15}.Python is dynamically typed, so a variable can change its type during execution. The type() function returns the data type of a value.
Operators perform operations on data. The main types of operators in Python are listed below.
+ (addition), - (subtraction), * (multiplication), / (float division), // (integer division), % (modulus), and ** (exponent or power).==, !=, >, <, >=, <=. They return a boolean value.and, or, not. They combine conditions.=, +=, -=, *=, /=. For example, x += 5 is same as x = x + 5.is and is not.in and not in, used to check whether a value is present in a sequence.print(17 // 5) # Output: 3
print(17 % 5) # Output: 2
print(2 ** 3) # Output: 8
The // operator gives the integer quotient, % gives the remainder, and ** computes the power.
The print() function is used to display output. Multiple values can be printed by separating them with commas, and the sep and end arguments control the separator and the ending character.
print("Sum =", 10 + 20) # Output: Sum = 30
print("Hello", "World", sep="-")
The input() function is used to accept data from the user. The input() function always returns a string, so numbers must be converted using the int() or float() functions before arithmetic is performed.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "you are", age, "years old")
This example shows the complete cycle of input, processing, and output in a Python program.
Python uses the if, elif, and else statements for decision making. Unlike many other languages, Python does not use braces to define blocks; instead, indentation defines the block structure. Every statement in a block must be indented by the same number of spaces.
age = int(input("Enter age: "))
if age >= 18:
print("Eligible to vote")
elif age >= 13:
print("Teenager")
else:
print("Child")
The if statement checks a condition; if it is true, the indented block executes. The elif (else if) checks another condition when the previous one is false, and the else block executes when all conditions are false. Indentation errors are among the most common mistakes for beginners.
Loops allow a block of code to be executed repeatedly. Python has two main loops.
i = 1
while i <= 5:
print(i)
i = i + 1
for i in range(1, 6):
print(i)
The range(a, b) function generates numbers from a to b-1. The break statement exits the loop immediately, and the continue statement skips the rest of the current iteration.
Strings are sequences of characters and support many built-in operations. The + operator concatenates strings, the * operator repeats them, and the len() function returns the number of characters. Strings can be indexed starting from 0, and slices such as s[0:3] extract parts of a string.
s = "Python"
print(len(s)) # Output: 6
print(s[0]) # Output: P
print(s[0:4]) # Output: Pyth
print(s.upper()) # Output: PYTHON
Common string methods include upper(), lower(), capitalize(), title(), find(), replace(), and split(). Strings are immutable, meaning they cannot be changed after creation.
| Data Type | Description | Example |
|---|---|---|
| int | Whole numbers | 15 |
| float | Decimal numbers | 92.5 |
| str | Text | "Riya" |
| bool | True or False | True |
| list | Ordered mutable collection | [10, 20] |
| tuple | Ordered immutable collection | (1, 2) |
| dict | Key-value pairs | {"a": 1} |
| Operator | Purpose | Example Result |
|---|---|---|
| // | Integer division | 17 // 5 = 3 |
| % | Remainder | 17 % 5 = 2 |
| ** | Power | 2 ** 3 = 8 |
| == | Equal to | 5 == 5 is True |
| and | Logical AND | True and False is False |
| in | Membership check | 'a' in "cat" is True |
The single feature that most distinguishes Python from other languages is its use of indentation to define blocks of code. In languages like C or Java, braces mark the beginning and end of a block, but in Python, the consistent indentation of a group of statements tells the interpreter that they belong together. This makes Python programs clean and easy to read, but it also demands discipline: every statement in the same block must be indented by exactly the same number of spaces, and mixing tabs with spaces is a frequent source of errors. Whenever a program fails with an indentation error, the fix is almost always to make the spaces uniform throughout the block.
A second idea worth understanding deeply is the behaviour of the input() function. It always returns a string, even when the user types a number. This means that a program which asks for the user's age and then tries to add one to it will fail unless the string is converted with int() or float(). The examples in this chapter follow the pattern of input, convert, calculate, and print, and recognising this cycle is the foundation of every interactive program. It is also the reason that the same statement can produce different output on different runs, because the program's behaviour depends on the data supplied by the user at the moment it runs.
Finally, the distinction between an expression and a statement helps beginners read code accurately. An expression is a piece of code that produces a value, such as 2 ** 3, while a statement is an instruction that performs an action, such as print() or an assignment. The condition inside an if statement is an expression that is evaluated to a boolean, and the right side of an equals sign in an assignment is an expression. Keeping this difference in mind makes it much easier to predict what a program will do line by line and to find the line where a mistake has occurred. Combined with the habit of tracing small programs by hand, these ideas give students a reliable method for understanding and debugging any Python code they write.
input() returns a string, so arithmetic operations on input without int() or float() conversion give an error.= in a condition instead of ==. = assigns while == compares./ with //. / gives a float result, while // gives an integer quotient.range(1, 5) and expecting 5 to be included. The range ends at n-1, so it gives 1 to 4.print(), input(), if...elif...else, and for loops, since these form the core of practical exams.17 // 5, 17 % 5, and 2 ** 3 for one-mark questions.input() always returns a string and must be converted with int() or float() for calculations.Python is the ideal language to learn programming because of its simple syntax and powerful features. In this chapter we learned about the features of Python, how to install and run it, and the basic building blocks of programs: variables, data types, operators, and input-output functions. We studied decision making with if, elif, and else, loops with while and for, and the important concept of indentation that structures Python code. Strings and their operations were also explored. With these fundamentals in place, students are ready to learn functions, which allow code to be organised and reused, in the next chapter.