# Python Programming Examples

This section covers various examples in Python programming Language. These Programs examples cover a wide range of programming areas in Computer Science. Every example program includes the problem description, problem solution, source code, program explanation and run time test cases. These examples range from simple Python programs to Mathematical functions, lists, strings, sets, dictionary, recursions, no-recursions, file handling, classes and objects, linked list, stacks and queues, searching and sorting, trees, heap, graphs, games, greedy algoritms and dynamic programming. Here’s a listing of all the Python programs covering all these areas.

## 1. Simple Python Programs

This section deals with Simple Python Programs. The Python programs in this section performs arithmetic operations like Addition, Multiplication and Division on the given set of input numbers. This section also covers python programs on reversing the given number and checks whether the given number is positive or negative number. The other programs in this section checks whether a given number is palindrome or not, prints identity matrix and inverted star pattern, print prime numbers in a given range using sieve of eratosthenes method. It also talks about python programs on exchanging two number values without using any temporary variables, counting the number of integers in a given number, entering the student marks in 5 subjects and displaying the grades.

## 2. Python Programming Examples on Mathematical Functions

The following section contains programs on different types of mathematical functions. These include programs to checks whether the given number is strong number, perfect number, prime number or armstrong number. It also checks whether a given year is leap year or not. The Python programming examples also covers programs on performing simple interest with given values, printing the largest even and odd number in the given list. Python programs on number conversions like converting celcius temperature to farenheit temperature, gray code to binary and binary code to gray. This section also covers python programs on finding the lcm and gcd of two numbers and also checks whether the two given numbers are amicable numbers or not. The other programs in this section prints all prime numbers in a given range, print the pascal triangle, programs on trigonometric functions such as sin(x) and cos(x).

advertisement

## 3. Python Programming Examples on Lists

This section contains Python programs examples on Lists. List is a collection of data or information which is ordered and changeable. Lists are written with square brackets. List is used to store the multiple data at a same time. Each element which is inside the list is called an item. The Python programs in this section finds the largest and second largest number in the given list, it also finds the second largest number using bubble sort algorithm. The Python programming examples also contains programs on sorting the given list according to their lengths, merging two lists and sort it, finds the intersection of two lists. The other python programs in this section performs swapping values and removing duplicate values from the list, program to solve maximum subarray problem using kadane’s and divide and conquer algorithms. It also talks about python programs on finding the number which is having perfect square in a given range of numbers.

## 4. Python Programming Examples on Strings

The Python programming examples also contains programs on Strings. Python strings are immutable it means we cannot changed once its created. Python has a built in string class named str with many features. String is a sequence of characters that exist as an object of the class. The Python programs in this section performs replace and remove operations in the given string, detects the string if it is anagrams by using sorted() function. This section also covers python programs on vowels count in a given string and also calculates the string length without using any library functions, checks whether the given string is palindrome or not. It also talks about python programs on doing calculations of upper and lower case letters count in the given string and it also calculates how many numbers and characters are in the string. The other programs in this section performs word occurence count in the given sentence, checks whether a given string is pangram or not, prints all string permutations in an lexicographic order with and without using recursion.

## 5. Python Programming Examples on Dictionary

This section contains Python programming examples on Dictionary. Dictionary is a mapping of unique keys to values. Dictionaries are mutable that means it can be changed. To construct the dictionary we will use curly brackets {}, square brackets[] is used to index it. Key and values can be seperated by using comma and colon between the pairs. The Python programs in this section adds a key value pair to the dictionary, concatenates two dictionary into one, checks whether the given key is exists in dictionary or not. The Python programming examples also covers programs on dictionary generation that contains the numbers in the format and removes the specific key from the dictionary. The other Python programs in this section performs addition and multiplication of the items in the dictionary, count the word occurence in the given string using dictionary.

## 6. Python Programming Examples on Sets

The following section contains Python programming examples on Sets. Set is a collection of data which is unordered and unindexed. String is a sequence of characters that exist as an object of the class. The Python programs in this section checks common letters in the given two input strings, displays the letters which are present in the both strings. This Python programming examples in this sections covers programs on displaying the letters present in the first string also counts the number of vowels present in the string by using sets.

## 7. Python Programs with Recursions

This section contains Python programming examples on Recursions. Recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive. Recursion is a method in which the solution of a problem depends on the smaller instances of the same problem. The Python programs in this section determines whether a given number is even or odd, finds the fibonacci series and factorial number using recursion. This programming examples in this sections covers programs on finding the lcm and gcm of two numbers using recursion and also checks whether given number is a prime number or not. It also talks about python programs on reversing a string using recursion. The other Python programs in this section finds the length list using recursion and also finds the sum of the given digits.

## 8. Python Programs without Recursions

The Python programs in this section finds the fibonacci series and factorial number without using recursion. The Python programming examples also covers programs on reversing the string without using recursion and program to find odd numbers and palindromes between a given range of numbers. It also talks about python programs on finding the sum of given digits without using recursion.

