Can a list comprehension call a function?

Question

In this exercise, the variable from the list comprehension is used to calculate a value. Is it possible to call a function using the variable?

Answer

Yes, the variable in the for of a list comprehension can be used as a parameter to a function. In this example, the variable number is passed to randint() to calculate a random number between number and 2 * number for the list produced by the comprehension.

from random import randint

random_numbers = [ randint(number, 2 * number) for number in range(10)]

print(random_numbers)
10 Likes

7 posts were split to a new topic: How can I round a decimal?

2 posts were split to a new topic: What is my code?

randint()?

honestly think codeacademy need to help some of us learn well by avoiding arbitrary introduction of new concepts. I am sorry for the use of the word arbitrary if it sounds odd. Though, i have come across it somewhere, However, it brings a lot of confusion and mix up if you chose the route of bringing up different concept at anytime. It might also do us good if you give heads up on some of the things we need to look at or read somewhere else since you are yet to teach us and you want to make use of it in the response to the question you posted. In most cases, the new concepts or seemingly more advanced than what we have learnt so far stem for the questions posted by codeacademy. Please help us learn better.

I must also mention that based on the comment of most students, there seems to be similarity in the complain especially in this regard. Codeacademy no doubt has a very good platform to learnā€¦ But if this is not really looked into, purpose might be defeated. I honestly love you guys. But i think i have started building this feeling that ā€œi will be frustratedā€ anytime i want to take lessons. Please look into this

Please note that my submission might be wrong and i will be happy if corrected

Thank you

36 Likes

randint

3 Likes

2 posts were split to a new topic: Typo in lesson instructions - more list comprehensions

If this is for the fahrenheit list comprehension challenege, what i did was:

def temperature_in_fahrenheit(temperature_in_celsius):
  return temperature_in_celsius * 9/5 + 32
fahrenheit = [temperature_in_fahrenheit(temp) for temp in celsius]
1 Like

This is exactly why this is my last month of Codecademy, if every couple of exercises I have to google for information I can learn with some free Python course like on Pythonā€™s reddit and compiler. Might come back in a year when Iā€™m ready for machine learning but right now when it costs 7% of my salary itā€™s just not worth it.

Additionaly most of the complaints I have are voiced by other people so they KNOW abut the issue, they choose not to do anything about it, so really, my mony will be better spent elsewhere.

1 Like

Thatā€™s what self-learning is about. All the information we need cannot be compressed into a single course, and starts with a lot of terminology to get a handle on, as well as grappling with concepts that eventually prove themselves to be very simple, on the face of it. Learning involves struggle, confusion, frustration, exasperation, exhaustion, and yes, lots of failure.

Whatever language we are learning there are official documentation sites for all of them. python.org, docs.python.org, wiki.python.org, are all possible URLs for Python docs. Often the lesson will not cover every aspect of a concept, nor put into practice the more intuitive avenues we can travel down.

Take for instance the range() function. This came up in another topic, yesterday. The problem has us write a function that will return a list of all the numbers between an input value and 100, inclusive, counting in threes.

The naive approach we typically see implemented always uses an if statement to qualify the input.

if start < 101:
    # return a list
else:
    # return an empty list

which often looks similar to thisā€¦

if start < 101:
    return list(range(start, 101, 3))
else:
    return []

This does not take advantage of the behavior of the range function, which is to return an empty list if the bounds are exceeded.

return list(range(start, 101, 3))

accomplishes the same thing without the if statement. If this is not explored in a lesson then it is left to the learner to discover how this behavior can be used in place of explicit logic.

Bottom line, a lesson may omit intuitive aspects which can only be gleaned from reading the documentation to learn what a function actually does, and practice so we can see it in action in all manner of scenarios. Whatever investment we make in ourselves is never wasted if we do not walk away. Take responsibility for doing the leg work, and call the course what it is, a starter, or introduction that only gets us out of the gate. Itā€™s up to us to set the pace after that.

17 Likes

want to say this comment is spot onā€¦ you market yourselves as open to all levels ,i am totally new to coding and find your explanations extremely frustrating sometimes. I understand you have people on the course that have been doing coding for years. When you devised your course did you do it alongside someone who has never done coding before and then asked them each timeā€™ do you understand what we are trying to say hereā€¦? ā€™ orā€¦ā€˜does this comment make sense to you hereā€¦?ā€™ That would ensure your comments and teaching is reaching everyone.

3 Likes

Of course you will see the naive approach - that is the one approach that has been shown, so the assumption is that is the correct approach, and thereā€™s no reason to GO looking as a beginner.

I donā€™t disagree with the idea of self-learning at all; I encourage it. But when you are arbitrarily asked to provide an answer in a lesson or quiz using an approach that hasnā€™t even been introduced, thatā€™s not encouraging self-learning, thatā€™s instigating frustration unnecessarily.

