Python Tutorial for Beginners 4 {For absolute beginners]- Python print() and input() Function

 

hey guys welcome to the next blog on Python to tutorial for beginners in this blog we will see how to use print function in order to print output on the console in various different ways and I'm also going to show you how to take user input using Python so we have already seen that in Python we can use this print function in order to print something and we have already a printed hello world at the time of installation right and when I press ENTER it's going to print hello world on the console now some of you might wonder what is this print so print is a inbuilt function in Python so python has a number of inbuilt function and print is one of them so here print is a function and then inside the parentheses what you give here is called a parameter or an argument ok so hello world is an argument which is a string argument right so let's try to give in this print an integer argument let's say I just provide 25 here and it will print this number once again let's say I'm going to write print and then I want to execute some expression 50 x 60 and then press Enter print function is also able to do this now let's say I want to output some more complex formatted string so let's say I want to print on the console 50 x 10 is equal to 60 how can I do this so let's see how we can do it so I can write print here and then inside these double quotes you can write your string for example 50 x 10 and then in print function you can give multiple arguments using this comma separator so here I can just write 50 x 10 and let me put equals symbol here inside this string and then press Enter and now it prints 50 x 10 equals 500 and you can also provide more than one argument to this print function so for example I can just write hello here and then in the second argument I will just provide the space between hello and world and in this third argument I will just a world here and this print function will concatenate all these string values and give you the output in this format now let's say you want to also provide at this value here which is 50 and 10 instead of string you want to provide some value which you take from a variable so for example you declare a variable X is equal to 50 here and then Y is equal to 10 and you want to use these two variables instead of this static string so how you can achieve this you can achieve this in various different ways so let me show you the first way so I can write print and then in the double quotes this is your string so whenever you use this double quotes and whenever you write something in between this double quotes it becomes a string so after this string I will provide a dot here and wait for some seconds and as soon as you do this you will be able to see various different hints here which are provided so these are all the inbuilt methods which you can use with the string so you can use find method you can use format method so for now we are going to use this format method with strings so you can just click on this format method so let's say I will provide X as the first argument Y as the second argument and I want to see the output of X multiplied by Y in the third argument now how you can print the value of x Y and the multiplication of X Y using this blank string so it turns out that you can use these curly brackets inside the string and you can give some index here so index starts from zero so this index is index zero and then I'm going to provide space and then this Asterix symbol to provide this kind of format so we want to achieve this format here right so just write zero in the curly bracket space Astrix and once again curly bracket and this time we are going to provide the index one and the result will be transferred to index two why because here the index starts from zero so X will be transferred to the index zero Y will be transferred to the index one and the multiplication of x and y will be transferred to index two here so let me press ENTER here and you can see it prints the string which we desire now once again I'm going to write this print function and once again I'm going to give this parenthesis and you can see the hint here so I want to use this SCP keyword now inside the string so how can I use this so I will just close this parenthesis and once again I want to print hello world so I will just write hello and then I'm going to provide the comma and inside the double quotes I'm going to just write world here and then in the last argument you can provide this keyword SCP is equal to and inside the double quotes you can provide some separator to separate the hollow from world so I want to just give this string to separate hello from world and now I'm going to press ENTER and you can see this string is separating the keyword hello from the world now in Python you can also use C style string formatting to create a new formatted string so let me show you how we can do it so I'm going to declare a variable called name and I'm going to assign some value to it for example Maxia and then press enter and now I'm going to just write print function and in the parenthesis I want to just greet this name so I can just write double quotes hello here and then if you are familiar with C style string formatting then you may know this modulo s symbol which is used to print strings right and then after this double quote you just need to provide once again this modulo symbol and then you need to provide the variable name which you want to replace instead of this modulo s so in our case we want to replace this modulo s by name variable and I'm going to press ENTER and you can see it prints hello max now let's say I also have the age of this person so the age is let's say 22 and once again I'm going to use this print function and inside the parentheses I want to say hello to the name including the age so I can just write hello and then modulo s here and then I'm going to print the age of this person I can just write re u modulo D which is used to print the integer values and then I'm going to just write years old and after the double quotes I can just write modular and this time I cannot simply provide this name argument like this I must provide the name in the form of a couple and what is a tuple a tuple is a fixed size list and we are going to see in the future blogs how to use tuples but for now just remember that a double is a fixed size list okay so inside these parentheses once again I can provide first of all the name and then as a second value in the tuple I can provide the age variable and now I'm going to press ENTER and now you can see it prints hello max are you 22 years old so this is how also you can use these C styled string formatting in Python I want to give you one more example of float here so I can just write for example this person's marks so I'm going to just write marks is equal to and let's say I want to provide these Mark's in the floating point so I can give this modulo F here and then after this modulo I can just give the mark so ninety two point five and then press ENTER and you can see it prints the marks now one more little thing which we can do here is we can limit the number of decimal values which we want to see after this point so we can write the same printf function and same argument and in order to limit the number of digits after the decimal you just need to write after F dot and some number for example two oops I don't want to see this output I wanted to see the output after the decimal value so once again I think this should be this point should be before F so I just need to write dot and two before F I think and then press ENTER and now it limits the number of digits after this decimal point now the last thing I want to show here is how you can allow the user to provide some input so for example I will declare a variable called value and then I can use equals and then I'm going to use a inbuilt function which is called input so input is also an inbuilt function which allows you to take user input so here I can just write as the first argument and then I will provide this colon here and that's it you just need to press ENTER here and now you can see this terminal is asking us the same question which we have written here so let's say I just provide 50 here and then press ENTER then what's going to happen is this 50 is going to be assigned to this variable which is value so now when you just use this value variable you can see this 50 is assigned to this value variable you will also see that this 50 is a string and not a number right because it's enclosed in single quotes so how you can assign 50 to this value using the user input so what we can do here is once again I'm going to use this value variable and what we can do here is we can typecast the output of input so you can just write int and then here inside the parentheses you can just write and now when I press Enter and provide 50 and then press Enter and see the value now it's an integer right so you can typecast the output of this input function which provides us the string and this int method is going to convert this string into an integer and then give us the result instead of this int you can also use a float keyword here so let me show you this example also so I'm going to just copy this and paste it here and instead of using the int I'm going to just write float here okay and then press ENTER and I'm going to provide this time hundred here and then press Enter and then I'm going to just print the value of the value variable and now it prints the answer in floating points so this is how you can use print function to print the output on the console and input function to take the input from the user I hope you have enjoyed this blog  

Post a Comment

Previous Post Next Post

Recent in Technology News