Python Programs

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
9
Jul

Assertion Error

Question:

Raise Assertion error when trying to verify that every number in the list [2,4,6,8,1] is even.

Use “assert expression” to make assertion

Program:

Explanation:

The Assert Statement: When python encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception. 

Output:

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

Checkout more Python Programs

Click here
9
Jul

Square a List Without Loops

Question:

Write a program to print the squares of the numbers in a list [1,2,3,4,5] without using any iterative function.,

Program:

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

Checkout more Python Programs

Click here
9
Jul

zip() and izip()

Question:

Write a python program to show the difference in zip() and izip() function

Explanation:

zip() is a standard library which combines elements into tuples.

The izip() function works the same way, but it returns an iterable object for an increase in performance by reducing memory usage.

We can directly print the list returned by zip() but, we cannot directly print an iterable object. We need to use next(iterable_object) or a loop.

Lists are stored in memory so they have the advantage of faster access times over Iterable Object whose contents are not entirely stored in memory but fetched on-demand instead.

Program:

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

Checkout more Python Programs

Click here
9
Jul

Solve Quadratic Equation

Question:

Write a python program to solve a quadratic equation

Program:

Explanation:

Quadratic Equations is a equation having form like ax2 + bx+c =0. Roots are of the form  x = (-b +/-√(b2 – 4ac))/2a. To know more about Quadratic Equations, click here.

Output:

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

Checkout more Python Programs

Click here
9
Jul

Convert Fahrenheit to Celsius

Question:

Write a python program to convert Fahrenheit to Celsius

Program:

Explanation:

To convert the temperature from Fahrenheit to Celsius, deduct 32, then multiply by 5, then divide by 9.

Output:

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

Checkout more Python Programs

Click here
9
Jul

Convert KPH to MPH

Question:

Write a python program to convert KPH to MPH

Program:

Explanation:

To convert KPH to MPH, just multiply by 0.6214

Output:

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

Checkout more Python Programs

Click here
9
Jul

Remove Punctuation From String

Question:

Write a python program to remove punctuations from a string

Program:

Explanation:

There are fourteen punctuation marks commonly used in English grammar. They are the period, question mark, exclamation point, comma, semicolon, colon, dash, hyphen, parentheses, brackets, braces, apostrophe, quotation marks, and ellipsis.

Output:

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

Checkout more Python Programs

Click here
9
Jul

Addition of Two Matrices

Question:

Write a python program to add two matrices

Program:

Explanation:

If A and B are the two matrices for addition, both of them must be of the same order.

If i and j are the index positions, then result[i][j] = A[i][j] + B[i][j]

Output:

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

Checkout more Python Programs

Click here
9
Jul

HCF Python Program

Question:

Write a python program to find the HCF

Program:

Explanation:

The Highest Common Factor (H.C.F) of two (or more) numbers is the largest number that divides evenly into both numbers. In other words the H.C.F is the largest of all the common factors. The common factors or of 12 and 18 are 1, 2, 3 and 6. The largest common factor is 6, so this is the H.C.F. of 12 and 18.

Output:

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

Checkout more Python Programs

Click here
9
Jul

Factorial Using Recursion

Question :
Write a program to find the factorial of a number using recursive function

Program:

Explanation:
Factorial of a number is the product of all natural numbers till that number. It does not exist for negative numbers.

For example, the factorial of 4 would be 1*2*3*4=24

A recursive function is a function that calls on itself. In the above recursive function, the function calls on itself with n-1 as argument till n becomes 1 and 1 is returned. Each time the function is called, product the corresponding n and the factorial of n-1 is returned. Hence, factorial is found.

Output:
1.

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

Checkout more Python Programs

Click here
9
Jul

Factors of a Number

Question:

Write a python program to find the factors of a given number

Program:

Explanation:

The factors of a number are any numbers that divide into it exactly. This includes 1 and the number itself. For example, the factors of 6 are 1, 2, 3 and 6. The factors of 8 are 1, 2, 4 and 8. For larger numbers it is sometimes easier to ‘pair’ the factors by writing them as multiplications.

Output:

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

Checkout more Python Programs

Click here
9
Jul

Largest of Three Numbers

Question

Write a python program to find the largest of three numbers

Hint: Use if and elif

Program:

 

Explanation:

Three numbers are accepted from the user ‘nos1′,’nos2′,’nos3’.

First ‘nos1’ is checked.If it is greater than both ‘nos2’ and ‘nos3’, then ‘large’ is allocated wth the value of ‘nos1’

Otherwise ‘nos2’ is checked. If it is greater than both ‘nos1’ and ‘nos2’,then ‘large’ is allocated with the value of ‘nos2’

Otherwise ‘large’ is allocated with ‘nos3’

Output:

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

Checkout more Python Programs

Click here