The coding challenges are great calls to self-learn - by their nature, you are expected to respond to them AS A CHALLENGE, which often means taking further steps with what youā€™ve already learned to move forward. On the other hand, quizzes and exercises arenā€™t necessarily the time for that; those are typically the time in which you ensure you understand the information youā€™ve actually been given so that you have a solid base to continue your self-learning adventures. Do you see the difference?

2 Likes

If you are expecting me to defend the course authors and make excuses for the deficiencies in their lessons, it will be a long time in coming. Weā€™ve seen over the years how some learners stumble and others donā€™t. Every learner is different.

We can only hope that someone at CC will see your comments and perhaps act upon them. Over here on the volunteer side it is all out of our hands. The best we can do is try to ascertain where a learner is having difficulty and why, and then coach them through the rough bits so they get under way on their own steam.

9 Likes

Fair enough! Iā€™m not a rookie, so I know where to find answers when I need them. Iā€™m more concerned about the impact on the true beginner, who doesnā€™t honestly know anything about places like Code Project, Stack Overflow, the online documentation, and so on.

3 Likes

Our ilk on this side might harp a bit much over online resources so I make a point of not trying to post too many links. MDN is my go-to site because it is closely associated with W3C in both compliance and documentation.

SO is great once a person has got rid of the training wheels and is ready to take on the tighter curves. For my own part, not being a true professional (meaning, ā€˜trainedā€™) programmer, I stay in the slow lane with the beginners and stress docs and rudimentary mechanics with a little sniff now and then of what we can actually do. By we, I mean ā€˜us beginnersā€™ in that we are not hogtied just because weā€™re at the beginning. Itā€™s okay to explore and peer down the rabbit hole.

9 Likes

Please do not take my feedback too harshly as itā€™s not meant to be mean. If you struggled with the introduction of randint() even given the context and explanation given in the text, ā€œthe variable number is passed to randint() to calculate a random number between number and 2 * numberā€, then programming may not be for you.

If you rush through these lessons just doing what youā€™re told and not asking questions, testing new ways to get the desired results, or expanding the task beyond the scope, then youā€™re not in the right mindset yet to self-learn. For Example, I was curious if you could nest list comprehension, so hereā€™s my height exercise:

heights = [[161, 164], [156, 144], [158, 170], [163, 163], [157, 199]]

can_ride_coaster = [height for groups in heights for height in groups if height > 161]

print(can_ride_coaster)

After speaking with a few programmers about what their day looks like, it seems to oscillate between identifying a problem you want to solve and googling/searching StackOverflow for all the things you donā€™t know. After that, itā€™s all about asking yourself questions and trying new things.

While every once in a while I run into a bug, they are few and far between. So far, Codecademy has been a great launching point for ideas and, if youā€™re considering the lessons critically, what-if questions. Going out and answering those what-if questions is what will make you a better programmer and foster a much deeper understanding of the material.

This isnā€™t school; you canā€™t learn problem-solving by rote.

8 Likes

Not taking it personal at all. You pulled the bull by the horn which is what we need at times. We are all learners (though at different levels). In most cases, our comment or perspective about issues is based on our level of knowledge about such issue. What appear as simple as it could be to one might be the most complicated to another. We learn everyday and will continue to learn. I want to say though that anyone can learn anything as long as they have the passion, zeal and willing to sacrifice. I have always known this route wasnt going to be smooth, so much hurdle to cross. PROGRAMMING CAN ACTUALLY BE FOR US ALL.

6 Likes

I understand what you are saying, but at the same time the developers for this experience need to understand that for a student that is new to a language, they cannot guess as to the next steps that they need to take in solving a problem when they donā€™t know what they should even be looking for. How can they be aware of what they are note aware of?

Additionally, if new terms or or concepts are broached in the assigned problems but not in the explanations, all this accomplishes is tearing down the students motivation, not building it up. If the point of the courses are to create more programmers out there in the world, then have a complete newbie take each of these courses and you as the architects for the course work keep detailed notes of where problem areas arise that will need better and more thorough explanation. However if the point of the courses are to sow despair and frustration about not understanding a concept, to create a student that is happy to simply get passed a given question and wanting to move on, then you havenā€™t actually accomplished teaching, youā€™ve accomplished complacency with meeting the bare minimums. Just to be sure you understand the difference, the former goes on to learn as much as they can of whatever they can, the latter doesnā€™t. Remember, we as the student came this far, we invested our time and money into this. We have paid to be taught, not to get the crap kicked out of us.

1 Like

I was able to understand the answer given, but I donā€™t see anyone here was asking for everything to be given to them on a plate, (donā€™t want to be mean but that comment is patronising at best, arrogant at worst) not every one is at exactly at same level as you. There is nothing wrong with throwing tangents into learning ā€¦ can be a great way to understand things better but its also fair to say that there is an excessive number for what is for most people a beginning to coding and or python. Also as I think was mentioned many appear to be fairly arbitrary and donā€™t do much to expand learning.

1 Like