Python Programs

9
Jul

Encrypt Text

Question:

Write a Program to encrypt text by replacing each character by it’s successor.

Program:

Output:
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Addition Using Magic Methods

Question:

Using Magic Methods, overload the + operator so that length of varying units can be added. 

Program:

Explanation:

What are magic methods? They’re everything in object-oriented Python. They’re special methods that you can define to add “magic” to your classes, that is you won’t have to call them manually. They are called automatically behind the scenes.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Sum of Natural Numbers Using Recursion

Question:

Write a python program to find sum of natural numbers using recursion

Program:

Explanation:

A recursive function (DEF) is a function which either calls itself or is in a potential cycle of function calls.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Print ASCII Character

Question:

Write a python program to find ASCII character of a variable

Program:

Explanation:

ASCII (American Standard Code for Information Interchange) is the most common format for text files in computers and on the Internet. In an ASCII file, each alphabetic, numeric, or special character is represented with a 7-bit binary number (a string of seven 0s or 1s). 128 possible characters are defined.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Divisibility of 13 Using Lambda

Question:

Use anonymous and filter functions and print a list of numbers divisible by 13 from the list [79, 65, 54, 39, 555, 339,623,]

Program:

Explanation:

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the functions filter(), map() and reduce().

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Iterators for xrange()

Question:

Write a iterator function to work like xrange()

Program:

Explanation:

The iterator protocol consists of two methods. The __iter__() method, which must return the iterator object and the next() method, which returns the next element from a sequence. Python has several built-in objects, which implement the iterator protocol. For example lists, tuples, strings, dictionaries or files.

Output:

 

9
Jul

Find Lines in Multiple Files

Question:

Write a python program to print only those lines in a list of files that has a particular word

Here, I am looking for a word ‘print’ in a list of python files.

Program:

Explanation:

Generator Expressions are generator version of list comprehensions. They look like list comprehensions, but returns a generator back instead of a list.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Trace All Calls To A Function

Question:

Write a program to trace all calls to the below function

Program:
Output:
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here

 

9
Jul

Multiples of 7 But Not 5

Question:

Write a python program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 1500 and 3000 (both included).

Program:

Output:
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Intersection of Two Sets

Question;

With two given lists ([6,5,6,2,2]) and ([9,5,6,7,9,2,7,0]), write a python program to make a list whose elements are intersection of the another list.

Program:

Explanation:

Use set() and “&=” to do set intersection operation.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Python Program to Find Leap Year

Question:

Write a python program to find whether a given year is a leap year or not

Program:

Explanation:

A leap year is a calendar year containing one additional day added to keep the calendar year synchronized with the astronomical or seasonal year. 2016 is a leap year, which means that it has 366 days instead of the usual 365 days that an ordinary year has. An extra day is added in a leap year—February 29 —which is called an intercalary day or a leap day.

Output:

1.

2.
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Filter Even Numbers

Question:
Write a program which can filter even numbers in a list by using filter function. The list is: [11,12,13,14,15,16,17,18,19,20]. Use filter() to filter some elements in a list. Use lambda to define anonymous functions.

Program:

Explanation:

The function filter(function, list) offers an elegant way to filter out all the elements of a list for which the function function returns True.

The function filter(f,l) needs a function f as its first argument. f returns a Boolean value, i.e. either True or False. This function will be applied to every element of the list l. Only if f returns True, the element of the list be included in the result list.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Power of 2

Question:

Display powers of two using anonymous function

Program:

Explanation:

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created.

The general syntax of a lambda function is quite simple:
lambda argument_list: expression

The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Array Manipulation

Question:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.

Note: i=0,1…. X-1; j=0,1…,­Y-1.

Also input is in a comma-separated form.

Program:

Explanation:

Range function generates a list of numbers, which is generally used to iterate over with for loops.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Binary Search

Question:

Write a program to sort the list  [58,6,78,12,333] using binary search and return index position of 12

Program:

Explanation:

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Compress Decompress Strings

Question:

Write a program to compress and decompress the string “Global SQA!Global SQA!Global SQA!Global SQA!Global SQA!Global SQA!”

Use zlib.compress() and zlib.decompress() to compress and decompress a string.

Program:

Explanation:

zlib.compressobj([level[, method[, wbits[, memlevel[, strategy]]]]])

Returns a compression object, to be used for compressing data streams that won’t fit into memory at once.

zlib.decompress(string[wbits[bufsize]])

Decompresses the data in string, returning a string containing the uncompressed data.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Display Calender

Question:
Display calender using in-built functions

Program:

Explanation:

Module calendar is imported and the function month() is used to print the month calendar.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Number of Letters and Digits

Question:
Write a program that accepts a sentence and calculate the number of letters and digits.

Program:

Explanation:

Function isdigit() checks whether the concerned character is a digit, while isalpha checks whether it is an alphabet

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Catch Exception

Question:
Write a function to compute 1 /0 and use try/except to catch the exceptions.

Program:

Explanation:

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal.

try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Get Name from Email Address

Question:

Write python program to print the user name of a given email address.

Program:

Explanation:

Email addresses are in the “[email protected]” format.

Both user names and company names are composed of letters only.

Use \w to match letters.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here