NPS International School
Topic: Python Programming
Name: _______ Subject: ICT
Date: ________ Grade: 7
Python – Solutions:
Task 1: My First Python Program [5 Marks]
Create a python program myname.py to display your name and class.
print(“Name – Lucy Class – 8A”)
----End of Task 1 ----------
Task 2: Variable Declaration [5 Marks]
Create a program to declare three variables and assign integer, float and string values
of your choice. Then print the values of all three variables.
height = 145.2
age = 15
name = “Tom”
print(height ,age, name)
----End of Task 2 ----------
Task 3: Getting input [5 Marks]
Write a program to prompt the user for two numbers, and calculate the sum of two
numbers. Then print out a sentence stating the sum. For example, if the user entered
2 and 3, you would print ‘The sum of 2 and 3 is 5.
Page No:1
NPS International School
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
answer = num1+num2
print("The sum of",num1,"and",num2,"is",answer)
------End of Task 3 ----------
Task 4: Decision Making
1. Write a program to input two integers and print out the smallest. [5 Marks]
a = int(input("Enter first number"))
b = int(input("Enter second number"))
if (a<b):
print(a,"is the smallest number")
else:
print(b,"is the smallest number")
2. Write a program to accept two integers and then print out either “the numbers
are equal” or “the numbers are not equal”. [5 Marks]
a = int(input("Give value for A"))
b = int(input("Give value for B"))
if (a==b):
print("Numbers are equal")
else:
print("Numbers are not equal")
------End of Task 4 ----------
Page No:2
NPS International School
Task 5: Looping
1. Create a program that prints numbers from 1 to 50 using while loop. [5 Marks]
start = 1
stop = 50
while(start <=stop):
print(start)
start = start +1
2. Create a program that prints numbers from 1 to 50 using for loop. [5 Marks]
for start in range(1,51):
print(start)
------End of Task 5 ----------
Task 6: Functions: [5 Marks]
Create a function called ‘savings’ with two parameters pocket_money & spending.
Then calculate money_in_hand by subtracting these parameters using the function
‘savings’.
def savings(pocket_money,spending):
money_in_hand = pocket_money-spending
print(money_in_hand)
savings(100,25)
savings(150,45)
------End of Task 6 ----------
Page No:3
NPS International School
String Functions:
A string is a list of characters in order. A character is anything you can type on the
keyboard in one keystroke, like a letter, a number, or a backslash. Strings can have
spaces: "hello world". An empty string is a string that has 0 characters. Python
recognize as strings everything that is delimited by quotation marks (" " or ' ').
Creation word = "Hello World"
print( word)
Hello World
Accessing : Use [ ] to access word = "Hello World"
characters in a string letter=word[0]
print(letter)
H
Length word = "Hello World"
print(len(word))
11
Finding word = "Hello World"
print word.count('l')
# count how many times l is in the
string
3
Split Strings word = "Hello World"
print(word.split(' ')) # Split on
whitespace
['Hello', 'World']
Page No:4
NPS International School
Changing Upper and Lower Case String1 = "Hello World"
Strings print( string1.upper())
HELLO WORLD
print(string1.lower())
hello world
print(string1.title())
Hello World
print(string1.capitalize())
Hello world
print(string1.swapcase())
hELLO wORLD
print(string1.replace(“World”,”Python”)
Hello Python
Task 7: String Manipulation [5 Marks]
Write a program to input a sting of your choice and perform the following string
operations:
a. Change to lowercase letters
b. Capitalize the first character of each word
c. Calculate the length of the string
d. Access the character in 4th place of your string.
e. Count how many times the character ‘a’ is appearing in your string.
word = "Hello world"
a. print( word.lower())
b. print (word.title())
c. print(len(word))
d. print(word[4])
e. print (word.count('a'))
------End of Task 7 ----------
Page No:5