Programming in Python
Chapter One: Revision of the Basics of Python
Comprehensive study guide exploring I/O, operators, variables, loops, conditional statements, and fully solved textbook exercises for SEE preparation.
Welcome to Programming in Python! Python is widely considered one of the best programming languages because it is readable, versatile, and powerful.
Before we dive into advanced programming, we need to master the foundational building blocks. In this guide, you will revise data types, loops, conditional statements, operators, and gain access to complete textbook exercise solutions for your Class 10 SEE preparation.
1.Revision of the Basics of Python.
4.1 Input/Output Statements (I/O)
Think of Input/Output (I/O) statements as the “mouth” and “ears” of your program. They are the primary ways your code interacts with the person using it.
1. Print Statement (The “Mouth”)
Whenever you want your program to “speak” or display a result on the screen, you use the print() function. For example, print("Hello, Python!") will simply display: Hello, Python! on the screen.
2. Input Function (The “Ears”)
When your program needs to ask the user for information, you use the input() function. By default, anything the user types is captured as a Text String. For example, when you use number = input("Enter a number: "), the program pauses, displays the prompt “Enter a number: “, and waits for the user to type something and press Enter.
4.2 Data Types, Identifiers, and Variables
To process information correctly, Python needs to know what kind of data it is looking at.
1. Core Data Types
Python offers several built-in data types to handle different categories of information.
2. Identifiers
An Identifier is simply a name you give to a program unit, such as a variable or a function. A good rule of thumb is that an identifier should make sense. user_age is a great identifier, while x123 is confusing!
3. Variables
Think of Variables as labeled storage boxes or containers that hold information in the computer’s memory. A variable is created the moment you assign a value to it using the equals sign (=). For example, in the code a = 15 and name = "Aarosh", a is the box holding the integer 15, and name is the box holding the string “Aarosh”.
4.3 Operators and Expressions
Operators are special mathematical and logical symbols. They allow us to manipulate our variables and perform specific actions.
1. Arithmetic Operators
Used for performing standard mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 20 | 30 |
| – | Subtraction | 10 – 20 | -10 |
| * | Multiplication | 10 * 20 | 200 |
| / | Division | 20 / 10 | 2.0 (Always gives a float) |
| % | Modulus | 20 % 10 | 0 (Gives the remainder of division) |
| ** | Exponent | 10 ** 2 | 100 |
| // | Floor Division | 9 // 2 | 4 (Divides and chops off the decimal) |
2. Relational Operators
Relational operators compare two values. They act like questions and always answer with a Boolean (True or False).
| Operator | Name | Description | Example (evaluates to True) |
|---|---|---|---|
| == | Equal to | Are these exactly the same? | (5 == 5) |
| != | Not equal to | Are these different? | (3 != 5) |
| > | Greater than | Is the first strictly bigger? | (7 > 5) |
| < | Less than | Is the first strictly smaller? | (3 < 9) |
| >= | Greater than or equal | Is it bigger or the same? | (8 >= 8) |
| <= | Less than or equal | Is it smaller or the same? | (4 <= 6) |
3. Logical Operators
Logical operators combine multiple conditions to make complex decisions.
| Operator | Name | Rule | Example |
|---|---|---|---|
| and | AND | True only if both sides are true. | (5 > 3) and (10 > 5) |
| or | OR | True if at least one side is true. | (5 > 3) or (1 == 2) |
| not | NOT | Flips the logic. True becomes False. | not(5 == 5) results in False |
4.4 Conditional Statements
In programming, we don’t always want every line of code to run. Sometimes, we want code to run only if a specific condition is met. These are the decision-makers!
1. The if Statement
This is the simplest decision. If the condition is true, execute the code. If not, do nothing.
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
2. The if-else Statement
This gives us a “Plan B.” If the condition is true, do the first thing; otherwise, do the second thing.
user_age = int(input("How old are you? "))
if user_age >= 18:
print("You are an adult!")
else:
print("You are a teenager or a kid.")
3. The if-elif-else Statement
Used when there are multiple distinct possibilities to check in sequence. elif stands for “else if”.
user_number = int(input("Enter a number: "))
if user_number > 0:
print("The number is positive.")
elif user_number == 0:
print("The number is zero.")
else:
print("The number is negative.")
4. Nested if (if inside if)
Sometimes a decision requires a follow-up decision. A nested if is an if statement placed completely inside another.
age = int(input("Enter your age: "))
if age >= 16:
print("You are eligible for citizenship.")
if age >= 18:
print("You are eligible to cast a vote.")
else:
print("You are not eligible to cast a vote.")
else:
print("You are a minor.")
4.5 Iteration (Loops)
Iteration means doing things repeatedly. It is the process of repeating a specific task automatically until a condition is met, saving us from writing the same code over and over again.
1. The for loop
Use a for loop when you know exactly how many times you want to repeat a block of code.
for x in range(5):
print("Programming")
The range(5) function tells the loop to run exactly 5 times.
2. The while loop
Use a while loop when you want the code to repeat as long as a condition remains true.
count = 1
while count <= 5:
print(count)
count += 1
Important: count += 1 adds 1 to ‘count’ each time, preventing an infinite loop!
4.6 Python Lists
A list is a built-in data structure in Python that acts like a highly flexible filing cabinet. It allows you to store multiple items (even of different data types) inside a single variable using square brackets [ ].
thislist = ["Computer", "Science", 20, True] print(thislist)
Exercise 1: Choose the correct answer.
Select an option to view the correct answer and justification.
Justification: In Python, the built-in print() function is explicitly used to output data and text to the computer’s screen or console.
Justification: Python does not have a standalone “character” data type. Single characters are simply treated as strings (str) of length 1.
Justification: The Boolean (bool) data type is a logical classification that can exclusively hold one of two absolute values: True or False.
Justification: The == symbol is a relational operator used to check if two values are equal. + and * are arithmetic operators, and and is a logical operator.
Justification: The and logical operator mandates that the conditions on both its left and right sides must evaluate to True for the overall expression to be True.
Justification: The if statement acts as a conditional gatekeeper, running the indented code block beneath it strictly when its specified logical condition is met.
Justification: The elif (else if) block enables programmers to chain multiple, distinct condition checks together sequentially until one evaluates to True.
Justification: Iteration refers to the computational process of executing a set of instructions repeatedly, commonly achieved in Python using loops.
Justification: The for loop, especially when paired with sequence generators like range(), is purposefully designed for definite iteration where the exact loop count is known beforehand.
Justification: Lists are designated by square brackets [ ] and are specifically designed to hold ordered collections of varied data types in a single variable.
Exercise 2: Write short answers to these questions.
input() function is to pause the program’s execution, wait for the user to type information via the keyboard, and capture that data into the program (by default, as a text string).
"Python@123" is a string literal. A string literal is any sequence of letters, numbers, or special characters enclosed safely inside single or double quotes.
user_age or total_score). It allows programmers to reference and interact with those units later in the code.
/) always returns a precise float value with decimals (e.g., 9 / 2 = 4.5). The floor division operator (//) divides the numbers and discards the decimal remainder entirely, returning a whole integer (e.g., 9 // 2 = 4).
int, float, str, and bool) define the category of a value. They inform the Python interpreter how to store the data in memory and what logical or mathematical operations can be safely performed on it.
> (Greater than) operator, as in 7 > 5, evaluates to True.
else block serves as a fallback or “Plan B”. Its functionality is to execute a specific block of code strictly when the preceding if condition evaluates to False.
if statement is a structural construct where one if statement is placed entirely inside the indented block of another if statement, enabling the program to check for a secondary condition only if the first condition is met.
for loop for definite iteration—when you know exactly how many times the loop should run, or when iterating through a fixed sequence like a list. A while loop is used for indefinite iteration—when the code must repeat based on a dynamic condition remaining True.
range() function dynamically generates a sequence of numbers (e.g., range(5) generates 0, 1, 2, 3, 4). Its role is to dictate exactly how many times the for loop’s block of code should be executed.
my_list = ["Apple", 5, True].
Exercise 3: Write long answers to these questions.
+ (Addition), - (Subtraction), * (Multiplication), % (Modulus). For instance, 10 % 3 = 1 (because 10 divided by 3 leaves a remainder of 1).== (Equal to), != (Not equal to), < (Less than). For instance, 5 == 5 is True.and (requires both sides to be true), or (requires at least one side to be true), not (inverts truth). For instance, (5 > 3) and (2 < 4) evaluates to True.if condition is True, its code executes. If it is False, the code within the else block executes instead, ensuring an action is always taken.if condition. If False, it moves down to check the elif (else if) condition(s). If absolutely no conditions match, it defaults to executing the final else fallback block.for Loop: Designed to iterate over sequences or run a specific, predetermined number of times. The syntax is for item in sequence:. For example:
for i in range(3):
print("Hello")
This loop prints “Hello” exactly 3 times.
while Loop: Designed to run continuously as long as a specified condition evaluates to True. It is ideal when the total number of iterations is unpredictable. The syntax is while condition:. For example:
count = 1
while count <= 3:
print(count)
count += 1
This loop prints 1, 2, and 3, then stops when count hits 4.
42.3.14."Computer Science".True or False.Understanding data types is critical because it dictates what operations are legally allowed by the computer. For instance, Python can multiply two integers, but it will crash if you attempt to mathematically divide a string by an integer. Proper data typing ensures program logic and stability.
i. Prints “Positive” if the number is greater than 0.
ii. Prints “Negative” if the number is less than 0.
iii. Prints “Zero” if the number is equal to 0.
iv. If the number is positive, it then checks if it’s even or odd and prints the result.
Write the Python code for this program, demonstrating your understanding of input/output, conditional statements (including nested if), and basic arithmetic operators. 4 Marks
# Taking an integer input from the user
number = int(input("Enter an integer number: "))
# Using if-elif-else to determine the primary category of the number
if number > 0:
print("Positive")
# Nested 'if' statement to determine if the positive number is even or odd
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
elif number < 0:
print("Negative")
else:
print("Zero")
📚 Also Read: Class 10 SEE Notes
Computer Science Units
Other Subjects
