Learn - Practice makes Perfect


#1

Hi I am stuck on this section

this is my code

to_21=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
odds =(1,3,5,7,9,11,13,15,17,19,21)
middle_third = (8,9,10,11,12,13,14)
print to_21
print odds
print middle_third

**This is what it prints **

(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21)
(8, 9, 10, 11, 12, 13, 14)

**This is what it asks you to do **

Create a list, to_21, that’s just the numbers from 1 to 21, inclusive.

Create a second list, odds, that contains only the odd numbers in the to_21 list (1, 3, 5, and so on). Use list slicing for this one instead of a list comprehension.

Finally, create a third list, middle_third, that’s equal to the middle third of to_21, from 8 to 14, inclusive.


#2

are you familiar with type() function? It gives use the data type:

to_21=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
print type(to_21)

which clearly outputs to_21 is a tuple, tuple != list

You really shouldn’t hard code the numbers, its really bad practice

which function did we learn which gives us a range of numbers?

for odds and middle_third lists, you really should use list slicing. We are programming, hard coding all the numbers is really bad programming design


#3

I tried the range but it said to " Use list slicing for this one instead of a list comprehension."

also I tried the print type (to_21) i did it for all of them but it still didnt work


#4

range should only be used for creating to_21, for odds list and middle_third you should use list slicing. Did you? Show me

that was only to show the data type you use is wrong, that you created tuples and not list.


#5

to_21=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
odds =(1,3,5,7,9,11,13,15,17,19,21)
middle_third = (8,9,10,11,12,13,14)
print type (to_21)
print type (odds)
print type (middle_third)

This is what I did but it didnt work

also i thought that comprehension was the range and I thought slicing was listsing all the numbers


#6

as the type() function shows you, to_21, odd and middle_third are all of the wrong data type

no range() is used for to_21

no list slicing is used for odds and middle_third

If you don’t know what data types are at this point in the course, you have a big problem. That might sound rude, but its the truth. Data types where introduced in the first track, you are currently in track 17.


#7

I changed it to this but its still wrong

to_21 = range(1,21)
odds =(1,3,5,7,9,11,13,15,17,19,21)
middle_third = (8,9,10,11,12,13,14)
print to_21
print odds
print middle_third


#8

odds and middle_third are still tuples, you should really use list slicing to get the values from to_21 list

https://docs.python.org/2.3/whatsnew/section-slices.html


#9

I dont know what you mean


#10

Did you check the list slicing doc i posted? You should use list slicing to get odds and middle_third from to_21 list

Why are you so struggling? What is wrong with my/our explanations? They don’t seem to come across


#11

I know what you mean the [::3]
that one ???

also which one do i use


#12

This is my work

to_21 = range(1,22)
odds = [1:3:5]
middle_third = [1:3:5]
print to_21
print odds
print middle_third


#13

I give up, i just don’t know but it seems my explanations aren’t coming across for some reason


#14

Okay so, here’s the pseudo code (non-coded instructions for a particular algorithm)

  • create a variable to_21 and set it to a range of 1, 21 -> range(1, 22)

  • create a variable odds using list slicing eg. [::2] every other item in the list…that’s a data type

  • create a variable called middle_third which is 8 to 14, but minus 1 on the first number since it’s index-based -> 7

  • everything above is going off of the to_21 variable, so don’t forget that!!! :smiley: -> odds = to_21[your_code]

Hope this clears everything up


#15

@stetim94 did a great job getting you this far. Let me see if I can help you get a little further.

You currently have this code:

The First List

to_21 is correct. The point was to use range instead of hard coding in a set of numbers. Let the code generate those numbers for you.

The second list

Here are the instructions:

Create a second list, odds, that contains only the odd numbers in the to_21 list (1, 3, 5, and so on). Use list slicing for this one instead of a list comprehension.

Notice that it says "only the odd numbers in the to_21 list. That means you want to slice out the odd numbers from theto_21` list.

odds = to_21[start:end:stride]

You need to fill in the slice part of the above code. If you want the odd ones from the list of 1-21, how could you step through that list and slice out every other one?

Hint Hint: Go back to List Slicing Syntax and the answer is right there in the last sentence of the description:

Where start describes where the slice starts (inclusive), end is where it ends (exclusive), and stride describes the space between items in the sliced list. For example, a stride of 2 would select every other item from the original list to place in the sliced list.

Think about it. You want to start at the beginning of the list, go to the very end of the list, and then select every other one.

Middle List

The last list is asking you to slice out the middle of to_21 from 8 to 14 inclusive. You want to end at 14. But how about the start? “inclusive” means we want to include 8 also. Therefore, what would the start be, 7 or 8? Try it and see:

print to_21[7:14]
print to_21[8:14]

which one of the above slices gets you a list that goes from 8 to 14, i.e. [8,9,10,11,12,13,14]?


#16

Its ok I read the thing again and re did the code and it worked!!

Also I have finished the course!!!


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.