PYTHON BASIC PROGRAM -theory part 2
PYTHON BASIC PROGRAM -theory part 2
def greet(name):
print(f"Hello, {name}!")
In this example, the greet function takes one argument called name. When the
function is called, a value can be provided for the name argument. For
instance:
greet("Alice")
Output:
Hello, Alice!
Here, the argument "Alice" is passed to the greet function, and the function
uses that value to print a personalized greeting.
2. Return Values: Return values are the results or outputs that a function
produces after performing its task. Functions can optionally return a value
using the return statement.
Here's an example of a function with a return value:
3. Function calling
result = add_numbers(3, 4)
print(result)
Output:
Here, the function add_numbers is called with the arguments 3 and 4. The
function adds these numbers and returns the result 7, which is then stored in
the result variable. Finally, the value of result is printed.
Note that a function can have multiple return statements or no return
statement at all. If a function doesn't explicitly return a value, it returns None
by default.
Additionally, functions can return multiple values as a tuple or any other
iterable object. Here's an example:
print(result)
Output:
(20, 18)
EXCEPTIONS
Exceptions are the unusual event that occurs during the execution of the
program that interrupts the normal flow of the program. Generally, exceptions
occur when the code written encounters a situation it cannot cope with.
Whenever an exception is raised, the program stops the execution, and thus
the further code is not executed. Therefore, an exception is a python object
that represents a run-time error.
try:
Statements to be executed
except:
Statements get executed if an exception occurs
example.
num=10
try:
print("The number is:",Num)
except:
print("An exception occurred")
Output:
An exception occurred
try:
Statements to be executed
......................
except ExceptionI:
If ExceptionI occurs, this block gets executed.
except Exception2:
If Exception2 occurs, this block gets executed.
:
:
:
example.
num=10
try:
div=num/0
except ZeroDivisionError:
print("Zero cannot be in the denominator")
except SyntaxError:
print("There is some error in the syntax")
Output:
Zero cannot be in the denominator
3.We can also have else block along with the try/except blocks. The code in the
else block executes only if the code in the try block does not result in an
exception.
num=10
try:
div=num/2
except:
print("An exception occurred")
else:
print("The value is",div)
Output:
The value is 5.0
While the card-playing, we compare the hands of cards with each other. Most
of the player likes to sort the card in the ascending order so they can quickly
see which combinations they have at their disposal.
The insertion sort implementation is easy and simple because it's generally
taught in the beginning programming lesson. It is an in-place and stable
algorithm that is more beneficial for nearly-sorted or fewer elements.
The insertion sort algorithm is not so fast because of it uses nested loop for
sorting the elements.
The array spilled virtually in the two parts in the insertion sort - An unsorted
part and sorted part.
The sorted part contains the first element of the array and other unsorted
subpart contains the rest of the array. The first element in the unsorted array is
compared to the sorted array so that we can place it into a proper sub-array.
It will repeatedly happen until the all element is inserted at correct place.
To sort the array using insertion sort below is the algorithm of insertion sort.
We will consider the first element in the sorted array in the following array.
[10, 4, 25, 1, 5]
[10, 4, 25, 1, 5]
Now we take the first element from the unsorted array - 4. We store this value
in a new variable temp. Now, we can see that the 10>4 then we move the 10 to
the right and that overwrite the 4 that was previously stored.
Here the 4 is lesser than all elements in sorted subarray, so we insert it at the
first index position.
Now check the number 25. We have saved it into the temp variable. 25> 10 and
also 25> 4 then we put it in the third position and add it to the sorted sub
array.
Again we check the number 1. We save it in temp. 1 is less than the 25. It
overwrites the 25.
Now, we have 4 elements in the sorted subarray. 5<25 then shift 25 to the
right side and pass temp = 5 to the left side.
Now, we get the sorted array by simply putting the temp value.
Merge sort is similar to the quick sort algorithm as works on the concept of
divide and conquer. It is one of the most popular and efficient sorting
algorithm. It is the best example for divide and conquer category of algorithms.
It divides the given list in the two halves, calls itself for the two halves and then
merges the two sorted halves. We define the merge() function used to merging
two halves.
The sub lists are divided again and again into halves until we get the only one
element each. Then we combine the pair of one element lists into two element
lists, sorting them in the process. The sorted two element pairs is merged into
the four element lists, and so on until we get the sorted list.
We have divided the given list in the two halves. The list couldn't be divided in
equal parts it doesn't matter at all.
Merge sort can be implement using the two ways - top-down approach and
bottom-up approach. We use the top down approach in the above example,
which is Merge sort most often used.
The bottom-up approach provides the more optimization which we will define
later.
The main part of the algorithm is that how we combine the two sorted sublists.
Let's merge the two sorted merge list.
o A : [2, 4, 7, 8]
o B : [1, 3, 11]
o sorted : empty
First, we observe the first element of both lists. We find the B's first element is
smaller, so we add this in our sorted list and move forward in the B list.
o A : [2, 4, 7, 8]
o B : [1, 3, 11]
o Sorted : 1
Now we look at the next pair of elements 2 and 3. 2 is smaller so we add it into
our sorted list and move forward to the list.
o A : [2, 4, 7, 8]
o B : [1, 3, 11]
o Sorted : 1
Continue this process and we end up with the sorted list of {1, 2, 3, 4, 7, 8, 11}.
There can be two special cases.
o What if both sublists have same elements - In such case, we can move
either one sublist and add the element to the sorted list. Technically, we
can move forward in both sublist and add the elements to the sorted
list.
o We have no element left in one sublist. When we run out the in a sublist
simply add the element of the second one after the other.
We should remember that we can sort the element in the any order. We sort
the given list in ascending order but we can easily sort in descending order.