advertisement

## 9. Python Programs on File Handling

This section contains Python programs examples on File Handling. File is a collection of data and information. The Python programs in this section reads the contents of a file, count the words and lines number in a text file. This section also covers python programs on word occurrences count in a given text file, copying and append the contents from one file to another file. The Python programming examples also covers programs on reading a text file and find the letter apperance count in the given text file. It also talks about python programs to count blank spaces in the text file, reading the contents of a given text file in a reverse order. The other programs in this section reads a file and capitalize first letter of every word in the given file.

## 10. Python Programs on Classes and Objects

This section contains Python programming examples on Classes and Objects. Class can be defined as a template that describes the behavior that the object of its type support. Class is declared by use of the class keyword. The Python programs in this section to find the rectangle area using classes and modifies the data in list using class. The Python programming examples also covers programs on creating a class and finds the area and perimeter of the circle and also finds all possible subsets. The other Python programs in this section creates a class in one method and prints in another method.

## 11. Python Programming Examples on Linked Lists

The following section gives a collection of Python Programs which are categorized into various Categories like Programs on Creating and Displaying the Elements of a Linked List, Programs on Search and Display Functions of a Linked List, Programs on Implementation of Count, Programs on Length and Print Operations on a Linked List, Programs on Implementation of other Data Structures using Linked List, Programs dealing with the Operations on the Elements of a Linked List, Programs on Print, Reverse and Read Operations, Programs on Binary Tree Implementation using Linked List, Programs on Interchange and Modify Operations, Programs on Singly Linked Lists, Programs on Circularly & Doubly Linked Lists. The following link will guide you to the various programs that belong to the above mentioned category.

## 12. Python Programming Examples on Stacks & Queues

In this section, we have Programs on Stack Implementation, Programs on Queue Implementation, Programs on String Implementation. The following link contains various Python programs on Stacks & Queues under the category mentioned above.

## 13. Python Programming Examples on Searching and Sorting

In this section, we have various Programs on Searching and Sorting. These include Programs on Searching Algorithms, Programs on Sorting Algorithms, Programs on Sorting using Binary Trees. The link below illustrates all the Programs that are Categorized as above.

## 14. Python Programming Examples on Trees

This section deals with categories of Trees. They include Programs on Tree Traversals, Programs on Heap and Binary Tree Implementation, Programs dealing with the Nodes of a Tree, Programs on Special Properties of Binary Trees, Programs on BFS and DFS Traversals and Programs on Inorder Traversal of a Binary Tree. The link below will guide us through all of the above mentioned Programs.

advertisement

## 15. Python Programs on Heap

In this section, we have various Python Programs on Heap. These include Programs on binary heap, binomial tree and binomial heap. Binary heap is a heap data structure it takes the form of a binary tree. It is mainly used to implement priority queues. Binomial heap is implemented as a set of binomial trees which satisfies the binomial heap properties. This section also covers python programs on fibonacci heap, ternary and d-ary heap. Fibonacci heap is a heap data structure consisting of a collection of trees. Fibonacci heap has a better amortized running time than a binomial heap. In ternary heap each node has up to three children. Ternary Heap is implemented by using the concept of D-ary Heap.

## 16. Python Programming Examples on Graphs

This section deals with programs on Graphs. These include Programs on Connected Components using BFS, Programs on Connected Components using DFS, Programs on Shortest Path. The link below guides us through all of these Programs.

## 17. Python Programs on Games

This section contains Python programming examples on Games. These includes the implementation of tower of hanoi problem. It also talks about python programs on solving celebrity problem and n-queen problem with and without using recursion. A celebrity is someone who doesn’t know anyone (including themselves) but is known by everyone. N-queen problem is the problem of placing N queens on an n x n chessboard such that no queen can attack another queen.

## 18. Python Programs on Greedy Algorithms

This section deals with Python programs on Greedy Algorithms. These include Python Programs on fractional knapsack and interval scheduling problem using Greedy Algorithm. Greedy Algorithms gives optimal solution for all subproblems. But greedy algorithm cannot be used to solve all the dynamic programming problems. This section also covers python programs on closed intervals unit and lateness minimize using greedy algorithm. In Interval Scheduling Problem, the problems are consider as a set of tasks. Each task is represented by an interval describing that which time it needs to be executed. Fractional Knapsack Problem is used to find the maximum total weight.

## 19. Python Programs on Dynamic Programming

This section contains Python programming examples on Dynamic programming. Dynamic programming is a method used for solving a complex problem by breaking it down into simpler subproblems, solving each of those subproblems just once and stores their results in an array. While solving the complex problem, if the same subproblem occurs again we can reuse the already stored result instead of recomputing its result is called memoization. Memoization uses the top down technique to solve the problem. Tabulation uses the bottom up approach to solve the problem. The Python programs in this section to solve rod cutting problem and matrix chain multiplication using dynamic programming with bottom up approach and memoization. The other Python programs in this section prints fibonacci number and also finds the longest common substring using dynamic programming.