EXPERIMENT 02
OBJECTIVE
Run some python programs on Pi like:
a) Read your name and print Hello message with name
b) Read two numbers and print their sum, difference, product and division
c) Word and character count of a given string
d) Area of a given shape (rectangle, triangle and circle) reading shape and appropriate
values from standard input
SOFTWARE/COMPONENTS USED
1. PC with Arduino Thonny software
2. Raspberry Pi Pico (RP2040) board
3. Micro USB cable
a) Read your name and print Hello message with name
CODE
name = input("Enter your name: ")
print("Hello", name + "!")
RESULT
b) Read two numbers and print their sum, difference, product and division
CODE
num1 = int(input("Enter First Number: ")) num2 =
int(input("Enter Second Number: ")) print("Enter which
operation would you like to perform?") ch = input("Enter any of
these char for specific operation +,-,*,/: ") result = 0 if ch == '+':
result = num1 + num2 elif
ch == '-':
result = num1 - num2 elif
ch == '*':
result = num1 * num2 elif
ch == '/':
result = num1 / num2 else:
print("Input character is not recognized!") print(num1,
ch , num2, ":", result)
RESULT
c) Word and character count of a given string
CODE
test_string = "Geeksforgeeks is best Computer Science Portal"
print("The original string is : " + test_string) # using split()
# to count words in string
res = len(test_string.split())
print("The number of words in string are :" +str(res)) print("The
number of words in string are :", len(test_string))
RESULT
d) Word and character count of a given string Area of a given shape (rectangle, square, triangle,
circle and parallelogram) reading shape and appropriate values from standard input
CODE
from standard input def
calculate_area(name):
name = name.lower() if
name == "rectangle":
l = int(input("Enter rectangle's length: ")) b
= int(input("Enter rectangle's breadth: "))
rect_area = l * b
print("The area of rectangle is: " + str(rect_area))
elif name == "square": s = int(input("Enter
square's side length: ")) sqt_area = s * s
print("The area of square is: " +
str(sqt_area))
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length:
")) tri_area = 0.5 * b * h
print("The area of triangle is: " + str(tri_area))
elif name == "circle":
r = int(input("Enter circle's radius length: "))
pi = 3.14 circ_area = pi * r * r
print("The area of circle is: " + str(circ_area))
elif name == 'parallelogram':
b = int(input("Enter parallelogram's base length: ")) h
= int(input("Enter parallelogram's height length: "))
para_area = b * h
print("The area of parallelogram is: " + str(para_area))
else:
print("Sorry! This shape is not available")
# driver code if
name == " main " :
print("Calculate Shape Area")
shape_name = input("Enter the name of shape whose area you want to find: ")
# function calling calculate_area(shape_name)
RESULT
PROCEDURE
Click the run button and save it on the Raspberry Pi Pico board. Save the file as main.py.
RESULT
We have successfully run python programs on Raspberry Pi.