Learn Python Basics Together - Day 4 | Python Dictionaries Explained for Beginners

Learn Python Basics Together - Day 4 | Python Dictionaries Explained for Beginners by @techcoderlabz on 3Speak

▶️ Watch on 3Speak --- Learn Python Basics Together - Day 4 🚀 Welcome back to our Python learning series! In today's video, we learn about one of the most useful data structures in Python: ✅ Dicti...

Tags: #hivesuite

Transcript

Hello everyone, welcome back to my YouTube channel. So today we're going to talk about try and handle the exception and then some built -in functions. I hope that you are aware of what we have covered so far, and if you aren't aware, I request you to check my previous video and go through the playlist of Python. Alright, let's get started with try and accept. So try and accept is kind of a syntax with which you can handle the piece of code which can throw errors. So let's start with one example over here. So let's assume that in number we have value 10, and for value variable, we have, let's say, value 0 over here. And if I'm checking if number, divide by value, is greater than 1 or not, and of course, this line number 4 has an error over here. Anything that you try to divide by 0 would throw an error. So let's say if I'm printing over here, greater than 1. And over here in the else, I'm going to put, let's say, print, not greater than one. So obviously over here at line number four, we have logical error. In terms of code, there is no error, but there is logical error, and which you can encounter sometimes, you know, when you're running the code, running your code in the production. So, of course, right now it's complaining. over here because we haven't completed the syntax over here which is also supplying the accept statement and in that print over here I'm gonna say something went wrong okay so now if I try running this it would say something went wrong okay why because the code over here has a logical error if I change this and try running this over here it would work just fine. So in order to handle logical errors, you should always use try and accept code block or syntax basically. I hope that, with that, we're all on the same page with try and accept. Now let's talk about some building functions. So one of the built-in function that we use all the time is print function. This is built-in function. For that, you don't need to write anything at all. Import statement or anything like that. It works just fine. Now another built-in function that I want to talk about is the ABS. So with the ABS, you get the absolute value of a given number. So let's say if I am supplying minus 30, it gives me 30 value. But of course, we'll use sprint function as well in order to see the value. And now I'm going to run this over here, run this file in terminal, and with that you can see that 30 is printed. So this is another in-built function. And one more in-built function, about which we have already talked. which is ops and in the, no, not in the ops, I guess string operations. Over here, length, length is nothing but a list or length of a string. So if I say print and then length over here and say hello word, so it's going to give me the length of the string over here. So this length function is inbuilt function, so if I try running this, I get the length of the given string. Okay, another built-in function which I want to discuss today is the bull function, which gives you the Boolean value of the value that you have supplied in that. So for 0, it's going to always return false. But if you supply 1 or 100 or anything, basically, in that matter. So over here, let's say I'm supplying hello as well. So it's going to print true, true in all these cases. And over here, this is something new. If I supply in none, none is nothing but no value, representation of no value, or you can call it none, so it's Python's built-in value. If I run this, over here you can see, for the 0, it printed false, for everything else it printed to accept the none, for none also is going to print false. So, yes, over here we discussed about the bull function, which gives you the bullion value of a supplied value. let's talk about the directory. So let's say I have a string over here. So let's say I'm writing hello over here. So for hello, we have so many things. This is a string. The moment I hit the dot button, I can capitalize it, I can case fold, center, count, so many functions, so many functions which are available over here. So, DAR is directory, basically. So if I say print, and then directory of let's say hello over here, then it is going to give me the list of functions and variables and objects which are available on that given object over here. So let's say if I run this over here, I get everything over here. So similar to that, if I let's say simply type 1 and then, no, not 1, let's say I have a list over here, list and then 1. 2, 3, 4, whatever. And then if I say list dot, then I see append clear copy. So if I want to see all of that, I simply have to say directory for the list. So with that, I get full functions and variables which are available on that. Of course, you don't have to print it. It's all visible over here in front of you. So I would recommend not to use it. It's nothing but a help command in case you don't have IDE, like vS4, you can use directory command. And just like that, let's say if you want help on a given function, so let's say print help for list. Let's say append or pop, let's say pop function, you want to understand, how does that work. So for that I'm going to say help and then over here I'm supplying list.pop. I'm not executing it, make sure that you know, you simply supply the name of the function for which you want to understand. Now if I run this, over here, you can see that I'm getting the help for that. function pop function hitting the Q button over here or Q from the keyboard to exit for now commenting it out so that I can take a look at more outputs over here so we covered print then absolute value then length then bullion and we also learned about couple of help functions now let's talk about the function which can execute the code which is there inside a string. So let's say I'm writing a function called my code and in that I'm adding a statement. Let's say print, hello world. So over here you can see that in this string we have a code. Let's say if you want to run the code which is inside a string variable, put that you can simply write UL and with that you would be able to run the code which is inside a string. So now if I run this you can see meaning, there's a, yes, syntax error over here, I forgot to close the string. Now if I try running this, we'll get Hello World over here, and just like that, EXC, execute a string. With that, I'd be able to execute the string, or the code which is inside the string. So you can see that now we have printed Hello World twice, one with Eval and the another one with the XCC. Okay, now that we have covered the set of functions let's talk about some other inbuilt functions which are important for typecasting okay so let's say I have this 10 and if I do 20 it's not gonna work so over here I'm gonna typecast it to integer now I'll get the total of it so if I now print over here 10 plus 20 I get 30 over here in the console right click run this in the Python, you can see 30. So with int function, basically, we are doing a typecasting over here, string to integer, just like that, if I say hello plus string, STR, basically, and then 30 over here. So with that, you can see we are joining two strings over here. Oh, yeah, my bag. I was just trying to typecast string from string, which was incorrect. Over here, we are trying to typecast 30 to string, and then joining it over here. so now if I run this over here you can see Hello 30. So just like that, I can also typecast float value as well. So I'm going to say float from the string. Typecast to float from a string. So let's say if I have 1,234 .34, let's try adding this to float value which are derived from, let's say, a string. So I'm going to put a float over here, and in that, 22 of course float value coming from a string so 22.23 over here so what is going to happen over here actually we will be converting the string to float then we'll be converting this string to float and then both of this value would be added and then it would be joined to the print statement over here hello so if I try running this I hope there is no compilation matter and you can see that it worked just fine and we got the total for this two values over here So with this, we covered some basic building functions, and we also learned how to handle the errors inside the code, logical errors mostly, with the help of try and accept. So this is it for this video. I hope that you enjoyed the video, and if you like the work we do, hit the upload button and subscribe us. That's it for this video, see you in the next one. Bye-bye!

More videos on 3Speak