This is the "Python" language(not to be confused with the Reptillian variety
In this regard, I have gone through the language basics, Data Types, Variables, basic conditional and other logic and so on.
Yet for modularity, reuse and repeatability, today's languages rely on Functions(and Stored Procedures) or further packages or libraries a lot.
In this respect, Python functions are pretty useful.
Plus the ease of Python's variable declaration, assignment and usage rules means that one can pretty quickly come up with a useful code for many needs.
Let's explore further:
We try our code on Google Colab :
https://colab.research.google.com/drive/1qhgLCsxG8zb5RQ0G9bZnUmyBl3mXRfw7#scrollTo=dsgAEt0hIByn
A)Simple Python Function:
def simple_python_fun() :
print("first and simple function")
#Now let's call the function :
simple_python_fun()
Output : String :
first and simple function
B)Python function to check whether an input number is odd or even.
# Text are the comments.
# Python function to check
# whether input_num1 is even or odd
def evenOddCheck(input_num1):
if (input_num1 % 2 == 0):
print(input_num1 , "is \t Even Number")
else:
print(input_num1 , "is \t Odd Number")
evenOddCheck(10)
evenOddCheck(23)
Output :
10 is Even Number
23 is Odd Number
C)Function to add up numbers and show the sum :
The below function takes up N number of non-keyword arguments :
def sum_of_nums(*input_nums):
print("\n Data Type of arguments :", type(input_nums))
sum = 0
for n in input_nums :
sum = sum + n
return sum
First we pass :
sum_of_nums(1,2,9,12,24,35)
result :
Data Type of arguments : <class 'tuple'>
83
Then :
sum_of_nums(90,100,200)
Data Type of arguments : <class 'tuple'>
390
We can add any random number of digits.
Above, we also see the data types that we pass.
The Functions can be used to accomplish much more - Mathematical functions, Analytics, Averages and more.
The image is of an execution done during running of this program.
For more such thought provoking Articles, insights and discussions, do come and join the wonderful world of Forumcoin. An English language forum, where you can discuss and share idqeas and opinions on a variety of topics.
For sign up, click here :
https://forumcoin.com/index.php?r=5080
-- 21 Apr 2024, 17:03 --






