Python Programspdf
Python Programspdf
def my_function(x):
return 5 * x
OUTPUT:
(7, 7, 11)
OUTPUT:
7 7 11
OUTPUT:
enter the number: 5
The factorial of 5 is 120
OUTPUT:
How many terms you want to display: 8
0 1 1 2 3 5 8 13
18. Write a program that takes a string with multiple words and then capitalize
the firstletter of each word and forms a new string out of it.
Program-21 Write a program to find the minimum and maximum number in a list.
Output:
Enter the elements: 56,78,98,23,11,77,44,23,65
Enter the element that you want to search : 23
Element found at the position : 4
24. Counting the frequencies in a list using a loop
def CountFrequency(my_list):
freq = {}
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
# Driver function
if __name__ == "__main__":
my_list = [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2,
2, 2]
CountFrequency(my_list)
Output:
1 : 5
2 : 4
3 : 3
4 : 3
5 : 2
Time Complexity: O(N), where N is the length of the list.
a1 = min(x, y, z)
a3 = max(x, y, z)
a2 = (x + y + z) - a1 - a3
Output:
P -> 2
y -> 1
t -> 1
h -> 1
o -> 2
n -> 2
-> 1
r -> 2
g -> 2
a -> 1
m -> 2
i -> 1
Local Scope
A variable created inside a function belongs to the local scope of
that function, and can only be used inside that function.
def myfunc():
x = 300
print(x)
myfunc()
Example
The local variable can be accessed from a function within the function:
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
Global Scope
A variable created in the main body of the Python code is a global variable and
belongs to the global scope.
Global variables are available from within any scope, global and local.
Example
A variable created outside of a function is global and can be used by anyone:
x = 300
def myfunc():
print(x)
myfunc()
print(x)