Python with AI – Lists
Lists in python
• A list holds ordered collection of items.
• And item can be a string or a number
Lists in python
• A list holds ordered collection of items.
• And item can be a string or a number
Lists in python
• A list holds ordered collection of items.
• And item can be a string or a number
Add an item to a list
• A list holds ordered collection of items.
• And item can be a string or a number
Access an item and delete
• A list holds ordered collection of items.
• And item can be a string or a number
Adding Items - List
Adding items to Lists:
myList = ['apple','mango']
myList.append('orange')
myList.append('grapes')
myList.append('strawberry')
myList.append('kiwi')
print(myList)
# Output:
['apple', 'mango', 'orange', 'grapes',
'strawberry', 'kiwi']
Removing Items - List
Remove Elements in a List:
myList1 = ['a','b','c']
myList1.remove('a')
myList1.remove('b')
myList1.remove('c')
# Output:
[]
Note* : Even though there are no elements
in the list, it still takes up memory, we
can still add elements!
Exercise
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed data types
my_list = [1, "Hello", 3.4]
# nested list (array inside of an array)
my_list = ["mouse", [8, 4, 6], ['a']]
* Question: How can we access "mouse" or ['a']? *
Exercise
list1 = ["Hello ", "Hi"]
list2 = ["Goodbye", "Bye"]
Desired Output:
# Output:
List3 = ['Hello', 'Hi', 'Goodbye',
'Bye']
Exercise
• Combine two lists
list1 = [1, 5, 8, 23]
list2 = [2, 6, 9, 21]
Desired Output:
# Output:
[1, 5, 8, 23, 2, 6, 9, 21]
Loops in flow chart
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a variable len_list that stored the length of the list
Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a variable len_list that stored the length of the list
Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a number a=0
Declare a variable len_list that stores the length of the list
If a >
len_list
Stop a = a+1
value =
list_sample[a]
If value is
a even
number
Print value
True False
False True
Loops in flow chart
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
Loops in flow chart - while
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
Loops in flow chart - while
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
Declare a number a=0
If a >
len_list
Stop a = a+1
value =
list_sample[a]
If value is
a even
number
Print value
True False
False True
Exercise
• Print alternate values of a list
list1 = [1, 5, 8, 23, 10, 4]
Exercise: Design a chat bot - flowchart
• list_happy_words = [“joyful”, “happy”, “amazing”, “beautiful”]
• list_sad_words = [“sad”, “tears”, “crying”, “depressed”]
• list_games = [“tetris”, “flappy bird”, “snake”]
• Ask the user to enter a word that describes their feeling
• If the feeling they enter is one of the list_happy_words, print “Glad you are
happy” and exit.
• If the feeling they enter is one of the list_sad_words, ask the user to play
one of the games from the list list_games.
• Ask the user again how they are feeling and continue till the list of games is
exhausted.
• If you already suggested all the games and the user is still unhappy, exit.
Exercise: Design a chat bot - code
Start
Declare the 3 lists
Ask the user to input a word describing their feeling, save the string
Check if
word is a
part of
happy list
Check if
word is a
part of
sad list
True
Stop
Print
sentence
False
False True
Print
sentence
Print
game
But what if you exhaust the list of
games?
This flowchart does not check for it
Exercise: Design a chat bot - code
Start
Declare the 3 lists
Ask the user to input a word describing their feeling, save the string
Check if
word is a
part of
happy list
Check if
word is a
part of
sad list
True
Stop
Print
sentence
False
False True
Print
sentence
Print
game
Check if
your
games
list is
over
False
Lets try it!
http://aiclub.world

Pa1 lists subset

  • 1.
    Python with AI– Lists
  • 2.
    Lists in python •A list holds ordered collection of items. • And item can be a string or a number
  • 3.
    Lists in python •A list holds ordered collection of items. • And item can be a string or a number
  • 4.
    Lists in python •A list holds ordered collection of items. • And item can be a string or a number
  • 5.
    Add an itemto a list • A list holds ordered collection of items. • And item can be a string or a number
  • 6.
    Access an itemand delete • A list holds ordered collection of items. • And item can be a string or a number
  • 7.
    Adding Items -List Adding items to Lists: myList = ['apple','mango'] myList.append('orange') myList.append('grapes') myList.append('strawberry') myList.append('kiwi') print(myList) # Output: ['apple', 'mango', 'orange', 'grapes', 'strawberry', 'kiwi']
  • 8.
    Removing Items -List Remove Elements in a List: myList1 = ['a','b','c'] myList1.remove('a') myList1.remove('b') myList1.remove('c') # Output: [] Note* : Even though there are no elements in the list, it still takes up memory, we can still add elements!
  • 9.
    Exercise # empty list my_list= [] # list of integers my_list = [1, 2, 3] # list with mixed data types my_list = [1, "Hello", 3.4] # nested list (array inside of an array) my_list = ["mouse", [8, 4, 6], ['a']] * Question: How can we access "mouse" or ['a']? *
  • 10.
    Exercise list1 = ["Hello", "Hi"] list2 = ["Goodbye", "Bye"] Desired Output: # Output: List3 = ['Hello', 'Hi', 'Goodbye', 'Bye']
  • 11.
    Exercise • Combine twolists list1 = [1, 5, 8, 23] list2 = [2, 6, 9, 21] Desired Output: # Output: [1, 5, 8, 23, 2, 6, 9, 21]
  • 12.
    Loops in flowchart • Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
  • 13.
    Loops in flowchart Start Declare list_sample [4, 3, 2, 5, 8] Declare a variable len_list that stored the length of the list
  • 14.
    Loops in flowchart Start Declare list_sample [4, 3, 2, 5, 8] Declare a variable len_list that stored the length of the list
  • 15.
    Loops in flowchart Start Declare list_sample [4, 3, 2, 5, 8] Declare a number a=0 Declare a variable len_list that stores the length of the list If a > len_list Stop a = a+1 value = list_sample[a] If value is a even number Print value True False False True
  • 16.
    Loops in flowchart • Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
  • 17.
    Loops in flowchart - while • Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
  • 18.
    Loops in flowchart - while • Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers Declare a number a=0 If a > len_list Stop a = a+1 value = list_sample[a] If value is a even number Print value True False False True
  • 19.
    Exercise • Print alternatevalues of a list list1 = [1, 5, 8, 23, 10, 4]
  • 20.
    Exercise: Design achat bot - flowchart • list_happy_words = [“joyful”, “happy”, “amazing”, “beautiful”] • list_sad_words = [“sad”, “tears”, “crying”, “depressed”] • list_games = [“tetris”, “flappy bird”, “snake”] • Ask the user to enter a word that describes their feeling • If the feeling they enter is one of the list_happy_words, print “Glad you are happy” and exit. • If the feeling they enter is one of the list_sad_words, ask the user to play one of the games from the list list_games. • Ask the user again how they are feeling and continue till the list of games is exhausted. • If you already suggested all the games and the user is still unhappy, exit.
  • 21.
    Exercise: Design achat bot - code Start Declare the 3 lists Ask the user to input a word describing their feeling, save the string Check if word is a part of happy list Check if word is a part of sad list True Stop Print sentence False False True Print sentence Print game But what if you exhaust the list of games? This flowchart does not check for it
  • 22.
    Exercise: Design achat bot - code Start Declare the 3 lists Ask the user to input a word describing their feeling, save the string Check if word is a part of happy list Check if word is a part of sad list True Stop Print sentence False False True Print sentence Print game Check if your games list is over False
  • 23.