Taking Input From User

HINA SAYYAD
4 min readJun 27, 2021

Return keyword

1.Main function

2.Taking user inputs

3. Return keyword

In the other programming languages like Java, C we have seen that A function can take numerous parameters but return returns only single value. But in python not only does a function accept multiple values as input but also returns multiple values.

By looking at the output format we can see that the values are stored in tuple. Which means, when a function returns multiple values it is stored inside tuple. So now we know how to print.Let us see how to store those values

Now let us see how to store in multiple variables

In the above output, the tuple is now unpacked and 10 is given to res1, 20 to res2 and 30 to res3.

Note: Number of variables should be equal to number of values returned by the tuple.

Main function — main( )

Let us create mymodule.py which consists of functions power_of and get_remainder and then import the module in test.py

We can see in the above output, call to the functions from mymodule got automatically executed which is not what we wanted.

Whenever we import a module, we just want to import the functions in the module and don’t want the function calls to get executed. So let us see how to do that 

.>Every python file has a variable name called as dunder name. The content inside this variable is based on whether the python file is executed in script mode or imported in other file.

.> If it is executed in script mode, dunder name contains __main__ 

.> If it is imported in other file, dunder name will be same as the file name. 

Now we know that, the function call commands must execute only when the python file is executed in script mode. So let us make the required changes in mymodule

Now if we execute test.py we expect function calls to not get executed unless explicitly called for. Let us see if we succeed this time

Certainly we got as expected. Now let us call functions in mymodule explicitly in test.py and see the output

With this information let us create a main()

Now just like Java, C, C++ here in python also we have main() which is where the execution will start.

Taking user inputs

All clients expect the input to be given by user. So it is very important that we know how to take inputs from user. 

During program execution — input()

User input During program execution Before program execution input( ) function Command line

Output:

Note: Irrespective of what we enter as input, it is considered as string. As division cannot be performed on strings let us type cast it to integer.

Note: Irrespective of what we enter as input, it is considered as string. As division cannot be performed on strings let us type cast it to integer.

We can further more reduce the size of code and make it efficient as following

Before going to the next way of accepting inputs let us see another example

eval() is a function which evaluates an expression.

Thank You For Reading :)

--

--

HINA SAYYAD

A vibrant mind with great personality.Always opine enigmatic thought about everything. Like to write about technology and lifestyle.