1
# This program demonstrates a function.
# First, we define a function named message.
def message():
print 'I am Arthur,'
print 'King of the Britons.'
#Call the message function
message()
# This program has two functions. First we
# define the main function.
def main():
print ' I have a message for you. '
message ( )
print 'Goodbye!'
# Next we define the message function.
def message ( ) :
print 'I am Arthur, '
print 'King of the Britons. '
# Call the main function.
main()
Program Output
I have a message for you.
I am Arthur,
King of the Britons.
Goodbye !
Program:
a = int(input())
b = int(input())
c = int(input())
if(a>0):
if(b%2==0 and c%3==0):
print("All conditions are satisfied")
else:
print('')
else:
print('Conditions are not satisfied')
-----------
score = int(input())
if score > 90:
print("Congratulations! You have scored A+")
else:
if score >= 80 and score <= 90:
print("Congratulations! You have scored A-")
else:
print("Sorry. You have not passed this course")
-----------
def total_addition(*numbers):
addition = 0
for number in numbers:
addition += number
return addition
2
total_addition(1,2,3)
output:6
def total_addition(*numbers)::
This line defines a function called total_addition. The *numbers syntax is a special
Python feature that allows the function to accept an arbitrary number of positional
arguments. These arguments are collected into a tuple named numbers inside the
function.
addition = 0:
Inside the function, a variable addition is initialized to 0. This variable will store the
running total of the numbers.
for number in numbers::
This loop iterates through each number in the numbers tuple (which contains all the
arguments passed to the function).
addition += number:
In each iteration of the loop, the current number is added to the addition variable.
This incrementally builds the sum.
return addition:
After the loop has finished processing all numbers, the function returns the final
calculated addition.
total_addition(1,2,3):
This line calls the total_addition function and passes three arguments: 1, 2, and 3.
Inside the function, *numbers will collect these arguments into a tuple numbers = (1,
2, 3).
The loop will then execute:
addition becomes 0 + 1 = 1.
addition becomes 1 + 2 = 3.
addition becomes 3 + 3 = 6.
Finally, the function returns 6
-----------
def fun(*args):
# Function body
pass
Explanation:
*args: This syntax allows the function to accept an arbitrary number of positional
arguments. These arguments are then collected into a tuple named args within the
function's scope. This allows fun to be called with any number of arguments, like
fun(1, 2, 3) (where args would be (1, 2, 3)) and fun(23, 24) (where args would be (23,
24)).
------------
def fun(*int):
result = 1
for x in int:
result *= x
return result
fun(1,2,3,4,5)
Explanation:
def fun(*int)::
3
This defines a function named fun. The *int syntax indicates that the function can
accept a variable number of positional arguments, which will be collected into a tuple
named int within the function's scope.
result = 1:
Initializes a variable result to 1. This variable will store the cumulative product.
for x in int::
This loop iterates through each element x in the int tuple (which contains the
arguments passed to the function).
result *= x:
In each iteration, the current value of result is multiplied by x, and the new product is
assigned back to result.
return result:
After the loop completes, the function returns the final calculated result.
fun(1,2,3,4,5):
This line calls the fun function with the arguments 1, 2, 3, 4, and 5.
Output:
When fun(1,2,3,4,5) is executed, the function calculates:
1 * 2 * 3 * 4 * 5 = 120
Therefore, the function call fun(1,2,3,4,5) will return the value 120.
-------------------
def function(var1=2, var2=4):
var2 = 6
var1 = 5
print(var1, var2)
function(10, 20)
Explanation:
Function Definition:
The code defines a function named function that accepts two parameters, var1 and
var2, with default values of 2 and 4 respectively.
Function Call:
The line function(10, 20) calls the function and passes 10 as the argument for var1
and 20 as the argument for var2. These passed arguments override the default
values defined in the function signature.
Inside the Function:
var2 = 6: Inside the function, the value of var2 is reassigned to 6. This overwrites the
value 20 that was passed as an argument.
var1 = 5: Similarly, the value of var1 is reassigned to 5. This overwrites the value 10
that was passed as an argument.
Printing Values:
The print(var1, var2) statement then prints the current values of var1 and var2
within the function's scope, which are 5 and 6 respectively.
------------
4
def my_function(a, b, **kwargs, *args):
return a+b
my_function(5,6)
--->output :error
-----
def fun(A, B=30):
return A + B
How it works:
1. def fun(A, B=30)::
This line defines the function.
fun is the name of the function.
A is a positional argument, meaning you must provide a value for it when you call
the function.
B=30 is a keyword argument with a default value. This means if you don't provide a
value for B, it will automatically be set to 30.
2. return A + B:
This line specifies what the function will do and what it will send back to the place
where it was called.
It calculates the sum of the values of A and B.
The return statement sends this calculated sum back as the function's result
Object Oriented Programming:
class My_data_type: # first letter capitalized
def init_some_vals(self,val2): # self is first argument of function
self.first_var = 1.7 # attributes named self.attribute_name
self.second_var = val2 # this function doesn't return anything...it
just sets some attribute values
def multiply_vals(self): # another internal method, but this one does
return a value
return self.first_var*self.second_var
me = My_data_type() # declare an object of the class My_data_type
me.init_some_vals(2.2) # don't give self as an input!!!
print(me.first_var) # access attribute
print(me.multiply_vals()) # run a method
5
Program: using class
import random
# The Coin class simulates a coin that can
# be flipped.
class Coin:
# The _ _init_ _ method initializes the
# sideup data attribute with 'Heads'.
def _ _init_ _(self):
self.sideup = 'Heads'
# The toss method generates a random number
# in the range of 0 through 1. If the number
# is 0, then sideup is set to 'Heads'.
# Otherwise, sideup is set to 'Tails'.
def toss(self):
if random.randint(0, 1) == 0: