This is the one video you want to watch. This topic is particularly very confusing on Cisco Networking Academy. Watching this before heading over there is definitely a good idea. You can tell that this guy is a formidable teacher because he is able to cover all the essentials that you need to start writing and reading code in one short video. Thank you very much mate.
Thank you. I was searching for the code behind exceptions while using SOLO LEARN. Thank you for explaining that they are objects and how to construct and tailor specific instances!
It would be helpful to have a talk describing multiple exceptions within a block. Some programmers would say that each block should just do one very specific thing and would only require one try...except. However, sometimes you are doing a few very related things such as working with files and each of those steps might throw an exception. You don't necessarily want to chop up that piece of code into a bunch of separate functions. Even in your Factorial example. You could have multiple checks: 1) Is the type correct? 2) Is the passed in number an integer? 3) Is the passed in number >= 0 How are those tests integrated together? I've seen things like this which seem over the top: try code except exception else: try code except exception else: try etc Is there a better way?
7 ปีที่แล้ว +1
The details of course depend on the situation and the programmer's sense of aesthetics. But as a general thought: If you have multiple checks, you can generally do them sequentially, one after another; so a deeply nested structure (which is awful) like your example is seldom necessary. And when it comes to input validation, type hints might also come in handy (th-cam.com/video/rytP_vIjzeE/w-d-xo.html). But at the end of the day: If you need to check a lot of things, you'll inevitably end up with a lot of boilerplate code!
have functions that each test 1 condition, with specific try/except + raise the_error("message") and the main code has a generic try/except Exception as e, with the many functions in the try block. try: test_is_num(a_value) test_is_int(a_value) test_is_pos(a_value) # here we can do the work or in the else block except Exception as e: print("Error: bad input", a_value) print(e) # optional else: print("good input, we can work with this") finally: pass
What is the benefit of using try: else:? I just put everything in the try block. I would appreciate if someone could explain this to me. Thank you
4 ปีที่แล้ว +2
If you include too much code in the try block, it becomes difficult to know where Exceptions come from. So it's good practice to include as little code as possible in the try block.
"i have a data frame with columns ID and date, If date is invalid how my python program can tell me which ID indexed with invalid date"
5 ปีที่แล้ว
You can use the `isnull()` method. So say that your DataFrame is called `df` and your column is called `date`, then you can get the (indexes of) invalid dates with `df.date.isnull()`.
This is the one video you want to watch. This topic is particularly very confusing on Cisco Networking Academy. Watching this before heading over there is definitely a good idea. You can tell that this guy is a formidable teacher because he is able to cover all the essentials that you need to start writing and reading code in one short video. Thank you very much mate.
one of the most useful videos i have ever seen in TH-cam.
your are my friend a dame good teacher.
I really love the rigorous way in which you explain things. Thanks
I tried to learn this from a few sources before I finally understood from watching this. Thank you!
As someone learning Python, your videos are fantastic :) Keep them coming!
+Julian Nash thank you!
You know your stuff and you explain it very well. You're a great teacher. Thank you!
Great content, rare content
Amazingly useful video!
Thanks for making these awesome videos, you are a great teacher! :)
Glad you like them!
waooh! This is concise, succinct and informative
Thank you. I was searching for the code behind exceptions while using SOLO LEARN. Thank you for explaining that they are objects and how to construct and tailor specific instances!
It would be helpful to have a talk describing multiple exceptions within a block. Some programmers would say that each block should just do one very specific thing and would only require one try...except. However, sometimes you are doing a few very related things such as working with files and each of those steps might throw an exception. You don't necessarily want to chop up that piece of code into a bunch of separate functions.
Even in your Factorial example. You could have multiple checks:
1) Is the type correct?
2) Is the passed in number an integer?
3) Is the passed in number >= 0
How are those tests integrated together? I've seen things like this which seem over the top:
try
code
except
exception
else:
try
code
except
exception
else:
try
etc
Is there a better way?
The details of course depend on the situation and the programmer's sense of aesthetics. But as a general thought: If you have multiple checks, you can generally do them sequentially, one after another; so a deeply nested structure (which is awful) like your example is seldom necessary. And when it comes to input validation, type hints might also come in handy (th-cam.com/video/rytP_vIjzeE/w-d-xo.html). But at the end of the day: If you need to check a lot of things, you'll inevitably end up with a lot of boilerplate code!
There's also if else, which gives another layer. Taken from channel SentDex
have functions that each test 1 condition, with specific try/except + raise the_error("message")
and the main code has a generic try/except Exception as e, with the many functions in the try block.
try:
test_is_num(a_value)
test_is_int(a_value)
test_is_pos(a_value)
# here we can do the work or in the else block
except Exception as e:
print("Error: bad input", a_value)
print(e) # optional
else:
print("good input, we can work with this")
finally:
pass
very clear explanation,thanks
You can catch SyntaxErrors when importing or running code with eval/exec.
+cmdLP that's a good point, yes.
keep going dude, way to hawaii
Thank you!
Nice video many thanks
Yeah this one was very useful... It seems it was made specifically to address my issue...Thank you very much!
How do you use this method in a df
def riddle():
try:
return 1
finally:
return 2
Very nice series, thank you!
fyi, saying there is no logic behind 0! = 1, just def, hurts my mathematician's hearth ;(
What is the benefit of using try: else:?
I just put everything in the try block.
I would appreciate if someone could explain this to me.
Thank you
If you include too much code in the try block, it becomes difficult to know where Exceptions come from. So it's good practice to include as little code as possible in the try block.
@ thank you for the answer and thank you for great videos
What is the document/environment you are using to display this?
It's a Jupyter Notebook, which is basically an IPython terminal in a webbrowser. Very useful!
thnx .... looks very cool :)
very interesting! thanks
"i have a data frame with columns ID and date, If date is invalid how my python program can tell me which ID indexed with invalid date"
You can use the `isnull()` method. So say that your DataFrame is called `df` and your column is called `date`, then you can get the (indexes of) invalid dates with `df.date.isnull()`.
more video please
WTF I thought your cam was a random picture of a dude on a beach having a vacation until I pressed the play button.
I owe you a coffee.
AWESOMEEEEE
Are you Dutch?
Ja dat klopt!
esle ???? why ??? try esle before except block (inside "try except")... lol