Functions Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Method Description

Python abs() returns absolute value of a number


returns true when all elements in
Python all()
iterable is true
Checks if any Element of an Iterable is
Python any()
True
Returns String Containing Printable
Python ascii()
Representation
Python bin() converts integer to binary string
Python bool() Converts a Value to Boolean
Python bytearray() returns array of given byte size
Python bytes() returns immutable bytes object
Python callable() Checks if the Object is Callable
Returns a Character (a string) from an
Python chr()
Integer
Python returns class method for given
classmethod() function
Python compile() Returns a Python code object
Python complex() Creates a Complex Number
Python delattr() Deletes Attribute From the Object
Python dict() Creates a Dictionary
Python dir() Tries to Return Attributes of Object
Returns a Tuple of Quotient and
Python divmod()
Remainder
Python enumerate() Returns an Enumerate Object
Python eval() Runs Python Code Within Program
Executes Dynamically Created
Python exec()
Program
constructs iterator from elements
Python filter()
which are true
returns floating point number from
Python float()
number, string
Method Description
returns formatted representation of a
Python format()
value
Python frozenset() returns immutable frozenset object
returns value of named attribute of an
Python getattr()
object
returns dictionary of current global
Python globals()
symbol table
returns whether object has named
Python hasattr()
attribute
Python hash() returns hash value of an object
Python help() Invokes the built-in Help System
Python hex() Converts to Integer to Hexadecimal
Python id() Returns Identify of an Object
Python input() reads and returns a line of string
returns integer from a number or
Python int()
string
Checks if a Object is an Instance of
Python isinstance()
Class
Checks if a Object is Subclass of a
Python issubclass()
Class
Python iter() returns iterator for an object
Python len() Returns Length of an Object
Python list()
creates list in Python
Function
Returns dictionary of a current local
Python locals()
symbol table
Python map() Applies Function and Returns a List
Python max() returns largest element
Python
returns memory view of an argument
memoryview()
Python min() returns smallest element
Method Description
Python next() Retrieves Next Element from Iterator
Python object() Creates a Featureless Object
Python oct() converts integer to octal
Python open() Returns a File object
returns Unicode code point for
Python ord()
Unicode character
Python pow() returns x to the power of y
Python print() Prints the Given Object
Python property() returns a property attribute
return sequence of integers between
Python range()
start and stop
returns printable representation of an
Python repr()
object
returns reversed iterator of a
Python reversed()
sequence
rounds a floating point number to
Python round()
ndigits places.
Python set() returns a Python set
Python setattr() sets value of an attribute of object
creates a slice object specified by
Python slice()
range()
returns sorted list from a given
Python sorted()
iterable
Python
creates static method from a function
staticmethod()
returns informal representation of an
Python str()
object
Python sum() Add items of an Iterable
Allow you to Refer Parent Class by
Python super()
super
Python tuple() Creates a Tuple
Method Description
Function
Python type() Returns Type of an Object
Python vars() Returns __dict__ attribute of a class
Python zip() Returns an Iterator of Tuples
Python __import__() Advanced Function Called by import
1) Simple Calculator by Making Functions
# Program make a simple calculator that can add,
subtract, multiply and divide using functions

# This function adds two numbers


def add(x, y):
return x + y

# This function subtracts two numbers


def subtract(x, y):
return x - y

# This function multiplies two numbers


def multiply(x, y):
return x * y

# This function divides two numbers


def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user


choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))

if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':


print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':


print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':


print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
2)Python Program To Display Powers of 2 Using
Anonymous Function
# Python Program to display the powers of 2 using
anonymous function

# Change this value for a different result


terms = 10

# Uncomment to take number of terms from user


#terms = int(input("How many terms? "))

# use anonymous function


result = list(map(lambda x: 2 ** x, range(terms)))

# display the result

print("The total terms is:",terms)


for i in range(terms):
print("2 raised to power",i,"is",result[i])
3) Python Program to Solve Quadratic Equation

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module

import cmath
a=1

b=5

c=6

# To take coefficient input from the users

# a = float(input('Enter a: '))

# b = float(input('Enter b: '))

# c = float(input('Enter c: '))

# calculate the discriminant

d = (b**2) - (4*a*c)

# find two solutions

sol1 = (-b-cmath.sqrt(d))/(2*a)

sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

4)# Python Program to display the powers of 2 using


anonymous function
# Change this value for a different result

terms = 10

# Uncomment to take number of terms from user

#terms = int(input("How many terms? "))

# use anonymous function

result = list(map(lambda x: 2 ** x, range(terms)))

# display the result

print("The total terms is:",terms)

for i in range(terms):

print("2 raised to power",i,"is",result[i])

(5)Using recursion to generate a list of X


random numbers between 0 and 100, sorted
from low to high in Python?
# import the random module
import random

print(random.randint(0,9))
or

import random

# determines how many values

# will be printed

for x in range(10):

# print 10 random values

# between 1 and 100

print (random.randint(1, 101))

You might also like