You are on page 1of 6

Q-1 (B) Write a program that calculates and prints the number of minutes in a year.

Solution:
Days=365
Hours=24
Minutes=60
Print(‘Number of Minutes in a year:’ , Days* Hours*Minutes)

Q-2 (B) Write a program that accepts the lengths of three sides of a triangle as inputs.
The program output should indicate whether or not the triangle is a right triangle.
Solution:

Take the Input side of triangle as A , B ,C


if ( A > B)
{ if ( A > C)
{ if ( A² = B² + C²)
{ print :Triangle is right angle triangle}
Else { print :Triangle is not right angle tringle}
Elseif ( C² = B² + A²)
{ print :Triangle is right angle triangle}
Else { print :Triangle is not right angle triangle}
}
Elseif ( B > C )
if ( B² = C² + A²)
{ print :Triangle is right angle triangle}
Else { print :Triangle is not right angle triangle}
Elseif ( C² = B² + A²)
{ print :Triangle is right angle triangle}
Else { print :Triangle is not right angle triangle}
}
End

Q-2 (C) Write a python script to Convert Decimal number to Binary number
Solution:

number = int(input("Enter any decimal number: "))


while(number >1)
if number > 1:
decimalToBinary(number // 2)
print(number % 2, end='')

OR Logic 2

def DecimalToBinary(num):
if num > 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')
# main
if __name__ == '__main__':
dec_val = 35
DecimalToBinary(dec_val)
Q-3 (b) Define a function named even. This function expects a number as an argument
and returns True if the number is divisible by 2, or it returns False otherwise.
Solution:
num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num):

if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")
find_Evenodd(num);//function call

Q-3 (C) Write a Python function to calculate the factorial of a number (a nonnegative
integer). The function accepts the number as an argument.
Solution:

Logic 1
def factorialUsingWhileLoop(n):
fact = 1
while(n>1):
fact = fact*n
n=n-1
print('Factorial is %d'%(fact))
if __name__== "__main__":
factorialUsingWhileLoop(4)

Logic 2

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))

Q-4 (B) Assume that the variable data refers to the dictionary {'b':20, 'a':35}. Write the
values of the following expressions: a. data['a'] b. data.get('c', None) c. len(data) d.
data.keys()

Solution:
A. 35
B. None
C. 2
D. Dict_keys([‘a’,’b’])
Q-4(c) Write a Python function that checks whether a passed string is palindrome or
not
Solution:
# function to check string is
# palindrome or not
def isPalindrome(str):

# Run loop from 0 to len/2


for i in range(0, int(len(str)/2)):
if str[i] != str[len(str)-i-1]:
return False
return True

# main function
s = "malayalam"
ans = isPalindrome(s)

if (ans):
print("Yes")
else:
print("No")

Q-5 (C) Write a Python program to copy the contents of a file to another file.
Solution:
Logic 1:
with open("hello.txt") as f:
with open("copy.txt", "w") as f1:
for line in f:
f1.write(line)
Logic 2:
new_file = open("copy.txt", "w")
with open("hello.txt", "r") as f:
new_file.write(f.read())
new_file.close()

Q6 (C) Write a python program to find the longest words in a read file.
Solution:

def longest_word(filename):
with open(filename, 'r') as infile:
words = infile.read().split()
max_len = len(max(words, key=len))
return [word for word in words if len(word) == max_len]

print(longest_word('test.txt'))
Q-7(b) Write a micropython script to Blink led pin connected to GPIO digital pin 1 to 4
of ESP8266 continuously.

Solution:

import machine
import time
led1 = machine.Pin(1, machine.Pin.OUT);
led2 = machine.Pin(2, machine.Pin.OUT);
led3 = machine.Pin(3, machine.Pin.OUT);
led4 = machine.Pin(4, machine.Pin.OUT);

while True:
led1.high();
led2.high();
led3.high();
led4.high();
time.sleep(0.5);
led1.low();
led2.low();
led3.low();
led4.low();
time.sleep(0.5);

Q-7 (C) Write a micropython script to read PIR sensor data for an ESP8266. If value is
high print “Motion detected” otherwise print “Motion Stopped”.
Solution:

from machine import Pin


from time import sleep

motion = False

def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin

led = Pin(12, Pin.OUT)


pir = Pin(14, Pin.IN)

pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)

while True:
if motion:
print('Motion detected', interrupt_pin)
led.value(1)
sleep(20)
led.value(0)
print('Motion stopped!')
motion = False

Q-8 (A) Explain steps in micropython to connect esp module’s wifi.


Solution:

wlan = network.WLAN(network.STA_IF) # create station interface


wlan.active(True) # activate the interface
wlan.connect('essid', 'password') # connect to an AP

Q-8 (B) Write a micro python program: if Button is Connected with Pin number 1 of
ESP8266. When button is pressed toggle the leds connected to pin number 2 and 3.
Solution:

import machine
button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
led2 = machine.Pin(2, machine.Pin.OUT);
led3 = machine.Pin(3, machine.Pin.OUT);

while True:
if not button.value():
print('Button pressed!')
led2.high();
led3.high();
else
led2.low();
led3.low();

Q-8 (C) Write a micropython script to read and print DHT Sensor data for ESP8266
continuously.
Solution:

from machine import Pin


from time import sleep
import dht

sensor = dht.DHT22(Pin(14))
#sensor = dht.DHT11(Pin(14))

while True:
try:
sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
print('Temperature: %3.1f F' %temp_f)
print('Humidity: %3.1f %%' %hum)
except OSError as e:
print('Failed to read sensor.')

You might also like