Unit 1: ARTIFICIAL INTELLIGENCE
What is Artificial intelligence?
python dictionary
A dictionary is an unordered and changeable sequence datatype that stores mappings of unique keys to values. Dictionary does not allow duplicates. Dictionaries elements are stored within curly brackets ({}), including key-value pairs separated by commas (,). In a dictionary, you can add, change or remove elements, also you can loop, copy and nest a dictionary.
PROGRAM EXAMPLES
Lab1
| 
   # a python
  program to display the position number (key) and reward (value) # using
  dictionary position = {     1 : "Gold",     2 : "Silver",     3 : "Bronze" } print(position) 
 #display the 2
  value of the dictionary print(position[2])      #output: Silver  | 
 
Lab2
| 
   # a python
  program to display the position number (key) and reward and cash prize
  (values) # using
  dictionary position_data
  = {     1 : "Gold, #500000",     2 : "Silver, #250000",     3 : "Bronze, #125000" } print(position_data)             #output: {1:
  'Gold, #500000', 2: 'Silver, #250000', 3: 'Bronze, #125000'} 
 #display the 1
  value of the dictionary print(position_data[1])         #output: Gold, #500000 
 #display the
  keys of the dictionary print(position_data.keys())       #output: dict_keys([1, 2, 3]) 
 #alternatively mykey =
  position_data.keys() print(mykey) 
 #display the
  length of the dictionary print(len(position_data))       #output: 3 
 #display the
  datatype of the dictionary print(type(position_data))       #output: <class 'dict'> 
  | 
 
Lab3
| 
   # a python program
  to display the country name (key) and capital (value) # using
  dictionary 
 country = {     "Nigeria" : "Abuja",     "Ghana" : "Accra",     "South Africa" :
  "Pretoria" } 
 print(country) 
 #display the
  South Africa value of the dictionary print(country["South
  Africa"])      #output: Pretoria 
 #display a new
  view of the dictionary print(country.items())      #output: Pretoria 
 #display the
  first N key-value pairs and above y =  list(country.items())[:1]      # test 0, 1, 2 print(y)                           #output:
  [('Nigeria', 'Abuja')]  | 
 
Lab4
| 
   # a python
  program to display the continent (key) # total
  countries in each continent (value) # using dictionary 
 
 continent_data
  = {     "Asia" : 48,     "Africa" : 54,     "Antarctica" : 0,     "Australia" : 6,     "Europe" : 50,     "North America" : 23,     "South America" : 12 } 
 print(continent_data)  | 
 
Lab5
| 
   # a country in
  each continent # a python
  program to display the country name (key) # country
  features such as capital, GDP (as at 2021), population, ethnic groups
  (values) # using
  dictionary # storing list
  inside a dictionary 
 country_data =
  {     "India" : "New Delhi,
  3.176 trillion USD (2021), 1.408 billion, 2000+",     "Nigeria" : "Abuja, 440.8
  billion USD, 213.4 million, 250+",    
  "Research_Station_In_Antarctica" : "Maitri and Dakshin
  Gangotri",     "Austrialia" : "Canberra,
  1.553 trillion USD, 25.69 million , 270+",     "United_Kingdom" :
  "London, 3.131 trillion USD, 67.33 million, 19",     "USA" : "Washington DC,
  23.32 trillion USD, 450 million, 6",     "Brazil" : "BrasÃlia,
  1.609 trillion USD, 214.3 million, 305+ ",     "independence_year": [1947,
  1960, "", 1901, 1776,  1776,
  1822] } 
 print(country_data)  | 
 
Lab6
| 
   # a python
  program to display my details # using dictionary # search and
  print matching key-value pairs # concepts
  applied: for loop statement to iterate, # if
  statememnt to check condition #str python
  datatype to convert dict datatype of value to string 
 myDetails = {     "Name" : "Ajala
  Theophilus",     "Favourite_color" :
  "Blue",     "Youtube_handle" :
  "dmactutor",     "Website" :
  "whitepaceblog.blogspot.com",     "Age" : 18,     "Area_of_interest" : "Data
  science, Machine Learning, Web Application, Mobile Application" } # display all
  dictionary elements print(myDetails) 
 # print
  key-value pair that meets the condition of value starting with "dm" # indentation
  is very important to adhere to in python when using 'for' and 'if' keywords for mykey,
  myvalue in myDetails.items():     if str(myvalue).startswith('dm'):         print(mykey, myvalue)               #Output: Youtube_handle
  dmactutor  | 
 
Using python dictionary and list sequence datatypes to create a question and answer system
PHASE 1
#Question Base
QnA = {
"Do you experience high temperature": [
"yes", "no"
],
"Do you have dry cough": [
"yes", "no"
],
"Do you experience moderate temperature": [
"yes", "no"
],
"Do you have chest pain": [
"yes", "no"
],
"Do you experience headache": [
"yes", "no"
],
}
#display
print("Hi! I am Whitepace Expert.\n\nYou can be diagnosed here at no charges!\nI will ask you 5 questions.\n")
for question, options in QnA.items():
selected_answer = options[0]
sorted_options = sorted(options)
#enumerate shows the index of each option
for label, option in enumerate(sorted_options):
print(f" {label}) {option}")
answer_label = int(input(f"{question}? "))
answer = sorted_options[answer_label]
PHASE 2
QnA = {
"Do you experience high temperature": [
"yes", "no"
],
"Do you have dry cough": [
"yes", "no"
],
"Do you experience moderate temperature": [
"yes", "no"
],
"Do you have chest pain": [
"yes", "no"
],
"Do you experience headache": [
"yes", "no"
],
}
#display
print("Hi! I am Whitepace Expert.\n\nYou can be diagnosed here at no charges!\nI will ask you 5 questions.\n")
for question, options in QnA.items():
selected_answer = options[0]
sorted_options = sorted(options)
#enumerate shows the index of each option
for label, option in enumerate(sorted_options):
print(f" {label}) {option}")
mapSelectedAnswer = []
answer_label = int(input(f"{question}? "))
answer = sorted_options[answer_label]
print("\n")
if answer == selected_answer:
for xcount in range(0, answer_label):
# delcare a variable stores an empty list, add user input to the list
mapSelectedAnswer.append(answer)
print('\nYou select:: ', mapSelectedAnswer)
Comments
Post a Comment