Grade 7 ICT Python Task Solution
Grade 7 ICT Python Task Solution
Python – Solutions:
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”
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
answer = num1+num2
if (a<b):
else:
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]
if (a==b):
else:
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]
print(start)
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)
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 ' ').
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
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
Write a program to input a sting of your choice and perform the following string
operations:
Page No:5