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.

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

LCM of Two Numbers

Question:

Program to find the LCM of two numbers

Program:

Explanation:

A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, …. The least common multiple(LCM) of two numbers is the smallest number (not zero) that is a multiple of both.

Output:

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

Checkout more Python Programs

Click here
9
Jul

The Palindrome Program

Question 

Write a python program to check whether given string is palindrome or not without using any inbuilt function to reverse the string

Program:

Explanation:

A palindrome is a string which is same read forward or backwards. Dad is a palindrome while Python is not.

In the program, we first accept the string to ‘string’. Since capital letters and small letters are perceived differently because of difference in ASCII codes, we convert the string to all lower for uniformity using inbuilt function ‘x.lower()’ where x is the string

In the above program, we have used the slice operator to reverese the string.The slice operator[n:m:p] returns the part of the string from nth to the (m-1)th character Setting p to -1 returns the reverse order of the slice of string you asked for. Since here we want the whole string, we use the shorthand [::-1]

Compare both the string and it’s reversal. If they are equal, then it’s a palindrome, otherwise not.

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

Python Armstrong program

Question 2

Write a Python program to check whether a given number is armstrong or not.

Program:

Explanation:

Armstrong number is the number whose sum of cube of individual digits is equal to the number itself.

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

The Fibonacci Sequence

Question:

Program to print the Fibonacci Number

Program:

Explanation:

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms

Ex: 5th term is sum of 4th and 3rd term;3=2+1

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

Multiplication Table

Question;

Write a program to print the multiplication table of a number

Program:

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

Checkout more Python Programs

Click here