Skip to main content

Posts

Showing posts from September, 2021

Merge Sort Algorithm in PYTHON

  First of all, Why do we need to learn so many sorting algorithms? We need to learn so many sorting algorithms because there are cases in which which algorithm fits best. According to the time complexity, we have to choose an algorithm of our use. That's why we are learning so many sorting algorithms. SO LETS START WITH THE MERGE SORT In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. It is one of the most popular algorithms and one of the most stable sorting algorithm. ALGORITHM 1.) First of all take a list and put it in a function (recursion) that it breaks itself into two halves. 2.) When we get the two halves, the recursion code will automatically breaks the two halves into four             parts and then into 8 parts until each...

Selection Sort Algorithm in PYTHON

In computer science, selection sort is an in-place comparison sorting algorithm. It is inefficient on large list. It has a performance wore than Insertion sort. But at some case it proves its simplicity and advantages. The time complexity of the selection sort is the same in all cases. At every step, you have to find the minimum element and put it in the right place. The minimum element is not known until the end of the array is not reached. ALGORITHM 1.) First of all take a list, and look for the minimum element. 2.) Place that minimum element at the first position and the first element at the index of that         minimum element. 3.) Now fix the minimum at first position, now look for the minimum element in the list              except the first element because it is already sorted. Now place the minimum element            on  second position and that second element at the index of that min...

Insertion Sort Algorithm in PYTHON

Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e, the position to which it belongs in a sorted array. It iterates the input elements by growing the sorted array at each iteration. It compares the current element with the largest value in the sorted array. Insertion is a simple way or simple algorithm to sort a list. It is much more efficient than bubble sort. But for larger list Insertion sort is not suggested. For that we will study about merge sort and quick sort algorithms. But in this this tutorial we will learn INSERTION SORT algorithm and data structure. ALGORITHM 1.) First of all take a list. Now compare the second element of the list with left hand side elements of the list i.e. the first element. If second element id greator than swap. 2.) Now take third element, compare it with the left hand side elements i.e. first and second element and place the element accordingly the first and seco...

Bubble Sort Algorithm in PYTHON

What is Bubble Sort ? And why do we need this ? Bubble sort is the simplest algorithm to sort any array. Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm.  In computer graphics it is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to x axis) and with incrementing y their order changes . ALGORITHM :- 1.) Firstly we have an unsorted list, which is to be sorted. 2.) Consider the first element of the list. Then compare it with the next element. 3.) If the second element is greator than the first element than swap them. 4.) If the second element is smaller than the first element, then we don't have to do anything. 5.) Going ahead, now after swap (may or may not), look for second...

Functions and Recursion in PYTHON

  A Function is a group of statements performing a particular task. When a program gets bigger in size and its complexity increases, it gets difficult for a programmer to keep track on which piece of code is doing what ! A function can be reused by the programmer in a given program any number of times as he wants. Syntax of a Function in Python :- def func():     print("Hello") #this function can be called any number of times. Function cell :- Whenever we want to call a function we put the name of the function as 'def func_name():'  Function definition is the exact set of instruction which are to be excuted. Lets write an easy code :- def greet(name): print("Good Day, " + name + "!") greet("John") #here we call the function Good Day, John! It was an easy program, lets see some other examples :- Now we will make a programme of summation using function :- def sum(num1, num2): mysum = num1 + num2 return mysum n1 = 2 n2 = 7 pr...

Loops in PYTHON (For loop and While loop)

Why do we need loops in python ? And how it makes our codes more simpler and shorter ?   Sometimes we want to repeat a set of statements in our programme for instance. If we want to print 1 - 1000 in our python programme. Then it is not easy to print every number separately. Thus we need loops because using only 2-3 lines of code we can do this. Loops make it easy for a programmer to tell the computer, which set of instruction to repeat as much as times we want. Types of loops in Python :- 1.) While loop 2.) For loop We will look into these one by one ! 1.) WHILE LOOP :- The syntax of a while loop looks like this : while(condition):     #code accordingly     #this code will keeps working until the condition is satisfied In while loops, the condition is checked first. If comes true, the body code will starts running else not. If the loop is started it will runs until the condition becomes false. Now lets write a code to print all the numbers from 1 - 100 :- i =...

Automate What's App Messager in PYTHON

