 # Python functions - Exercises, Practice, Solution

## Python functions [21 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python function to find the Max of three numbers. Go to the editor
Click me to see the sample solution

2. Write a Python function to sum all the numbers in a list. Go to the editor
Sample List : (8, 2, 3, 0, 7)
Expected Output : 20
Click me to see the sample solution

3. Write a Python function to multiply all the numbers in a list. Go to the editor
Sample List : (8, 2, 3, -1, 7)
Expected Output : -336
Click me to see the sample solution

4. Write a Python program to reverse a string. Go to the editor
Sample String : "1234abcd"
Expected Output : "dcba4321"
Click me to see the sample solution

5. Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. Go to the editor
Click me to see the sample solution

6. Write a Python function to check whether a number falls in a given range. Go to the editor
Click me to see the sample solution

7. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Go to the editor
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
Click me to see the sample solution

8. Write a Python function that takes a list and returns a new list with unique elements of the first list. Go to the editor
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
Click me to see the sample solution

9. Write a Python function that takes a number as a parameter and check the number is prime or not. Go to the editor
Note : A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself.
Click me to see the sample solution

10. Write a Python program to print the even numbers from a given list. Go to the editor
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Result
: [2, 4, 6, 8]
Click me to see the sample solution

11. Write a Python function to check whether a number is perfect or not. Go to the editor
According to Wikipedia : In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.
Click me to see the sample solution

12. Write a Python function that checks whether a passed string is palindrome or not. Go to the editor
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Click me to see the sample solution

13. Write a Python function that prints out the first n rows of Pascal's triangle. Go to the editor
Note : Pascal's triangle is an arithmetic and geometric figure first imagined by Blaise Pascal.

Sample Pascal's triangle : Each number is the two numbers above it added together Go to the editor
Click me to see the sample solution

14. Write a Python function to check whether a string is a pangram or not. Go to the editor
Note : Pangrams are words or sentences containing every letter of the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"
Click me to see the sample solution

15. Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically. Go to the editor
Sample Items : green-red-yellow-black-white
Expected Result
: black-green-red-white-yellow
Click me to see the sample solution

16. Write a Python function to create and print a list where the values are square of numbers between 1 and 30 (both included). Go to the editor
Click me to see the sample solution

17. Write a Python program to make a chain of function decorators (bold, italic, underline etc.) in Python. Go to the editor
Click me to see the sample solution

18. Write a Python program to execute a string containing Python code. Go to the editor
Click me to see the sample solution

19. Write a Python program to access a function inside a function. Go to the editor
Click me to see the sample solution

20. Write a Python program to detect the number of local variables declared in a function. Go to the editor
Sample Output:
3
Click me to see the sample solution

21. Write a Python program that invoke a given function after specific milliseconds. Go to the editor
Sample Output:
Square root after specific miliseconds:
4.0
10.0
158.42979517754858
Click me to see the sample solution

## Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

## Python: Tips of the Day

Understanding slice notation:

It's pretty simple really:

```a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array
```

There is also the step value, which can be used with any of the above:

```a[start:stop:step] # start through not past stop, by step
```

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

```a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items
```

Similarly, step may be a negative number:

```a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed
```

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relation to slice() object

The slicing operator [] is actually being used in the above code with a slice() object using the : notation (which is only valid within []), i.e.:

```a[start:stop:step]
```

is equivalent to:

```a[slice(start, stop, step)]
```

Slice objects also behave slightly differently depending on the number of arguments, similarly to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the : -based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Ref: https://bit.ly/2MHaTp7