I am taking my very first programming class ad kind of stump on the problem mention in this post.. In coding I only got so far as what shows here....

  1. Write Python statements corresponding to the following:
    a. Assign to variable flowers a list containing strings 'rose', 'bougainvillea', 'yucca', 'marigold','daylilly', and 'lilly of the valley'.
    b. Write a Boolean expression that evaluates to True if string 'potato' is in list flowers, and evaluate the expression.
    c. Assign to list thorny the sublist of list flowers consisting of the first three objects in the list.
    d. Assign to list poisonous the sublist of list flowers consisting of just the last object of list flowers.
    e. Assign to list dangerous the concatenation of lists thorny and poisonous.

flowers = ['rose', 'bougainvillea', 'yucca', 'marigold', 'daylilly', 'lilly of the valley']
'potatoe' in flowers
False

Some hint that should help you.

>>> lst = [1,2,3,4,5,6]
>>> lst[:3]
[1, 2, 3]

#Assign to list thorny
>>> help(lst.append)
Help on built-in function append:

append(...)
    L.append(object) -- append object to end
#Or
>>> help(lst.extend)
Help on built-in function extend:

extend(...)
    L.extend(iterable) -- extend list by appending elements from the iterable

>>> #Concatenation of lists?
>>> ['Hello'] + ['world']
['Hello', 'world']

Hint -->

flowers = ['rose', 'bougainvillea', 'yucca', 'marigold',
    'daylilly', 'lilly of the valley']

flowers2 = ['rose', 'bougainvillea', 'yucca', 'marigold',
    'potato', 'daylilly', 'lilly of the valley']

print('potato' in flowers)   # False
print('potato' in flowers2)  # True

Please show some honest coding effort yourself, or you learn nothing and will be just another Lance Armstrong.

Thanks Snippsat, your solutions highlights some of the things which I learned last week in my first programming class. I was totally stumped... Though the professor said NO to advance slicing technique (:) This is a very basic intro course to programming. Append command is the way to go; I wil try it!

>>> flowers = ['rose', 'bougainvillea', 'yucca', 'marigold', 'daylilly', 'lilly of the valley']
>>> 'potatoe' in flowers
False
>>> thorny = ['rose', 'bougainvillea', 'yucca']
>>> thorny
['rose', 'bougainvillea', 'yucca']
>>> poisonous = ['lilly of the valley']
>>> poisonous
['lilly of the valley']
>>> dangerous = thorny + poisonous
>>> dangerous
['rose', 'bougainvillea', 'yucca', 'lilly of the valley']
>>> 

Snippsat, I didnt to use append either.... still waiting to hear back from my prof. My solution seems to easy to be true, can it be?!! It did answer everything in the assignment (scratching head)

I assume your teacher wants you to use Python code to assign the first three flowers to a sublist.
That's where append could be used.

That's right, he does want us to use the append function.... I struggled with the appending business a bit!!

>>> flowers = ['rose', 'bougainvillea', 'yucca', 'marigold', 'daylilly', 'lilly of the valley']
>>> thorny = []
>>> #First element in flowers list
>>> flowers[0]
'rose'

>>> #append first element to thorny list
>>> thorny.append(flowers[0])
>>> thorny
['rose']
>>> thorny.append(flowers[1])
>>> thorny
['rose', 'bougainvillea']
>>> thorny.append(flowers[2])
>>> thorny
['rose', 'bougainvillea', 'yucca']

>>> #Remember you can use help or look at Python doc
>>> help(thorny.append)
Help on built-in function append:

append(...)
    L.append(object) -- append object to end

>>> dir(thorny) 
#All function/methods you can use on list,look away from magic methods start with __.

flowers = ['rose', 'bougainvillea', 'yucca', 'marigold', 'daylilly', 'lilly of the valley']
print(flowers[-1])

flowers = ['rose','bougainvillea','yucca','marigold','daylilly','lilly of the valley']

potato = 'ptato' in flowers

#python slicing
thorny = flowers[:3]

poisonous = [flowers[-1]]


dangerous = thorny+poisonous

So first off, this is the module, NOT the shell. usually, I don't like writing in code live, more used to C++. I too have the same homework problem and JUST figured it out.
I was soo mad because I was trying what that other guy mentioned above but was getting a concatenation error. & i didnt want to use the append or insert function because it alters my 'thorny' & 'poisonous' variables. so i figured out, I couldn't concatenate a list & a string so I forced 'poisonous' into a list by adding brackets. The reason 'poisonous' was a string instead was because only one variable was fed unlike 'thorny' which has 3 variables fed. Feel free to observe, it was soo simple :D

I couldn't concatenate a list & a string so I forced 'poisonous' into a list by adding brackets

A more common way is

poisonous = flowers[-1:]

Notice that this gives an empty list if flowers is empty, unlike your code which raises an exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.