Python + MySQL Day-2 Session | FREE Hands-On Coding Guide

A beginner Python class covers data types: strings, numbers, lists, tuples, and dictionaries. The instructor demonstrates literal syntax and constructor functions like list() and dict(), shows how to check types with type(), and introduces the random library. Students ask about storing random numbers and when to use dictionaries for tabular data.
Chapters
Hello everyone, how are you all? Yeah, am I audible to you?
Yes.
Okay, how are you all?
Good, ma'am.
Hey, that's great. So have you guys set up like Python software on your system?
Yes.
Okay, so which software are you using? You're using Visual Studio Code or you are using PyCharm or Google Colab? Which one? Just let me know. I'm Visual Studio. Okay, and what about others?
Visual Studio.
All right, so let's start. So I'm going to just share my screen. Let me know once it is visible to you. Visual Studio Code visible to you all? Yes, it's visible.
Yes.
Okay, so our next topic is the data types in Python. Before we begin with the data type, can any one of you let me know what is variables? What you have understood from the last session where we discussed about variables and basics of Python, right? So just let me know what is variable. Anyone who wants to try? Shekar, what about you? Yeah, no one? Have you guys understood the topic variables? Variables are to store the values in the memory location will be allocated for them. Correct. How many rules are there to write down a variable in Python specifically? Yeah, are there any rules, or we can just define any variable anyhow like we wanted to? No, not using.
Yes, uh, is recording?
Yes, yeah, I will send it to you on WhatsApp as well. The link I will share. Yeah, hello. You cannot write a variable starting with numbers. Yes, that's correct, Noni. So there are basically few rules that you need to follow, that you cannot write numbers in the beginning of the variables, you cannot use any other special character. You have to be clear, like you cannot, you know, you should avoid using any reserved words from Python. So those are certain rules that you need to follow. For writing variables. Okay, now our today's topic is that built-in data types. Today we will cover data types, that what exactly data types we have and how to get the information about the data types that we have, that we are defining. Okay, so in Python or in any programming languages, data types are divided into two types. First is built-in data type Built-in means which is already available in Python or in that particular language. So first we will cover built-in data type. So data type is important because what we are trying to store, that also matters. So in certain programming languages, you also have to mention— suppose you are storing a number in a variable, so you have to mention that this is a number. If you are trying to store a message, then you have to mention that, okay, this is a string. But in Python, we need not to do that because it has— it has a list of built-in data types within it. So different types can do different things. Like if you wanted to do some calculation, it means you have to follow the numeric data type, then only you can do some calculations. Otherwise, if you are going with other data types, that won't work. So first type is the text type, okay? So first data type is the text type, which is string. Okay, string is the first data type. String means any text or any message, or if you have something like a PDF or a paragraph, everything comes under this text one. So this is the first one. Second is the numeric data type. In numeric data type, there are multiple things. First thing in numeric data type is the integer. Integer means any whole number like 5, 6, 10, 20, 30, 40. Any whole number is integer. Then second type of numbers are decimal numbers. So whenever we call this— whenever we say decimal number, it means it's float. And the third one is the complex number. So there are three numeric data types. So like in text we have one, but in numeric we have integer, then we have second is float, and third one we have the complex. So integer means whole number, float means decimal numbers, and complex means combination of two types of numbers. First is imaginary numbers and then second one is the real number. For example, if I'm saying integer, then first thing to know, to note over here is whenever you are going to write down integer in Python, you have to just write first 3 words, which is int. When you are trying to write string, you have to write str. And if Python in output is giving you str, it means you have to understand, it's understood that str means string. And third is the, second is like, sorry, float, right? So float is decimal numbers. So anything like 2.5, 6.9. All these are the decimal numbers, and we, we call them float values in programming. Next is complex values. Complex means which consist of two types of values, combination of imaginary number plus real number. So imaginary number means, let's say if I am writing 6— sorry, 6j plus 75. It means we don't know the value of j, therefore we are calling it an imaginary number. And 75 is the number which we know, that 75 value is 75. So 75 is the real number. Where we don't know the j value, this is imaginary number. Generally we don't use these kind of complex numbers, okay? So majorly you will use these 3 data types. Any question up till this?
Others? No, ma'am.
Okay, so these were very basic data types. Then the next type of data type which we have is where suppose Right now when I say whole number, when I say decimal numbers, it means I'm talking about the single value. But there are certain data types that allows you to store multiple values at once. So those we call as a sequence type. Third is sequence type. So sequence type means suppose I wanted to create a wish list or I wanted to create a shopping list. So those list So the list is a sequence data type, sequence built-in type in Python. Second is tuple. Tuple is similar to list, but in case of list, whenever you are trying to store something, it means you can change it. Suppose if I am creating a wish list, but let's say I have added 2 items in wish list that I wanted to get a MacBook or I wanted to get something, anything it could be. So if you are trying to store something within list, you can change it. Later on. So right now, if I, I don't want to buy a MacBook, I wanted to buy shoes, so I can replace MacBook with shoes. So whatever you are trying to store within list, you can change it. But if you have so stored those value in case of tuples, like we have used tuples, then you cannot make the changes in it. So list and tuples are similar. The difference is that you cannot make the changes in tuple and list you can. Then comes range. These are certain— like, list and tuples can store the values, like multiple numbers. If you have, you can store it in the list. If you have a string thing, like as I said, MacBook, which is string, so that also you can store in list. But what if I say that you wanted to go with the numbers only? Then we have range. Range is also sequence data type. Range means suppose I wanted to in one go, I wanted to store 5 numbers starting from 1, 2, 3, 4, 5. So I can use this range sequence type that will help me store 5 values. So when I'm writing range bracket open 5 and then I can close it, this is range 5. It means now I have stored 5 values using the range sequence type. Any question?
No, ma'am.
You have to just understood that, okay, this is the list of data types. Obviously I'm going to show you how you have to use it, where you have to use it, everything I will tell you. But right now you just unders— understand what is the meaning of all these terms that I am writing on my screen. Let me just convert it to markdown. Okay, so these are 3 data types. Then comes mapping type. Mapping means where we can assign our values like Mapping type is dictionary. There is single data type which is dictionary. So dictionary is a kind of, uh, storage where you are going to allot a name. Just like we have variables, same way in dictionary, whatever values you are trying to store— dictionary can store multiple values at once, but you have to assign a name. Assign a name means, let's say, I am trying to store the name of students. Okay, so when I'm trying to store name of student, then it means I am trying to go with the— I can say that first student, second student, third student, and so on. I can continue. So first student, second student, third student is the name that I am providing. Or maybe I can write down the roll numbers of the students. I can say roll number 1, 2, 3. These are the numbers that I am going to allot to each one of the student, and I can store it in the dictionary. Whenever, whenever I wanted to get the name of the student whose roll number is 5th. So I can directly say 5th, and it will— in the, in the output, it's going to return the name of that particular student. So dictionary helps us to store the values in the key-value pair. Key-value means key is the numbers or name that you are going to allot to that value, and value is itself a value which you are going to call using keys. Okay.
Um, what is the difference between a variable and dictionary?
Variables and dictionary. Okay, variables means you are trying to store directly something. Let me give you an example of each one of these, then you will get it better. Okay, so let's start with first one. When I'm trying to write down a variable, let's say if I have written a is equal to apple Okay, now a is my variable where we have apple as a single value. Now if I wanted to store multiple values at once, maybe I wanted to create a fruit list, so I can simply say fruit is the list— fruit is the variable, and I can create a list. So I can say apple, grapes. So now I have stored 3 things, and I am using these square brackets. Whenever I am using square bracket, it means I have created a list. Okay, so this is list. I have tried to store list in hash, and this is value, which is— you can also say string. Okay, then next one is numbers. So if I'm saying num is equal to 45, it means I have stored an integer value. Integer short form is int. Whenever you're going to check what is the value, what is the data type that you have stored in the variable num, then it will return int to you. And let's say I'm writing grades is equal to 95.2, so this is float. I'm saying dimension is equal to 9j 45, it means this is complex value.
If you are—
if you wanted to store something in the tuple format, Then you can write it like, let's say I'm writing wishlist is equal to, open a bracket, and then you can mention— now this time I'm using round bracket. Whenever I'm using round bracket, it means this is tuple. When I'm using square bracket, it means that is list. So these are the syntax that is allotted to the list and the tuples. So here you can mention whatever you wanted to mention. So this is tuple. Next, what we have discussed is range function. Now when it comes to range function, let's say I wanted to— let's say variable name is value. Value is equal to range 5. So this will automatically store first 5 numbers in the variable. Now let's see the next one, which is dictionary. So let's say I've created dict is equal to— for dictionary you have to use curly brackets, and within curly brackets you have to use these colons. So towards the left-hand side you have to mention the value, and towards the right-hand side you have to mention— sorry, this way you have to mention the name of that particular value, and in the right-hand side you have to mention the value. So for example, if I'm saying student1 is A comma student2 is B. So here I have stored the name A and B with the name— with the key that is students. So this is called key-value pair. This is key and this is value. And then I have written second key-value pair, which is this one key and this is the value. Any question in this? No, ma'am.
Um, I have the question in the range, uh, like, uh, if we have to store any random number— means you have stored, uh, 1 to 5, right?
Yes.
Uh, so if we want to store any random number, so how will we do it?
If you wanted to store random numbers, then in Python we have libraries So we have one library called random. So you simply have to import that library, random library, in your Python notebook, and then you can use the methods from that library. Okay, and that method will help you get the random number. Like, obviously, suppose if I ask you that, okay, you have to choose one random number from 1 to 10, so you can choose any number like 7, 6, 5, any number, right? But I have given you a range that you have to select a number from 1 to 10 only. Right, so same way in random library also you can set a range that, okay, starting from 1 to 5 you have to select one number, any random number. So then random library can help you do that. Sorry, did you get it?
Yes, ma'am.
Yeah, now these are the exact— these are the syntax that you need to follow if you wanted to store the values.
Correct.
Now let's say I wanted to check— I have stored the value, but later on let's say I have created so many code cells and I don't know what I have stored in the value in the variable grade. I have stored 852, but I wanted to check which data type it is. So you wanted to check the data type. To check the data type, we have the method— you have to write type and then in the bracket you have to mention the variable name. So type bracket open, you will mention the variable name. So if I'm writing a, type a, then it will print the data type of variable a. Okay, this is going to print the data type of variable a. Okay, you can also— you have to also, you know, follow the same, like you have to write it within the print statement because I am using Visual Studio Code. Maybe any one of you is not using Visual Studio Code, so you won't get output like this. So I can run this. So now you can say it will show you class string. So class is okay because there are classes defined within Python, therefore you are getting the word class in the output. But it is string. As I told you that the short form of the string is str, so whenever you are printing the string you will get this str, okay? So this is str. If you wanted to check it with any other variable also, you have to follow the same thing. Suppose I'm going to check it for the wishlist. Now if I will run, it says tuple. And you can see over here I have used a round bracket, which is tuple. So type function can help you get the data type of any variable. So simply write type bracket open and you mention the value, uh, the variable. And you can see when I have written value, it's saying range because I have tried to store a range in it. If you are going to directly print the value, the variable value, then it will show you range 0 to 5. Anyone, any question on this? Hey, no, ma'am. Can you mute yourself? So these are certain built-in data types that will help you write down your code effectively if you know them in depth. Now let's say I have defined something as an integer, but now I wanted to convert it to the float, or I wanted to convert it in certain things, like however you want it to. So you can change the data type as well. You can change from one data type to another data type. When you wanted to change it, you have to use the correct spelling of that. Just like as I told you that string is in short, in like in Python you will find it with the str only. Integer you will find it with int only. Okay, so dictionary you will find it with dict. Okay, so you have to follow these words and then only you can create it. For example, let's say I wanted to change— this is integer, right? So let's first check this type bracket num. Okay, it shows int. Now I wanted to convert it into float. If we are going to convert 45 into float, it means it's going to give us 45.0, correct? Yes or no?
Yes, yes, ma'am.
It's going— if we are going to convert 45 into float, then it's going to give us 45.0. So now to convert it, you simply have to follow this method. Like, you have to first mention the type that you wanted to convert it in. I wanted to convert it into float. Now what do you want to convert it? I want to convert the num variable into float, correct? Now this was 45, right? Now you wanted to convert 45 into float. It means it should give you the output for 45.0. So I'm going to store it in the variable. Let's say num is equal to float of num, correct? It's giving 45.0, correct? Now what I have done is I already have stored a value 45 in the variable num, but later I changed the data type from integer to float, and then I reassigned that value into the same variable. Correct. It means now this time I have updated my variable. I haven't created a new variable, but I have updated my current variable. And now if I'm going to run this particular code cell again, which says that type of num, then this is going to give me float. You can see this is the previous output because I haven't used the print statement. If I'm going to use the print statement, it will just give us the single value, which is float. Correct. Any question? Yeah, anyone, any question? No, no questions, ma'am. Is this clear to you that how you have to exactly convert it from one data type to another data type?
Yes.
Now if I ask you that you have to convert list to tuple Then how will you do that?
We will use keyword tuple.
Okay, so this concept is called typecasting. Okay. This concept is called typecasting. So typecasting means converting from one data type to another data type. And what we are trying to use over here is we are trying to use a constructor, okay? We are constructing from one form to another form, okay? So, Keshav, you have written tuple and then you have written list within the square bracket. No, not, not like this you are going to do it. So simply, when you are trying to convert list to tuple or tuple to list, you have to use round brackets only. Yes, so if I have the existing— let's say I wanted to convert this fruit to tuple. So fruit is the variable. Now I have to convert it. So simply I can say fruit is equal to list bracket— sorry, I have to convert it into tuple. So I will write tuple bracket, and then you have to mention the variable. And then I have to check it whether I am getting it in the correct form or not. So I'm going to write it within type function, opening bracket, closing it. So now you can see I have converted it into tuple. Now, for now, you already have it defined, right? You already have defined the tuples, you have defined the list, and then you are converting it from one form to another form. But if you haven't defined and you, you wanted to define it using these constructors that we are using, right? This is called constructor. Whenever I've written tuples with the, this brackets, then it means this is tuple, and this is constructor, sorry. So constructors are tuple, this is constructor, list, this is constructor, float, this is constructor, which is used to construct certain thing, okay, in simple. So when you wanted to construct it using these constructor, so you have to use the same method. So let's say I'm going to create a new list, let's say x is equal to, I'm creating using constructor, so I will open a bracket. This is a constructor, but now I have to create a list, so I have to use one more bracket within it and And then I have to mention the values that I wanted to add. So let's say I'm just adding some random values, comma, c, and then I will run it. And now I'm going to check the data type of x. So now you can see this is list. Now in this case, I'm not using the square bracket, so you don't have to get confused that, okay, now we are don't using square bracket because we are using constructor in Python. When we, like, in the Python structure coding, if you will, overview, then you will find that list is a constructor which itself will convert these square— round brackets into the square brackets. Same thing you can do if you wanted to construct a tuple as well. Same thing, same formula, same structure you can use. Suppose I'm going to just paste it. I'm going to change x to y, and let's say list to tuple. Now we'll run this. Now let's check it for the variable y. And you can see now we have tuple. If you wanted to create a dictionary using constructor, then same thing you can do. Like, you have to use the round brackets only. You need not to focus on brackets like square bracket, round bracket. Those who find it difficult to remember every time, okay, for list you have to use square bracket, for tuples you have to use round brackets, for dictionary curly brackets, then you simply can follow these constructor method that you write down the constructor, within a bracket you mention the values. Now I will show you how to construct a dictionary. To construct a dictionary, method is same. Suppose I'm writing z is equal to dict bracket open. So let's say name is equal to comma, age is equal to what I have written. Is not callable.
Column is not column.
Which one? For this one you're saying?
Yeah, no, colon will be there.
Column, column. This one you're saying? No, no, right now we are using constructor basically. So when we use constructor, and even without constructor also, instead of those colon, right, you can use equal to also. Okay, so both are valid. I think I have defined somewhere dict above. Yeah, look. I have to restart the kernel and then it will work. I will try with the brackets. No, dictionary double brackets are not required. Yeah, I need to restart the kernel basically because I already have defined the dict above, right? So as I told you that you have to avoid using the reserved word. So for dictionary, dict is a reserved word in Python.
Okay, can't we change here any other name?
Which one? No, dict will be dict. Now this is the constructor only. We can't change the constructor. We have to restart the kernel and then only it will be refreshed basically. Then only this particular variable now which I have just Yeah, revised, revised, right? So that particular variable will be refreshed and then it will work. Okay, anyone else, any question in this?
Uh, ma'am, we can use this, uh, statement in another, uh, file. Like there is one more day1.py, we can use it there. Will it work over there?
So you change— it is saying that you have to change the file.
No, no, yeah, if you can change the file rather than going for a restart of a kernel, if we use this day1.py file over here which is there, will it work there?
Yeah, we can try that. Yeah, so let me just switch it.
Just take—
okay, it's not giving error, so let me just print Z. Yeah, you can see now we have successfully created a dictionary. Okay, so z is equal to dictionary.
So now here you have described dict as a keyword, so in this sheet again we can't use dict keyword, right?
No, basically if you are using dict as a constructor which is defined already in Python— so dict is basically a reserved word in Python. What I did is When I was working on these variables, right, to explain you that how to write down a dictionary, how to define a dictionary, I use that dict variable over here, right, which was wrong, that I should avoid that dict word, correct? But now I am using this dict to use— I'm using it as a constructor. When I'm using it as a constructor, it means I have to— I'm trying to create something, right? Now if I'm trying to reuse, reuse it, let's say y is equal to dict name 2 is equal to comma age is equal to. So now I have reused it, right? So it won't give any error because again I'm using it as a constructor, I'm not using it as a variable. Got it.
Okay, ma'am.
So it, it depends how you are using. If you are using it as a constructor, it won't give you any error, okay? If you are changing the way, like you are making it for the different use case, then it will throw an error, just like I did it for dictionary over a dict over here. And then if I will refresh everything, then it will work. Okay, anyone else? Any other question in this?
For dictionary, there is not required double brackets.
Yeah, for dictionary, double brackets are not required because we have to write it in the key-value pair, right? So directly those key-value pairs will be inserted in the dictionary. Now let me tell you why we are using double brackets. Okay, so in case of list, we have to convert one bracket to square bracket, correct? For list, these brackets will be converted to square bracket. For tuple, these bracket will remain there itself, okay? Apart from constructor. But in case of dictionary, those values will be directly inserted in the dictionary's curly bracket. Whenever we call dict, it comes with the curly bracket, okay? And then these value will be inserted in that. In dictionary you can write multiple values, just like I have written name is equal to this, right over here, right? I can create a list over here directly. Age also I can write it like if I wanted to write it in tuple format, I can use the tuple format as well. Like this. And now if I will execute it, it will give me the entire name. This is a list. For age, this is the list. So whenever we go for rows and column structure, right, dictionary is the row and column structure. So right now you are going to— you are starting with the programming, right? So later on, if you are going to use the Python, like, use the same built-in concept in data analysis. When it will come to data analysis, then you will majorly— you have to focus on dictionary because we have to deal with the data which is in the rows and columns format, or maybe it is relational data or other type of data. But that time we are going to deal with the rows and columns, so dictionary is the correct format for rows and columns. So therefore, whenever you're going to create a DataFrame, then you have to create it using dictionary format only. Okay, so dictionary is the main concept that you should focus on for data analysis and data science. Okay, anyone else? Any other question? Yeah, anyone, any question?
No, ma'am.
Okay, so for today what you have to do is you have to practice the data types, you have to practice variables, you have to try to merge multiple data types like using— like I have created age, right? 21, 20. Try to insert float values in this and then try to execute it and look forward like which type of error you are going to get if you are going to make any mistake while writing it.
Okay, okay.
Yeah, if you have any questions or anything, you can drop your questions in the Discord server that we have so that we will have a discussion over there and we'll resolve the error that you are getting. Okay, else you can tell me the errors in the next session as well.
Okay, I have a doubt. Yes, uh, what is the difference between, uh, the constructor and the bracket one? Means, uh, we are, uh, putting the values in dictionary like in curly brackets.
Yes.
Next thing is like you used, uh, means constructor for a dictionary. So my question is, what is the difference between two, both of them?
So basically when you use the constructor, right, it becomes flexible that you can change it, right? You can basically switch easily, okay? Obviously with the curly bracket, with the curly bracket like dictionary literal which we have used You can do it, but when you are using the curly bracket, it is most common, you can say, and it is concise way to create it, correct? But when we are going with the dictionary constructor, then that is basically a function that you are using, okay? And you will— in like, if you are going to make— you wanted to, you know, it's like saying that in terms of flexibility, right? You will find it more flexible if you are going with the constructor, because in terms of constructor, you need not to remember everything, like you have to use curly bracket this time, then you have to use colons and all. You can, you know, ignore those stuff if you are going with the constructor. It gives you basically more flexibility. Okay, so just like I told you that you are using this equal to, right? When you are using the curly bracket, you, you have to follow these key-value pairs within the colon.
Okay, so basically it makes the code easier, right? Yeah, yeah, thank you.
Yeah, anyone else? Any other question? No. All right, then we'll connect in the next session. Till then, you just practice these data types and try to switch them from one type to another type.
Okay, okay.
All right guys, bye-bye.
Bye, thank you.
Take care.










