embedded system,Arduino,Raspberry pi,ARM7,MM32,STM32,PIC and Python,Django,Datascience and web development
Friday, March 27, 2020
threading example in python
from threading import *
import time
e=Event()
def consumer():
print("consumer thread is waiting for updation")
e.wait()
print("consumer got notification and consuming items")
def producer():
time.sleep(5)
print("producer thread is producing items")
print("producer thread giving notification by setting event")
e.set()
t1=Thread(target=producer)
t2=Thread(target=consumer)
t1.start()
t2.start()
output:
consumer thread is waiting for updation
producer thread is producing items
producer thread giving notification by setting event
consumer got notification and consuming items
import time
e=Event()
def consumer():
print("consumer thread is waiting for updation")
e.wait()
print("consumer got notification and consuming items")
def producer():
time.sleep(5)
print("producer thread is producing items")
print("producer thread giving notification by setting event")
e.set()
t1=Thread(target=producer)
t2=Thread(target=consumer)
t1.start()
t2.start()
output:
consumer thread is waiting for updation
producer thread is producing items
producer thread giving notification by setting event
consumer got notification and consuming items
number guess game using python
'''
You decide you want to play a game where you are hiding
a number from someone. Store this number in a variable
called 'answer'. Another user provides a number called
'guess'. By comparing guess to answer, you inform the user
if their guess is too high or too low.
Fill in the conditionals below to inform the user about how
their guess compares to the answer.
'''
answer = int(input("enter a number as ans"))#provide answer
guess =int(input("enter a number as guess")) #provide guess
if answer > guess :
#provide conditional
result = "Oops! Your guess was too low."
elif answer < guess : #provide conditional
result = "Oops! Your guess was too high."
elif answer == guess : #provide conditional
result = "Nice! Your guess matched the answer!"
print(result)
output:
enter a number as ans56
enter a number as guess47
Oops! Your guess was too low.
You decide you want to play a game where you are hiding
a number from someone. Store this number in a variable
called 'answer'. Another user provides a number called
'guess'. By comparing guess to answer, you inform the user
if their guess is too high or too low.
Fill in the conditionals below to inform the user about how
their guess compares to the answer.
'''
answer = int(input("enter a number as ans"))#provide answer
guess =int(input("enter a number as guess")) #provide guess
if answer > guess :
#provide conditional
result = "Oops! Your guess was too low."
elif answer < guess : #provide conditional
result = "Oops! Your guess was too high."
elif answer == guess : #provide conditional
result = "Nice! Your guess matched the answer!"
print(result)
output:
enter a number as ans56
enter a number as guess47
Oops! Your guess was too low.
if elif else in python
"""
Points Prize
1 - 50 wooden rabbit
51 - 150 no prize
151 - 180 wafer-thin mint
181 - 200 penguin
"""
num=int(input("enter a number"))
if num > 1 and num <=50 :
print("Congratulations! You won a wooden rabbit!")
elif num > 50 and num <=150:
print("Oh dear, no prize this time.")
elif num > 150 and num <=180:
print("Congratulations! You won a wafer-thin mint!")
elif num > 180 and num <=200:
print("Congratulations! You won a penguin")
else :
print("Oh dear, no prize this time.")
output:
enter a number173
Congratulations! You won a wafer-thin mint!
Points Prize
1 - 50 wooden rabbit
51 - 150 no prize
151 - 180 wafer-thin mint
181 - 200 penguin
"""
num=int(input("enter a number"))
if num > 1 and num <=50 :
print("Congratulations! You won a wooden rabbit!")
elif num > 50 and num <=150:
print("Oh dear, no prize this time.")
elif num > 150 and num <=180:
print("Congratulations! You won a wafer-thin mint!")
elif num > 180 and num <=200:
print("Congratulations! You won a penguin")
else :
print("Oh dear, no prize this time.")
output:
enter a number173
Congratulations! You won a wafer-thin mint!
dictionary in python
elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},
'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}
print(elements)
print("\n hydrogen:",elements['hydrogen'])
print("\n hydrogen_no:",elements['hydrogen']['number'])
print("\n hydrogen_weight:",elements['hydrogen']['weight'])
print("\n hydrogen_symbols:",elements['hydrogen']['symbol'])
print("\n helium:",elements['helium'])
output:
{'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'}, 'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}
hydrogen: {'number': 1, 'weight': 1.00794, 'symbol': 'H'}
hydrogen_no: 1
hydrogen_weight: 1.00794
hydrogen_symbols: H
helium: {'number': 2, 'weight': 4.002602, 'symbol': 'He'}
example 2:
cast = {
"Jerry Seinfeld": "Jerry Seinfeld",
"Julia Louis-Dreyfus": "Elaine Benes",
"Jason Alexander": "George Costanza",
"Michael Richards": "Cosmo Kramer"
}
for key in cast:
print(key)
output:
Jerry Seinfeld
Julia Louis-Dreyfus
Jason Alexander
Michael Richards
Subscribe to:
Posts (Atom)
-
How to do the programming python in windows command prompt Step 1: please go through below link website f...
-
Serial.print() Description Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers a...