GE8151 Python Programming - Question Bank and Example Programs
GE8151 Python Programming - Question Bank and Example Programs
GE8151 Python Programming - Question Bank and Example Programs
( www.edunotes.in )
num = 50
# Driver code
evenOdd(2)
evenOdd(3)
Output:
even
odd
def happyBirthdayEmily():
print("Happy Birthday to you!")
What are the two parts
print("Happy Birthday to you!")
10 CODE of function definition.
print("Happy Birthday, dear Emily.")
Give its syntax.
print("Happy Birthday to you!")
happyBirthdayEmily()
happyBirthdayEmily()
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person +
Explain a Python
".")
11 CODE function with arguments
print("Happy Birthday to you!")
with an example.
def main():
happyBirthday('Emily')
happyBirthday('Andre')
main()
OUTPUT
Using an interative approach
1+2+3+4+...+99+100=
5050
Python *args
Python has *args which allow us to pass
the variable number of non keyword
arguments to function.
Give the syntax for
In the function, we should use an asterisk *
13 CODE variable length
before the parameter name to pass variable
arguments.
length arguments.The arguments are passed
as a tuple and these passed arguments make
tuple inside the function with same name as
the parameter excluding asterisk *.
Example : Using *args to pass the variable
length arguments to the function
1. def adder(*num):
2. sum = 0
3.
4. for n in num:
5. sum = sum + n
6.
7. print("Sum:",sum)
8.
9. adder(3,5)
10. adder(4,5,6,7)
11. adder(1,2,3,5,6)
List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
# Add List2 to List1
List1.extend(List2)
print(List1)
# importing reduce()
from functools import reduce
def Average(lst):
return reduce(lambda a, b: a + b, lst) /
i) Write a function which len(lst)
15 CODE returns the average of
given list of numbers # Driver Code
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst)
OUTPUT
First element: 2
Second element: 4
Last element: 8
while l <= r:
mid = l + (r - l)/2;
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index %d" %
result
else:
print "Element is not present in array"
while test_expression:
Body of while
if (year % 4) == 0:
Create a Python program if (year % 100) == 0:
25 CODE to find the given year is if (year % 400) == 0:
leap year or not. print("{0} is a leap year". format(year))
if (year % 4) is not 0:
print("{0} is NOT a leap year".
format(year))
OUTPUT
Enter a year: 2019
2019 is NOT a leap year
Enter a year: 2000
2000 is a leap year