Graded Assignment Python help for non-programmer

I have spent 6 hours on Exercises 3 & 4 and have ground to a halt. The challenge isn’t the concepts or the exercises–i understand those–it’s the python/jupyter thing. I am not a programmer. I need help with how to structure these python operations (is that the right term?) and what arguments (maybe they are called parameters?) to use.

I don’t know how to debug my work so I can’t tell what’s working or not. The exercise instructions says “Feel free to add a return statement between different matrix operations” but how would I know how to do that or what that would do exactly. I don’t understand what “def” means. It said in the first exercise that I didn’t need to know that.

The description of this course said “This is a beginner-friendly program, with a recommended background of high school mathematics.” Someone left out the programming part of the requirement. At any rate, can you please step me through this or advise a VERY directed tutorial so I can get through the exercises? Just a couple of exercises on how to use the operators and how to use syntax and how to debug would do the trick. Thank you.

Hello there,

It’s unfortunate that the programming concepts needed to to understand and overcome the assignments are slightly over beginner level.

Umm, you can think of def as define.

I’m not trying to promote any website or anything like that, but you may try this one out there - kaggle

Happy learning…^^

Sorry to hear your frustrations with your unfamiliarity with Python. Have you checked out the Ungraded Lab from the same week? The code that you’re asked to produce in Exercise 3 is pretty much in the Ungraded Lab if you check it out. From there, hopefully you can try to piece together what the exercise is asking you to do, as well as get a sense of how the Programming Assignments are structured so that you know what parts to leave alone and what parts to hack away at.

FYI, “def” is a keyword in Python that defines a function. For example, “def MultiplyRow(M, row_num, row_num_multiple):” defines a function called “MultiplyRow” which takes the arguments inside the parentheses and returns a new matrix that has had the appropriate row operations performed on it, as defined in the indented lines of code beneath the definition. Once you finish Exercise 3, you’ll have three functions that should come in handy for you to complete Exercise 4. Again, take a look at the Ungraded Lab to see how you can use these three functions to step-by-step reduce the matrix to REF. Good luck, hope this helps!


