Saturday, August 12, 2017

Python-Bootcamp: Functions and Methods

Study Today on 8/12/2017 (Sat)

Write a function that computes the volume of a sphere given its radius.

def vol(rad):
    return (4.0/3)*(3.14)*(rad**3)


Write a function that checks whether a number is in a given range (Inclusive of high and low)

def ran_check(num,low,high):
    #Check if num is between low and high (including low and high)
    if num in range(low,high+1):
        print " %s is in the range" %str(num)
    else :
        print "The number is outside the range."



Write a Python function that checks whether a passed string is palindrome or not.

Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

def palindrome(s):
    s = s.replace(' ','') # This replaces all spaces " " with no space ''. (Fixes issues with strings that have spaces)
    return s == s[::-1] # Check through slicing



Write a Python function to check whether a string is pangram or not.

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"
Hint: Look at the string module

import stringd

def ispangram(str1, alphabet=string.ascii_lowercase):
    alphaset = set(alphabet)
    return alphaset <= set(str1.lower())