Why are we doing this ?  Only for knowledge, because in this tutorial, we will learn about how we can give commands and open other applications or website and search without typing any word in the search bar or typing automatically. To learn this, we should have knowledge of following libraries :- 1.) PyAutoGUI 2.) Webbrowser 3.) Time We can install these libraries by just going into the term inal, as shown in the image :- And by the same method, we can install 'webbrowser' and 'time' libraries By just giving commands 'pip install webbrowser' and we don't have install time using pip because it is our standard library and is installed already along with python. Now lets talk about their properties and working, so we can understand better. 1.) PyAutoGUI  is cross-platform GUI automation module that works on Python 2 & 3. You can control the mouse and keyboard as well as perform basic image recognition to automate tasks on your computer. 2.) Webbrowser m...

Conditional Expressions (If/Else or IF/Elif/Else conditions) in Python

Sometimes we want to go outside if its a weekend.  Sometimes we want to eat ice-cream if its hot . Sometimes we go for a party with our friends if our parents allow. All these are the decisions which depends upon a condition. In python programming too, we must met those condition to take a decision.  For example :- If the value of variable i is 2. Then we will print the value of i if it is greator than 3. Then in this example our condition doesn't met properly. So as a result nothing will be printed.  So here to give these commands to the computer, we must know the syntax. Here we need If/Else condition or if there are many conditions then we will apply If/Elif/Else condition. So lets take an example :- # let we have a variable i and the stored integer is 6. i = 6 #now, we will apply the condition if i is divisible by 2 then print "Yep" , #if it is not divisible by 3, then print "Nope". #now I think you understood the condition, lets apply it with a prope...

Dictionary and Sets in PYTHON

What do you mean Dictionary ?? A dictionary is a collection of key-value pairs. Its theory is not so important we are directly going on its syntax i.e. the way to write it in python language. Let we have a dictionary named mydict, so we can write it as :- mydict  =  { "Fast": "In a quick manner", "Hello": "Hey", "See": "Watch" } This above line is the syntax, how we write a dictionary in the terminal. If you notice, in the above dictionary we have some words in the left and their meanings in the right. Fast - In a quick manner Hello - Hey See - Watch We can add more words and their meanings according to our choice. Now let us see an example :- mydict = { "Fast": "In a quick manner", "Hello": "Hey", "See": "Watch"} print("The value of the key "Hello" is : ", mydict['Hello']) The value of key "Hello" is : Hey We can also use int...

List and Tuple in PYTHON

First of all, do you know what is a 'list' ?? If no, then don't worry and if yes congratulations ! List :-  A number of connected items or names written or printed consecutively, typically one below the other . Tuple :-  A tuple is a collection which is ordered and unchangable . But lists are changable . So now defining each of the above in a detailed manner, so that everyone could understand and after that we will implement these in our programme. 1.) LISTS Python lists are the containers to store a set of values of any type of data (int, str, float, boolean). For example:- If 'myname' is the name of that list and its components are Rohan, Mohan, Ram and John. So the code to write it is as follow :- myname  =  ['Rohan', 'Mohan', 'Ram', 'John'] Another examples :- mynums  =  [4, 2, 97, -43, 0] mylist  =  [5, 0.33, 'Veer', 1] Hope you understand how to write a list in python. Now talking about list indexing. If we have a list n...

Using Python as Calculator

In this tutorial, we will learn how we can use python programming language as a calculator. To learn this you should have knowledge of int, str, float:- Int:- It is integer number like 2, 5, 0, -2 etc. Str:- It is string means string like a, j, hello, my name etc. Float:- It is the floating point number like 3.8776, 1.874, 0.176 etc. So lets start with our first programme, which is addition of two numbers. #first we will store two integers in two variables respectively. #let the two integers are 3 and 5. a = 3 b = 5 sum = a + b print("The sum of a and b is : ", sum) The sum of a and b is : 8 So, as we have seen above that the sentence in print command  is printed as it is and at last the sum of two numbers is printed. In our next programme, we will study other calculations like substraction , multiplication etc. a = 6 b = 2 sum = a +b diff = a - b product = a*b quotient = a/b remainder = a%b print("The sum of a and b is :", sum) print("The difference between a...

First Python Programme

  If you are here, then I think you are a python enthusiast. On this website, we upload daily posts on new and basic programme for beginners.  So let’s start with the first code i.e. Hello World programme. So first of all you you should know how to give print command in python. To print we write print(“Hello World”). What’s inside the small brackets will be printed. So the programme is : print("Hello World") Hello World So, as we can see above, when we implemented the print command, the thing between the small bracket is printed. So let’s see some other examples In this example, we will store an integer value in a variable. And then print the integer will the help,of that variable. So let’s get started a = 3 print(a) 3 Another example Now we will store a string into a variable and then print the string with the help of that variable #if we put hastag in front of any line in python. Then there is no effect. #for storing the string in a variable, we have to enclose the whole s...