Python Tutorial for Beginners 3 - Variables and Inputs


hello guys welcome to the third blog tutorial on Python programming for beginners and in this blog I'm going to show you how to use variables in Python so I'm assuming that you don't have any prior programming knowledge so I'm going to tell you the basics of variables first so basically what is a variable variable is a container for some value this value can be an integer if this value can be a string of words this K value can be a float value or different kind of values so what is a variable variable is a container for the values so for example how you can declare variable in Python first of all you need to give a name of the variable so for example I can declare a name of a variable like my variable is equal to and this is equal to sign you use between the value and the name of the variable okay so this is the name of the valuable is equal to you can assign some value to the variable for example 30 now my variable has the value of 30 so my variable is a container for the value 30 right and generally it's the convention that whenever you use or declare a variable it's not necessary but it's a good programming habit that you separate your variable name if you have something like my variable the first word of your variable name start from small letter and then the second word first letter starts from big or capital case and the other letters are small and this way it will work like for example I want to give the variable name my variable name it will be like this okay so it it's like camel case it's called a camel case declaration so start with small letter and then the second letter second word first letter is the capital third word first letter is the capital just like this okay so just follow the convention like this it's not necessary but it's good now when I press ENTER now my variable has the value of 30 so once again when I call my variable and press Enter it gives me 30 because my variable is equal to 30 now now python is a case sensitive language so if you write my with a capital m variable and press Enter it will give you an error because this variable name is different from this variable name because this is case sensitive so just remember this okay so these are the two things now why we exactly use variable in any programming language so variables are generally used whenever you don't know what values you are going to deal with so for example you are going to deal with some values which are inputted by the users now first of all this variable you can use it as for example for adding something so I can just call my variable + 4 + 44 and it will give me the result yeah this is you see I haven't spelled this my valuable correct so it gives me an error okay so my variable plus 24 and it gives me 64 okay so now you can perform math operation using this variable also okay now the real use of this variable you can you can perform every math operation on this variable because it has some value which is an integer right so you can do variable to the power or exponent 3 give the answer and in the same way it will give you an answer for every math operation now the real news let's see some use for variable so for example I declare a variable called value okay and this value is equal to whatever user inputs from the user so how you can provide a user input in fight you just use the inbuilt function called in put so this is the in volt function in Python which will allow you to enter any value so you can just write and the value but there is a problem with this that this input whatever user enters will be a string okay so let me give you an example when I press enter now it gives me the prompt that enter the value and I can enter any value for example two key and press n and now this value which is a variable value has the value of 50 so whenever I call a value it has the value of 50 but notice this value is a string it's between dump this single goats so whenever I want to perform some addition so value plus something 20 that will give me an error seeing it give me an error why it's giving me an error because this value which is 50 is a string because whenever we use input function for input value it takes this value as a string okay and whenever you see something in single code that means it's a string whenever you have the single quotes around your value it's a string okay now in order to convert your string to an integer you can cast the string so I'm going to just copy this just you can just pop it in this and paste this here and in order to cast this value to an integer what you do just go before your input so value is equal to just write int so this is typecasting of string to the integer okay and just close it from them okay so when you do this this will be your string so input will be taken as a string and this int will convert your string to the integer okay and now when you press ENTER and you give some value for example 60 values equal to 60 and now you call value not value but value it has the value of 60 m notice that earlier this 50 is enclosed in single quotes but this time it's not enclosed in single quotes that means it's number right now I can perform some math operation on this like this value plus 50 x 60 something that will not give me an error this time so always remember whenever you want an integer as an input you need to cast this into the integer and casting means to convert your string to the integer you can cast this to the float value like this for example you can just copy just once again paste it here and you can cast this like float so the float is a decimal point value okay so you can cast your value as a float and now when you enter you can just write 50 point 1 3 4 whatever and then call value and it has the value of 50 point whatever okay so variables are useful whenever you don't know what values you are going to deal with okay so that's all about variables I hope you have enjoyed this blog

Post a Comment

Previous Post Next Post

Recent in Technology News