Perhaps you should consider just taking an introductory python course first. If your intent in taking M4ML is to later take more ML courses, those will also involve python programming. There are no python courses here from DeepLearning.AI: all the courses here assume you know python as a prerequisite and there are plenty of good python courses already out there on Coursera (and elsewhere) from other course providers. If you are already a proficient programmer in other languages (e.g. Java, JavaScript, C#, c or C++) then you can probably pick up python by just studying a few tutorials. But if this is literally your first exposure to programming, then you really should just take a python course. It’s not a good idea to try to learn on the fly if you have no previous exposure to programming. As you’re finding, it’s just one frustration after another.


I had written a ton of code in a dozen languages but not Python when I started taking these classes several years ago. I still struggled with some Python syntax and style, especially working with multi-dimensional objects. I eventually took a pause in the machine learning stuff and completed the Python for Everybody courses. Python for Everybody | Michigan Online

Those courses cover nooks and crannies of the language you won’t likely use directly in ML, like accessing relational databases, but you can also just take the first two, Getting Started and Python Data Structures and then come back to this. The mentors and community can help, but there is an assumption of familiarity with structuring, running, and fixing Python code that will be hard to bootstrap here.

Thanks for the suggestion of a particular python specialization to take. Your last sentence there makes a really important point: what you learn in a programming course is more than just the syntax and semantics of the language. How to approach debugging your code when it (inevitably) doesn’t work is also an absolutely key part of what you learn in a programming course. I’m retired now and have the grey hair consistent with that. I wrote my first computer program when I was perhaps 16 years old and I spent my entire career as a software engineer or engineering manager of software engineers. In all that time, I can still count on the fingers of one hand the times I wrote a piece of code that wasn’t completely trivial (meaning just one or two lines) that actually worked as written the first time. You always have to debug. It’s not an annoying afterthought: it’s a key part of the process. And it takes some exposure and experience to learn how to do it …

Kaggle is helping. Thank you. I spent an hour and did the first few exercises this morning. I now know how python evaluates functions (right side first, it turns out), what return() does, and what the : and indents are for.


You may already know this, but Python, like most programming languages, has well defined syntax and semantics that are documented here:

3.11.2 Documentation

Can be a little terse, and when its not it can be obtuse :rofl: but it’s a good place to look for insight about what is happening in your code.

Thanks for explaining def. I did complete the ungraded lab first and printed it out, so I could use it as a reference for Exercise 4. I see now that there is a reshape nested inside a hstack that can be used to combine the matrix and vector appropriately. I was trying to use reshape…unsuccessfully. I also mistakenly clicked on HINTS which expanded. So, I now see there are the definitions for lots of these functions. That’s helpful.

I do appreciate your sympathy @paulinpaloalto , but please know programming was NOT stated a pre-requisite, though I agree it should be, or else, explore including more remedial steps and guidance in the notebooks. It’s been two weeks to become evident. I have invested a lot of time in learning. I don’t plan to turn back right now. I will make use of this great group of thinkers here and ping specific challenges as they arise.

Any good resources on debugging you would recommend? I have a feeling I’m going to be doing alottttt of it.

Exercises 4 and 6 both follow this format where they define a function, then they define a variable within that function. Then they define another variable2. Then at the end they set that variable2 equal to the function and then print the variable2. Here’s an example from exercise 4, but same applies to exercise 6:

def augmented_to_ref(A, b):
A_system = _____
A_ref= _____
A_ref= augmented_to_ref(A,b)
print (A_ref)

There’s not an example using this structure that I can follow and I am having trouble understanding why two variables and why set the function to one of the variables at the end? I understand now how to use the built-in python functions but struggling with how to use the variables.

Kaggle explains defining functions and shows an example below:

def least_difference(a, b, c):
diff1 = abs(a - b)
diff2 = abs(b - c)
diff3 = abs(a - c)
return min(diff1, diff2, diff3)

This is super clear, three variables that all get used in a very clear way at the end. but structurally quite different from Exercise 4 & 6 which uses variables differently and basically throws in the equivalent of a "diff3=least_difference(a, b, c) " at the end.

I could just delete out the variables and start transforming the Array one step at a time, but it seems this def thing is important.

It is especially important if that unit of work performed is used more than once. By putting it inside a function definition you have made it reusable…you can repeat the operations without having to repeat the code. In addition, you have now encapsulated the details of how the operations are implemented. In effect, you have separated the interface (the definition of the function and its parameters) from the implementation (the executable statements within the function). This makes your code more modular, more testable, and more maintainable, which are all good things :slight_smile:

I’ll let the course mentors comment on that augmented_to_ref() function (since I’m not taking this course I can’t see the exercise that fragment is based on) but it looks suspicious to me that there is an assignment statement like this

A_ref = augmented_to_ref(A,b)

but nothing is being returned from the function that could be assigned to a variable. I would have expected something like this…

def augmented_to_ref(A, b):
    some_variable = something_else
    return some_variable

In that case, whatever value was in some_variable would be available to assign when the function completes. You can see that happening in your Kaggle example.

The mentors might also want to weigh in on the importance of indentation, and using the code block format here in the forum to help format code fragments. (not available in all browsers - I have it in Chrome on my Mac laptop, but not in Chrome on the iPad)

1 Like

Thanks @ai_curious this helped.

I successfully completed exercises 4 & 5, so that’s progress.

Exercise 6 shows that I both failed it (doesn’t match expected outcome) and passed it (line 95). I’d be astounded if my code is anywhere near correct. Any ideas what’s amiss? Sorry to be such a pest today. I’ve been at these three exercises for 9 hours today, so I am done for today.

So one observation right off the bat: looks like you pass matrix A_ref into a function, make a copy, do some stuff to the copy, return the copy, assign the values of the copy to a new variable, then print out the original matrix A_ref…not the one you did the stuff to and returned. So what you are seeing in the printout isn’t helping you compare with expected output. Given that the unit test passed, I’m inclined to believe you did better than you think :+1:

I changed the print function to print (A_diag)… just for “kicks” and giggles. It’s below the ##end course here## so I am supposed to be using what they gave me. It does look like the matrix is correct. Yay! But I clearly don’t know how to write this code to conform to the course conventions, so I’ve got to figure that part out ASAP.

If print(A_ref) is what is in the exercise code, and not print(A_diag) then to me it looks like a bug that should be reported and fixed. Maybe a mentor for this course can confirm @AeryB

Have a look at this page which is the top level of the description of all the courses and specializations here. Notice that even in the “Introductory” category, they list programming experience as a prerequisite. In the specific course description for M4ML, they don’t talk about programming as a prerequisite and only mention high school math. I think they generally try not to scare off potential customers with too many prerequisites, but I think it would be better if they were a bit more “above board” about this.

Hi @ai_curious

Well, in my lab assignment, it appears as
print(A_diag) not print(A_ref).

Thanks though!

Agreed. The course sign up page should mention coding next to high school math. The page you shared wasn’t part of the userflow I used to sign up so the useful info about coding wasn’t part of my decision-process. I think there is a course FAQ that should also set expectations. Anyway, this is for the course marketers. I will send them a note. Thank you for everyone for providing advice and help to me.