Python provides you with a great variety of built-in functions and modules. We wil display a bit of interesting examples here, using some built-in functions.
def biggest_number(*args):
return max(*args)
As you noticed, we can pass multiple inputs to our function using *args syntax.
# output: 6
print max(4,2,6)
# output: 3
print min(3,6,7)
# output: 98
print abs(-98)
# output: displays the types of variables: int, str, float
x_int = 52
x_string = "hello"
x_float = 2.436
# output: print type(x_string)
# output: print type(x_float)
# output: print type(x_int)
# verify the input and increment it if numeric
def increment(number):
if type(number) == int or type(number) == float:
return number + 1
else:
return False
def increment_input(input):
result = increment(input)
if result:
print "The result is : %s" % result
else:
print "Sorry, the input you provided is not a number"
# output: Sorry, the input you provided is not a number
input1 = raw_input("Enter a string: ")
increment_input(input1)
# output: the number provided + 1
input2 = raw_input("Enter a number: ")
input2 = int(input2)
increment_input(input2)
# calculating average with the provided list of numbers
# using pre-built function sum
def average(numbers):
total = float(sum(numbers))
total = total / len(numbers)
return total
print average([2,4,9])
from random import randint
random_number = randint(0, 12)
# output: a random number between 0 and 12
print rand_number
### Sum of digits in a number
def digit_sum(n):
total = 0
while n != 0:
number = n % 10
n = int(n / 10)
total = total + number
return total
# output: 10 = 1+2+3+4
print digit_sum(1234)
def factorial(x):
total = 1
for number in range(1, x + 1):
total = total * number
return total
# output: 24 = 4! = 4 * 3 * 2 * 1
print factorial(4)
def is_prime(x):
x = abs(x)
for number in range(0, x):
# skipping the numbers 0 and 1
if abs(number) == 0 or abs(number) == 1:
continue
# if number can be divided by any other number then it's not a prime number
if x % number == 0:
return False
return True
# output: True
print is_prime(13)
# output: False
print is_prime(12)
# output: True
print is_prime(-11)
def reverse(word):
reversed_word = ""
for character in word:
reversed_word = character + reversed_word
return reversed_word
# output: emesrever
print reverse('reverseme')
This function will remove the vowel in a string and return a function without vowels.
def anti_vowel(word):
word_without_vowels = ""
for character in word:
if character.lower() in ['a', 'e', 'i', 'o', 'u']:
continue
word_without_vowels = word_without_vowels + character
return word_without_vowels
# output: "Hr y g, dr!"
print anti_vowel("Here you go, dear!")
### Counting number of item occurences in the list
def count(sequence, item):
total = 0
for search_item in sequence:
if item == search_item:
total = total + 1
return total
# output: 3 (number 2 occurs 3 times in the list)
print count([1,2,3,2,3,2], 2)
### Removing odd numbers from the list
def purify(list):
new_list = []
for item in list:
if item % 2 == 0:
new_list.append(item)
return new_list
# output: [2] - odd numbers will be removed
print purify([1,2,3])
### Multiplying numbers in the list
def product(numbers):
total = 1
for number in numbers:
total = total * number
return total
# output: 6 = 1 * 2 * 3
print product([1,2,3])
### Removing duplicates from the list
def remove_duplicates(list):
clean_list = []
for item in list:
if item in clean_list:
continue
clean_list.append(item)
return clean_list
# output: [1,3,4]
print remove_duplicates([1,3,3,4])
### Calculating median in the list of numbers
# even number of elements in the list
# taking an average of the middle elements
# otherwise, taking the middle element
def median(numbers):
numbers = sorted(numbers)
if len(numbers) == 0:
return 0
if len(numbers) % 2 == 0:
middle = len(numbers) / 2 - 1;
return float(numbers[middle] + numbers[middle + 1]) / 2
else:
middle = (len(numbers) - 1) / 2;
return numbers[middle]
# output: 0 - median of empty list
print median([])
# output: 5.5
print median([1,2,5,6,7,8])
# output: 3
print median([1,2,3,4,5])
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def print_grades(grades):
for grade in grades:
print grade
def grades_sum(grades):
total = 0
for grade in grades:
total = total + grade
return total
def grades_average(grades):
return grades_sum(grades) / float(len(grades))
def grades_variance(grades):
average = grades_average(grades)
variance = 0
for grade in grades:
variance = variance + (average - grade) ** 2
variance = variance / len(grades)
return variance
def grades_std_deviation(variance):
return variance ** 0.5
# output: the grades list, each grade on a single line
print_grades(grades)
# output: 80.4230769231
print 'Average Grade: %s' % grades_average(grades)
# output: 334.071005917
variance = grades_variance(grades)
print 'Variance: %s' % variance
# output: 18.2776094147
print 'Standard Deviation: %s' % grades_std_deviation(variance)