0% found this document useful (0 votes)
19 views12 pages

Practice Sheets

This document is an introductory programming guide focused on Python, covering basics such as output prediction, conditionals, and while loops. It includes practice exercises for predicting outputs, filling in code, and writing complete programs. The content is structured into chapters that progressively build programming skills through hands-on activities.

Uploaded by

ezhilarasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Practice Sheets

This document is an introductory programming guide focused on Python, covering basics such as output prediction, conditionals, and while loops. It includes practice exercises for predicting outputs, filling in code, and writing complete programs. The content is structured into chapters that progressively build programming skills through hands-on activities.

Uploaded by

ezhilarasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Programming

COL 1000 Practice sheets


Department of Computer Science & Engineering
IIT Delhi
Contents

1 Introduction and Python Basics 3

2 Conditionals: If-Else 6

3 While Loops 10

2
Chapter 1

Introduction and Python Basics

Output Prediction
Predict the output or fix the errors for each of the following programs.
1.
print ( " Hello " )
print ( " Python " )

2.
x = 13
y = 3
print ( x + y )
print ( x * y )
print ( x % y )

3.
name = " Rama "
print ( name * 3)
print ( name + 3)

4.
a = 10
b = 3
print ( a / b )
print ( a // b )
print ( a % b )

5.
s1 = " Hello "
s2 = " World "
print ( s1 + " " + s2 )

6.
print ( " 5 " + " 6 " )
print (5 + 6)

3
4

7.
x = 4
x = x //2 + 1
x = x * 2
print ( x )

8.
x = " \ nPython "
x = x * 2
print ( x + " is a programming language . " )

Fill in the Code


Complete each program so that it performs the required task.

1. Read an integer from the user and print its square.


# read a number from user
num = int ( ________ )
# print its square
print ( ________ )

2. Read a name and age, then print: Name: <name>, Age: <age>
name = input ( " Enter your name : " )
age = input ( " Enter your age : " )
print ( " Name : " , ________ , " \ n Age : " , ________ )

3. Read two float numbers and print their average.


a = float ( input ( " Enter first number : " ) )
b = float ( input ( " Enter second number : " ) )
avg = ( ________ ) / ________
print ( " Average is " , avg )

4. Read a string and print it 3 times in a single line separated by spaces using *.
word = input ( " Enter a word : " )
result = ( word + ___ ) * ________
print ( result )

5. Read the length and breadth of a rectangle and print its area.
length = float ( input ( " Enter length : " ) )
breadth = float ( input ( " Enter breadth : " ) )
area = ________ * ________
print ( " Area : " , area )
5

6. Read a number and print True if it is even, else print False using only arithmetic
and comparison, no if.
num = int ( input ( " Enter a number : " ) )
print ( ________ == 0)

7. Read a 4-digit year and print the last two digits.


year = int ( input ( " Enter year : " ) )
last_two = ________ % ________
print ( last_two )

Write the Code


1. Write a program that takes your name as input and prints
Hello, <name>! where <name> is what the user typed.

2. Write a program that:

• Asks the user for two integers.


• Prints their sum and product on separate lines.

3. Write a program that:

• Asks the user for a word and a number n in the range [1, 5].
• Prints the word repeated n times on one line using the * operator.
Chapter 2

Conditionals: If-Else

Output Prediction
1.
x = 7
if x % 2 == 0:
print ( " Even " )
else :
print ( " Odd " )

2.
marks = 85
if marks >= 90:
print ( " A " )
elif marks >= 80:
print ( " B " )
else :
print ( " C " )

3.
a = 5
b = 5
if a > b :
print ( " a " )
elif a < b :
print ( " b " )
else :
print ( " equal " )

4.
n = -3
if n > 0:
if n % 2 == 0:
print ( " Positive even " )
else :
print ( " Positive odd " )
else :
print ( " Not positive " )

6
7

5.
num = 15
if num % 3 == 0 and num % 5 == 0:
print ( " FizzBuzz " )
elif num % 3 == 0:
print ( " Fizz " )
elif num % 5 == 0:
print ( " Buzz " )

6.
x = 0
if x :
print ( " True block " )
else :
print ( " False block " )

Fill-ups / Equivalence / Correction


1. Fill the blanks to print the minimum of two numbers using if-else.
a = int ( input ( " Enter a : "))
b = int ( input ( " Enter b : "))
if a < b :
print ( " Minimum is " , ________ )
else :
print ( " Minimum is " , ________ )

2. Check if the following two code fragments are equivalent for all integer x. If not,
give an example value of x where they differ.
Fragment A:
if x > 10:
print ( " Large " )
else :
print ( " Not large " )

Fragment B:
if x >= 10:
print ( " Large " )
else :
print ( " Not large " )

3. Check if the following two code fragments are equivalent. If they are not, explain
the difference.
Fragment A:
8

if n % 2 == 0:
print ( " Even " )
else :
print ( " Odd " )

Fragment B:
if n % 2 != 0:
print ( " Odd " )
else :
print ( " Even " )

4. Fill the missing condition so that the program prints Inside range only when n
is between 1 and 100 (inclusive).
n = int ( input ( " Enter n : " ) )
if ________ :
print ( " Inside range " )
else :
print ( " Outside range " )

5. Complete the program to print Leap year if year is divisible by 400, or divisible
by 4 but not by 100. Otherwise print Not leap year.
year = int ( input ( " Enter year : " ) )
if ( ________ ) or ( ________ and ________ ) :
print ( " Leap year " )
else :
print ( " Not leap year " )

6. Fill the blanks to classify a number as Even Positive, Odd Positive, or Non-positive.
n = int ( input ( " Enter n : " ) )
if n > 0 and n % 2 == 0:
print ( " Even Positive " )
elif n > 0 and ________ :
print ( " Odd Positive " )
else :
print ( " Non - positive " )

Write the Code


1. Write a program that reads an integer and prints whether it is Positive, Negative,
or Zero using if-elif-else.

2. Write a program that reads a person’s age and:

• Prints Child if age < 13.


9

• Prints Teen if 13 ≤ age ≤ 19.


• Prints Adult otherwise.

3. Write a program that reads three integers and prints the largest number using
nested if-else.

4. Write a program to read a 3-digit number and count how many of its digits are
odd, then print that count.
Chapter 3

While Loops

Output Prediction
1.
i = 1
while i <= 3:
print ( i )
i = i + 1
2.
i = 1.0
while i <= 13:
print ( i * 2)
i = i + 2.5
3.
i = 5
while i > 0:
print ( i )
i = i - 2
4.
sum = 0
while i < 9:
sum = sum + i
i = i +1
print ( sum )
5.
i = 1
while i < 5:
print ( " Loop " , i )
i = i + 1
print ( " End " )

Fill / Errors / Infinite Loops


1. The following program has three errors. Rewrite it correctly so that it prints
numbers 10, 9, 8, 7, 6.

10
11

i = 10
while i > 5
print ( i )
i = i - 1

2. Fill in the blanks to compute the factorial of a positive integer n using a while
loop.
n = int ( input ( " Enter n : " ) )
fact = 1
i = 1
while i <= n :
fact = fact * ________
i = ________
print ( " Factorial is " , fact )

3. Predict what the following infinite loop will print in the first five lines.
i = 1
while True :
print ( i )
i = i + 1
# assume user never stops the program

4. Fill the blanks to count the number of digits in a positive integer n.


n = int ( input ( " Enter n : " ) )
count = 0
while n > 0:
count = count + 1
n = ________
print ( " Number of digits : " , count )

5. Fill in the blanks so that the program reads integers from the user one by one
and stops when the sum reaches or exceeds 100. Print the final sum.
total = 0
while ________ < 100:
num = int ( input ( " Enter a number : " ) )
total = total + ________
print ( " Final sum : " , total )

6. Fill in the blanks so that the program reads an integer n and prints a pattern of
stars. Each line i (from 1 to n) has i stars.
n = int ( input ( " Enter n : " ) )
i = 1
while i <= ________ :
print ( " * " * ________ )
i = ________
12

7. Fill in the blanks so that the program computes ab (a raised to the power b) using
a while loop.
a = int ( input ( " Enter base : " ) )
b = int ( input ( " Enter exponent : " ) )
result = 1
i = 0
while i < ________ :
result = result * ________
i = ________
print ( f " { a } raised to { b } is { result } " )

8. Fill in the blanks so that the program reads an integer and reverses its digits
using a while loop (e.g., input 1234 → output 4321).
n = int ( ________________ )
rev = 0
while n > 0:
digit = ________
rev = rev * 10 + digit
n = ________
print ( rev )

Write the Code


1. Write a program using a while loop to print the squares of numbers from 3 to 8.

2. Write a program that reads a positive integer from the user and calculates the
sum of all its digits using a while loop. For example, if the input is 1234, the
output should be 1 + 2 + 3 + 4 = 10.

3. Write a program that reads a positive integer and counts how many times digit 1
appears in that number using a while loop. For example:

• Input: number = 112211, digit to count = 1


• Output: Digit 1 appears 4 times

4. Write a program that reads an integer and checks if it is a palindrome (reads the
same forwards and backwards) using a while loop. Print Palindrome or Not a
palindrome. For example, 121 is a palindrome, but 123 is not.

You might also like