0% found this document useful (0 votes)
5 views

RevisionPrograms-Python and Microbit-Answers

Uploaded by

saanvianne145
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)
5 views

RevisionPrograms-Python and Microbit-Answers

Uploaded by

saanvianne145
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
You are on page 1/ 4

Python and Micro:bit

Revision questions

1. Write a Python program to print numbers from 1 to 100 using a for loop.

for num in range(1,101,1):


print(num)
2. Write a program that prints the multiplication table of a given number (e.g., 5) using a for
loop.
num=int(input("enter a num"))
for n in range(1,11,1):
print(num,"x",n,"=",num*n)

3. Write a python program to print multiples of 2 and 3 between 1 and 100 numbers using for
loop
for num in range(1,101,1):
if num%2==0 and num%3==0:
print(num," is multiple of 2 and 3")

4. Write a program that appends 5 user-entered words into a list.


Print all the list elements in separate lines using for loop.

wordlist=[]
#input words into the list
for count in range(1,6,1):
word=input("enter a word")
wordlist.append(word)

#print list
for index in range(0,len(wordlist),1):
print(wordlist[index])

5. Write a Python program that takes three numbers as input from the user and calculates the
following expression:
Where a, b, and c are the input numbers.
a=int(input("enter a number"))
b=int(input("enter a number"))

c=int(input("enter a number"))
result=((a+b)*c)/2-b
print("the result is:",result)
6. Write a program that calculates the perimeter and area of a rectangle using the formula:

where l is the length and w is the width.


l=int(input("enter length"))
w=int(input("enter width"))

area=l*w
perimeter=2*(l+w)
print("area is:",area)
print("perimeter is:",perimeter)

7. Write a program that takes three numbers as input and prints the largest of the three using
if-else.
a=int(input("enter a number"))
b=int(input("enter a number"))
c=int(input("enter a number"))
if a>b and a>c:
print(a," is largest")
elif b>a and b>c:
print(b," is largest")
else:
print(c," is largest")

8. Write a Python program that checks if a number is divisible by both 3 and 5 using if-else
conditions.
num=int(input("enter a number"))
if num%3==0 and num%5==0:
print(num," is divisible by both 3 and 5")
elif num%3==0 and num%5!=0:
print(num," is divisible by 3")
elif num%3!=0 and num%5==0:
print(num," is divisible by 5")
else:
print(num," is not divisible by 3 and 5")
9. Write a program that takes a number as input and checks if it is positive, negative, or zero
using if-else.

num=int(input("enter a number"))
if num>0:
print(num, " is positive")
elif num<0:
print(num, " is negative")
else:
print("zero")
10. Write a program that takes the age of a person and prints "You are a minor" if the age is less
than 18, and "You are an adult" otherwise.
age=int(input("enter your age"))
if age>=18:
print("You are an adult")
else:
print("Your are minor")

11. Write a program that takes a string from the user and prints the first three characters using
slicing.
word=input("enter a word")
print(word[0:3])

12. Write a program that combines upper(), lower(), and capitalize() to format a user-input string
and display its length using len().
word=input("enter a word")
newword=word.upper()+word.lower()+word.capitalize()
print(newword)
size=len(newword)
print("length of the new word is:",size)

13. Define a function odd_even(num) to accept a number as parameter and prints whether it is an
even number or odd.

def odd_even(num):
if num%2==0:
print(num," is even")
else:
print(num," is odd")
num=int(input("enter a number"))
odd_even(num)
14. Define a function cube(num) to accept a number as parameter and returns cube of it.
def cube(num):
return num**3

num=int(input("enter a number"))
print("Cube of ",num,"is",cube(num))
15. Write a program to store any 5 numbers into a list, print sum of all even numbers in it.
num=[4,5,6,7,8]
total=0
for index in range(0,len(num),1):
if num[index]%2==0:
total=total+num[index]
print("sum of all even numbers",total)
16. Write a program in microbit to display a number between 1 to 10. When button A is pressed
display the number and when button B is pressed ‘Say’ the number.

# Imports go at the top


from microbit import *
import random
import speech
# Code in a 'while True:' loop repeats forever
num=random.randint(1,10)
while True:

if button_a.was_pressed():
display.scroll(num)

if button_b.was_pressed():
speech.say(str(num))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy