Unit 4: Programming in Python – Class 10 SEE Computer Science Notes
Importantedunotes.com
Back to Computer Notes

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.

Integer (int): Refers to whole numbers ranging from negative to positive infinity without decimals. Examples include -3, 0, 42, and 1000.
Float (float): Refers to numbers that contain decimals, acting as precise measurements. Examples include 3.14, -0.5, and 9.99.
String (str): Represents text data consisting of letters, numbers, or symbols enclosed in quotes. Examples include “hello” and “Python@123”.
Boolean (bool): Holds logical data that has one of two absolute states, similar to a light switch. Examples include True and False (Note: Always capitalize the first letter in Python!).

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.

i. Which statement is used to display output in Python?
a) input ( )
b) print ( )
c) display ( )
d) output ( )
Correct Answer: b) print ( )
Justification: In Python, the built-in print() function is explicitly used to output data and text to the computer’s screen or console.
ii. Which of the following is NOT a valid Python data type mentioned in the text?
a) int
b) float
c) character
d) bool
Correct Answer: c) character
Justification: Python does not have a standalone “character” data type. Single characters are simply treated as strings (str) of length 1.
iii. What type of values does the Boolean data type hold?
a) Whole numbers
b) Decimal numbers
c) Text
d) True or False
Correct Answer: d) True or False
Justification: The Boolean (bool) data type is a logical classification that can exclusively hold one of two absolute values: True or False.
iv. Which of the following is an example of a relational operator?
a) +
b) *
c) ==
d) and
Correct Answer: c) ==
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.
v. Which logical operator returns True if both conditions are true?
a) or
b) not
c) and
d) !=
Correct Answer: c) and
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.
vi. What is the purpose of the if statement in Python?
a) To repeat a block of code.
b) To define a function.
c) To execute a block of code only if a condition is true.
d) To store multiple items in a single variable.
Correct Answer: c) To execute a block of code only if a condition is 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.
vii. Which conditional statement allows you to check multiple conditions in sequence?
a) if
b) if-else
c) nested if
d) if-elif-else
Correct Answer: d) if-elif-else
Justification: The elif (else if) block enables programmers to chain multiple, distinct condition checks together sequentially until one evaluates to True.
viii. What is the term for repeating a block of code multiple times?
a) Selection
b) Iteration
c) Condition
d) Assignment
Correct Answer: b) Iteration
Justification: Iteration refers to the computational process of executing a set of instructions repeatedly, commonly achieved in Python using loops.
ix. Which type of loop is used when you know the number of times you want to repeat a block of code?
a) while loop
b) for loop
c) if loop
d) nested loop
Correct Answer: b) for loop
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.
x. Which data structure is used to store multiple items in a single variable in Python, as shown in the example [“Computer”, “Science”, 20, True]?
a) String
b) Tuple
c) List
d) Dictionary
Correct Answer: c) List
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.

a) What is the primary function of the input() function in Python? 2 Marks
The primary function of the 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).

b) Provide an example of a string literal in Python. 2 Marks
"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.

c) What is an identifier in the context of Python programming? 2 Marks
An identifier is a user-defined name given to program units such as variables, functions, or classes (e.g., user_age or total_score). It allows programmers to reference and interact with those units later in the code.

d) Explain the difference between the division operator (/) and the floor division operator (//) in Python. 2 Marks
The standard division operator (/) 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).

e) Define the concept of data types in Python. 2 Marks
Data types (such as 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.

f) What is the purpose of relational operators in Python? Give one example. 2 Marks
Relational operators are used to compare two values to see how they relate to one another, always returning a Boolean result (True or False). For example, the > (Greater than) operator, as in 7 > 5, evaluates to True.

g) Describe the functionality of the else block in an if-else statement. 2 Marks
The 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.

h) What is a nested if statement? 2 Marks
A nested 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.

i) When would you typically use a for loop instead of a while loop? 2 Marks
You use a 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.

j) What is the role of the range() function often used with for loops? 2 Marks
The 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.

k) Define a Python list and provide a simple example. 2 Marks
A Python list is a built-in, mutable data structure used to store a collection of multiple items (even of different data types) inside a single variable, enclosed in square brackets. An example is my_list = ["Apple", 5, True].

l) Discuss the concept of iteration in programming. 2 Marks
Iteration is the programming technique of doing things repeatedly. It allows a specific block of code to be executed over and over automatically until a specified condition is satisfied, preventing code duplication and making programs vastly more efficient.

Exercise 3: Write long answers to these questions.

a) Explain the different categories of operators available in Python (arithmetic, relational, and logical) with examples of each. 4 Marks
Arithmetic Operators: Used to perform mathematical calculations on numbers. Examples include + (Addition), - (Subtraction), * (Multiplication), % (Modulus). For instance, 10 % 3 = 1 (because 10 divided by 3 leaves a remainder of 1).
Relational Operators: Used to compare two values or variables against each other, yielding a Boolean output (True or False). Examples include == (Equal to), != (Not equal to), < (Less than). For instance, 5 == 5 is True.
Logical Operators: Used to combine conditional statements together to form complex decision-making logic. Examples include 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.

b) Describe the three main types of conditional statements in Python (if, if-else, and if-elif-else). 4 Marks
if Statement: The most basic conditional structure. It checks a single condition; if the condition is True, it runs the indented block of code. If False, the program bypasses it entirely.
if-else Statement: Provides a two-way decision intersection. If the 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-elif-else Statement: A multi-way decision structure. The program checks the initial 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.

c) Explain the two types of loops available in Python (for and while) with their respective syntaxes and provide a practical example demonstrating the use of each loop. 4 Marks

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.


d) Describe the four basic data types covered in the text (integer, float, string, and boolean), providing an example of each. Why is it important to understand data types when writing Python programs? 4 Marks
Integer (int): Whole numbers without a decimal component. An example is 42.
Float (float): Real numbers that contain a decimal point. An example is 3.14.
String (str): Text elements and characters enclosed safely in quotes. An example is "Computer Science".
Boolean (bool): Absolute logical truth states. An example is 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.


e) & f) Imagine you need to write a Python program that takes an integer input from the user and performs the following actions:

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

Scroll to Top