├── .deepsource.toml ├── LICENSE ├── Patterns └── Floyd's_Triangle ├── README.md ├── SearchingAlgorithms ├── GraphSearch │ ├── astar.py │ ├── breadth_first_search.py │ ├── depth_first_search.py │ └── dijkstra.py ├── Linear_Search.py └── binary_search.py ├── SimpleAddition ├── Sum_2_numbers.py ├── Sumof2no.py ├── for_loop_addition.py ├── sum.py ├── sum_classes.py ├── sum_decorator.py └── sum_numbers_from_arguments.py └── SortingAlgorithms ├── MergeSort └── MergeSort.txt ├── bubble_sort.py └── insertion_sort.py /.deepsource.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | 3 | [[analyzers]] 4 | name = "python" 5 | enabled = true 6 | 7 | [analyzers.meta] 8 | runtime_version = "3.x.x" 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Patterns/Floyd's_Triangle: -------------------------------------------------------------------------------- 1 | # Python 3 Program - Print Floyd Triangle 2 | 3 | 4 | ran = input("Upto how many line ? ") 5 | 6 | rang = int(ran) 7 | k = 1 8 | for i in range(1, rang+1): 9 | for j in range(1, i+1): 10 | print(k, end=" ") 11 | k = k + 1 12 | print() 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python - Beginner's Guide 2 | 3 | 4 | This will be an Open Guide for all beginners in Python ! You can read and learn simple Python codes here , and whats more interesting is that you can even Contribute !!! 5 | 6 | 7 | #Add whatever sample program you feel like is necessary for a beginner and make sure to add enough comments so that a beginner understands just by reading what each line of the code does ! 8 | 9 | If you are using any concept directly make sure to explain that using multiline comments. 10 | -------------------------------------------------------------------------------- /SearchingAlgorithms/GraphSearch/astar.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenGuide/Python-Guide-for-Beginners/15e659ea1ddfeb33c5d5e44ad1325a58ad6c9b11/SearchingAlgorithms/GraphSearch/astar.py -------------------------------------------------------------------------------- /SearchingAlgorithms/GraphSearch/breadth_first_search.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenGuide/Python-Guide-for-Beginners/15e659ea1ddfeb33c5d5e44ad1325a58ad6c9b11/SearchingAlgorithms/GraphSearch/breadth_first_search.py -------------------------------------------------------------------------------- /SearchingAlgorithms/GraphSearch/depth_first_search.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenGuide/Python-Guide-for-Beginners/15e659ea1ddfeb33c5d5e44ad1325a58ad6c9b11/SearchingAlgorithms/GraphSearch/depth_first_search.py -------------------------------------------------------------------------------- /SearchingAlgorithms/GraphSearch/dijkstra.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenGuide/Python-Guide-for-Beginners/15e659ea1ddfeb33c5d5e44ad1325a58ad6c9b11/SearchingAlgorithms/GraphSearch/dijkstra.py -------------------------------------------------------------------------------- /SearchingAlgorithms/Linear_Search.py: -------------------------------------------------------------------------------- 1 | 2 | # linear search of an item in the list inputted by the user (python 2.7) 3 | print("Enter numbers to be stored in a list (space separated") 4 | 5 | """ map function in order to store the values of the two space separated integers 6 | map (function, iterable..) applies the function on all the items of the iterable """ 7 | 8 | # linear search of an item in the list inputted by the user 9 | 10 | l = map(int, raw_input().split()) #enter space separated entries in a list 11 | item = input() #item to be searched for 12 | found = 0 #check to find if th eitem has been found or not 13 | 14 | for i in range(len(l)): #iterating through the array 15 | if(l[i] == item): #if element found 16 | found = 1 17 | print('Item found in the list at index position : ' + str(i)) 18 | break 19 | if(found == 0): #if element not found 20 | print("Sorry, Item not found in the list ") 21 | -------------------------------------------------------------------------------- /SearchingAlgorithms/binary_search.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Python 3 3 | 4 | Binary search is a really effective search algorithm that requires the searched array to be sorted. 5 | The basic idea is that we all the time split our array in half, and determine which half our wanted item 6 | is located. This way, we continuously cut the area we search in half, and in the end, we will have found our 7 | item. 8 | ''' 9 | 10 | def binary_search(needle, haystack, low = 0, high = None): 11 | ''' 12 | Searches for the needle using the binary search algorithm. 13 | This is a recursive implementation. 14 | :param needle: The item we want to locate 15 | :param haystack: The array in which we want to search for the needle 16 | :param low: The lower limit of where in the haystack we want to search. 17 | :param high: The upper limit of where in the haystack we want to search. 18 | :return: The index of the needle in the haystack, or -1 if it is not present 19 | ''' 20 | 21 | # If no value for high is specified, we should search until the end of the array 22 | if high == None: 23 | high = len(haystack) 24 | 25 | # If the high limit is lower than the low, the haystack does not contain the needle 26 | if high < low: 27 | return -1 28 | 29 | # We calculate the mid point of our array (rounding it to closest integer) 30 | mid = int((low + high) / 2) 31 | 32 | if haystack[mid] > needle: 33 | # If the middle element is higher than the needle, we need to search the lower half of the haystack 34 | return binary_search(needle, haystack, low, mid - 1) 35 | elif haystack[mid] < needle: 36 | # If the middle element is lower than the needle, we need to search the upper half of the haystack 37 | return binary_search(needle, haystack, mid + 1, high) 38 | else: 39 | # If the middle element is neither lower or higher than the needle, the needle is located in the mid position 40 | return mid 41 | 42 | if __name__ == '__main__': 43 | needle = 3 44 | haystack = [0, 1, 2, 3, 4, 5, 6, 7] 45 | index = binary_search(needle, haystack) 46 | print('The result for needle', needle, 'in haystack', haystack, 'is', index) 47 | 48 | needle = 0 49 | haystack = [1, 2, 3, 4, 5, 6, 7] 50 | index = binary_search(needle, haystack) 51 | print('The result for needle', needle, 'in haystack', haystack, 'is', index) -------------------------------------------------------------------------------- /SimpleAddition/Sum_2_numbers.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | # first lets get 2 numbers to add 4 | print "enter 2 number to add" 5 | py 6 | # getting the first number 7 | number1 = raw_input("Ok enter the first number ") 8 | 9 | # and now the second number 10 | number2 = raw_input("and now the second number ") 11 | 12 | # finally calculating the addidition of the numbers 13 | # sum = number1 + number2 14 | # seems to be right 15 | # but as the inputs are taken as string we need to type cast them 16 | # that is convert number1 and number2 to integer from string 17 | # so lets do that 18 | 19 | number1 = int(number1) 20 | number2 = int(number2) 21 | # and now lets calculate the sum 22 | sum = number1 + number2 23 | 24 | # and now lets print the numbers 25 | print "the addition of the 2 numbers is " 26 | print sum 27 | 28 | 29 | -------------------------------------------------------------------------------- /SimpleAddition/Sumof2no.py: -------------------------------------------------------------------------------- 1 | #python program to add two numbers 2 | x = input("Enter a number: ") 3 | y = input("Enter another number: ") 4 | 5 | sum = int(x) + int(y) 6 | 7 | print("The sum is: ", sum) 8 | -------------------------------------------------------------------------------- /SimpleAddition/for_loop_addition.py: -------------------------------------------------------------------------------- 1 | def for_loop_addition(a,b): 2 | ''' 3 | Add two numbers in strange way 4 | ''' 5 | result = 0 6 | 7 | for i in range(a): 8 | result += 1 9 | 10 | for j in range(b): 11 | result += 1 12 | 13 | return result 14 | -------------------------------------------------------------------------------- /SimpleAddition/sum.py: -------------------------------------------------------------------------------- 1 | # Simple Python program to Add Two Numbers 2 | 3 | num1 = input(" Please Enter the 1st Number: ") 4 | num2 = input(" Please Enter the 2nd number: ") 5 | 6 | # Using arithmetic + Operator to add two numbers 7 | sum = float(num1) + float(num2) 8 | print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) 9 | -------------------------------------------------------------------------------- /SimpleAddition/sum_classes.py: -------------------------------------------------------------------------------- 1 | # This example uses python classes for addition 2 | 3 | class Numbers(object): 4 | def __init__(self): 5 | self.sum = 0 6 | 7 | def add(self,x): 8 | # Addtion funciton 9 | self.sum += x 10 | 11 | def total(self): 12 | # Returns the total of the sum 13 | return self.sum 14 | 15 | 16 | 17 | if __name__ == "__main__": 18 | # Prints 12 on the terminal when the file is run, 19 | # you can even use input() to get numbers from 20 | # users. 21 | 22 | add = Numbers() 23 | add.add(5) 24 | add.add(7) 25 | y = add.total() 26 | print("Total Sum : " , y) 27 | -------------------------------------------------------------------------------- /SimpleAddition/sum_decorator.py: -------------------------------------------------------------------------------- 1 | # Most python newbies may not be knowing about decorators 2 | # They are inbuit python funcitons also know as magic function 3 | # They have a general format like __self__ , __SOMETHING__ etc. 4 | 5 | # So for this script we'll be using Pyhton decorators to add 6 | # numbers 7 | 8 | # 1. Simple Addition 9 | print (5).__add__(7) # prints 12 10 | 11 | print (2).__add__(1) # prints 3 12 | 13 | # 2. Functions 14 | 15 | class Number(int): 16 | ''' 17 | i have not use '+' to add self and other as '+' references to 18 | __add__ , hence it would create an infinite recursion. 19 | So we used two '-' to add the two numbers. 20 | ''' 21 | def __add__(self, other): 22 | return self - (-other) 23 | 24 | print Number(5) + 3 # Prints 8 25 | 26 | -------------------------------------------------------------------------------- /SimpleAddition/sum_numbers_from_arguments.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Python 3 3 | Takes two numbers given as command line arguments, and prints their sum 4 | ''' 5 | 6 | # The sys library lets us access command line arguments 7 | import sys 8 | 9 | 10 | # This line ensures the code will only run if the file is launched as a program - not if it is imported as a module 11 | if __name__ == '__main__': 12 | 13 | # The sys.argv array contains the command passed to Python. 14 | # The first element is the name of the program, while the following elements are command-line arguments. 15 | # The float function converts the argument, which is a string, to a decimal number. 16 | x = float(sys.argv[1]) 17 | y = float(sys.argv[2]) 18 | 19 | # To get the sum of two numbers, we simply use the + operator 20 | sum = x + y 21 | 22 | # Finally, we print our result to the console 23 | print(sum) -------------------------------------------------------------------------------- /SortingAlgorithms/MergeSort/MergeSort.txt: -------------------------------------------------------------------------------- 1 | Write a Python function that takes an unsorted list and sorts it using merge sort. 2 | -------------------------------------------------------------------------------- /SortingAlgorithms/bubble_sort.py: -------------------------------------------------------------------------------- 1 | # fist a quick introduction about Bubble sort 2 | # Bubble sort is a simple sorting algorithm. It repeatedly loops through the array and 3 | # compares each pair of adjacent items and swaps them if they are in the wrong order. 4 | # Read more at Wikipedia : https://en.wikipedia.org/wiki/Bubble_sort 5 | 6 | # lets start with getting the user input, a random sequence of number 7 | print "Enter numbers to be sorted (seperated by space)" 8 | # seperating the numbers by spaces would help us in getting individual number from the input string 9 | 10 | iplist = map(int, raw_input().split()) 11 | # raw_input() gets the sequence of numbers (seperated by space) 12 | # split(), split the input string in individual numbers by spaces 13 | # so if the input is "23 52 8" 14 | # after .split() the input becomes ["23", "52", "8"] 15 | # at the end we type cast all the string input to integers 16 | 17 | for i in xrange( len( iplist ) ): 18 | for k in xrange( len( iplist )-1, i, -1 ): 19 | if ( iplist[k] < iplist[k-1] ): 20 | tmp = iplist[k] 21 | iplist[k] = iplist[k-1] 22 | iplist[k-1] = tmp 23 | 24 | # lets break that down 25 | # len() gives the length or the number of elements inside a list 26 | # now we loop over the list 27 | # as we are arranging list in ascending order 28 | # we swap adjacent elements if they are not in order 29 | # and we do this till all the elements are in proper order 30 | 31 | # fnially lets print the results 32 | print iplist -------------------------------------------------------------------------------- /SortingAlgorithms/insertion_sort.py: -------------------------------------------------------------------------------- 1 | # A quick intro to Insertion sort 2 | # It is sorting algorithm that builds the final sorted list one item at a time. 3 | # At each iteration, insertion sort removes one element from the input data, 4 | # finds its proper location within the sorted list, and inserts it there. 5 | # Read more at Wikipedia : https://en.wikipedia.org/wiki/Insertion_sort 6 | 7 | # lets get the user input 8 | print "Enter numbers to be sorted (seperated by space)" 9 | 10 | iplist = map(int, raw_input().split()) 11 | # for detailed explanation look up at bubble_sort.py 12 | 13 | for index in xrange( 1, len(iplist) ): 14 | currentvalue = iplist[index] 15 | position = index 16 | 17 | while ( position > 0 and iplist[position-1] > currentvalue ): 18 | iplist[position] = iplist[position-1] 19 | position = position - 1 20 | 21 | iplist[position] = currentvalue 22 | 23 | print iplist 24 | --------------------------------------------------------------------------------