Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Built in Functions

Python built-in functions return value: true or false provides examples of common Python built-in functions like abs(), bool(), callable(), chr(), compile(), divmod(), enumerate(), eval(), filter(), format(), hex(), input(), int(), iter(), len(), list(), map(), max(), min(), next(), oct(), open(), ord(), pow(), print(), range(), round(), sorted(), str(), sum(), tuple(), type(), zip(), and provides brief descriptions and syntax examples for each.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Built in Functions

Python built-in functions return value: true or false provides examples of common Python built-in functions like abs(), bool(), callable(), chr(), compile(), divmod(), enumerate(), eval(), filter(), format(), hex(), input(), int(), iter(), len(), list(), map(), max(), min(), next(), oct(), open(), ord(), pow(), print(), range(), round(), sorted(), str(), sum(), tuple(), type(), zip(), and provides brief descriptions and syntax examples for each.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python built-in functions return value: true or false

*abs() - returns absolute value of a number


x=5
syntax: abs(num)
num is either an integer or float print(callable())
return value: absolute value
def test():
print(“test”)
x=int(input(“enter a number: ”) y=5
absval=abs(x) print(callable(y))
print(“the absval of”, x, “is” absval)
*chr() - Returns a Character (a string) from an
Integer
*bin() - converts integer to binary string
ASCII – American Standard Code for Information
syntax: bin(int)
Interchange
return value: a binary string
chr(97)
‘a’
x=int(input(“enter a number: ”)
binary=bin(x)
*compile() - Returns a Python code object
print(“the absval of”, x, “is” binary)
syntax: compile(source, filename, mode, flags,
don’t_inherit, optimize)

*bool() - converts a Value to Boolean source, filename, mode, flags,


don’t_inherit, optimize - parameters
syntax: bool([value])
source – normal string where you can find your
return value: true, false, none Python code
if 0 then false filename - file from where the code is read
any number not zero then, true
mode: eval - accepts only a single expression
x=int
exec - accepts a code block
bool(x)
single – contains a single inheritance
True or False statement
flags: (optional) controls which future statements
affects the sources
*float() – returns a floating number
- default value = 0
syntax: float(x)
don’t_inherit - optional
float(“32”)
- controls the compilation of the
source

optimize (optional) - optimization of the compiler

code=’a=5\nb=6\nsum=a+b\nprint(“sum=”,
*callable() - Checks if the Object is Callable; sum)’
returns true if the one being called can be called
codecompile=compile(code, ‘file1’, ‘exec’) print(eval(“x+1”))

exec(codecompile)
*exec() – executing a string or a code
sum = 11
syntax: exec()
code=”a=5\nb=6\nprint(a+b)”
*divmod() - Returns a Tuple of Quotient and
Remainder exec(code)

syntax: divmod(x,y)
x and y – integer/float *filter() – filters a given iterable/sequence using a
x – numerator defined function to test if each element of your
y – denominator iterable is true
syntax: filter(function,iterable)
return value: quotient, remainder
x=int(input(“enter a num:”))
students=[“Matthew”, “Maryrose”,
y=int(input(“enter a num:”)) “Kate”]
dm=divmod(x,y) def late(students):
print(dm) late=[“Matthew”]
if (student in late):
*enumerate() – adds a counter to an return True
iterable/sequence
else:
syntax: enumerate(iterable, start=0)
return False
place=[“Venice”, “Germany”, “Manila”]
filteredStudents=filter(late,
a=enumerate(places) students)
print(list(a)) print(“The late student is: ”)
for late in filteredStudents:
place=[“Venice”, “Germany”, “Manila”] print(late)
a=enumerate(places, start=1)
print(list(a))

place=[“Venice”, “Germany”, “Manila”]


for a in enumerate(places):
print(a)

*format() – formats/reformats a given value


*eval() – passing an expression and runs the code syntax: format(value, format space)
syntax: eval(expression) [:fill, assign, sign, width, [,], precision,
x=1 type]
fill – any character that you want to fit the space hex 10
width
‘0xa’
align - > - right align all the values
*input(prompt) – used to get an input from the
< - left align all the values
user
^ - center align all the values
= - forces all the numbers or values to the *int() – converts a number/string into an int
right
*iter() – returns an element of a given sequence
sign - + - forces the sign to show syntax: iter(sequence)
- - show only if number is negative
leave blank “ ” next()
width - specifies how many place values letters=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]
- minimum width a value takes
1_iter=iter(letters)
, - thousands separator print(next(1_iter))
precision – precision for floating numbers print(next(1_iter))
type – specifies the type of number print(next(1_iter))
print(next(1_iter))
12 types:
d – Decimal for integer *len(seq) – returns the number of items in an
c – corresponding unicode character iterable/sequence
o – octal
x – Hex format in lowercase seq - str
X – Hex format in upper case - tuple
e – exponential notation in lowercase - list
E – Exponential notation in uppercase - range
f – floating numbers
g – general format of numbers len(“string”)
G – general format “E”
% - multiplies the number by 100 and adds 6
the percent symbol
len(“range(9))”
format (123.125, “#<+10,.2f” 9
format (8811, “#>20,d”)
*list() – creates a list in python range
format (8811, “#=20,d”)
format (8811, “#<20,d”) list(“string”)
format (25, b)
[‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’]
*map() – applies a given function to each elemnt
of a sequence

*hex() – returns hex string of a number (int) return value – list of results
syntax: syntax:
hex(int)
“0x” map(fxn, seq 1, seq 2)
hex(15) def square (n):
‘0xf’
“””This function squares a *pow() – power – returns x raised to the power of
number””” y
return n*n syntax: pow(x, y)
number=[2, 3, 4, 5, 6] pow(x, y, z)
result=map(square, number) - (xy) ÷ z
print (list(result)) pow(3, 2)
9
*max() – returns the largest element from a pow(3, 2, 2)
sequence
1
syntax: max(sequence)

*print() – to display texts or result of


max (12, 45, 7, 3, 5, 81, 1, 4) computations
81 print()
num=[1, 2, 3] print(” ”)
max(num) var = int/float/list/string/tuple
9
*min() – smallest *range() – returns the sequence of numbers
between integers specified
syntax: min(sequence)
return value:
range(7)
*next() – return the next element in your sequence
0-6
a=[1, 2, 3, 4, 5]
range(start value, stop value)
iterator=iter (a)
range(3, 7)
print (next(iterator, “no more”))
print (next(iterator, “no more”)) range(start value, stop value, interval)
print (next(iterator, “no more”))
range(2, 10, 3)
print (next(iterator, “no more”))cee
print (next(iterator, “no more”))
print (next(iterator, “no more”))
print (next(iterator, “no more”)) *reversed() – reversed iterator of a sequence

*oct() – return the octal equivalent of a number syntax: reversed(x) sequence


return: a = “Hello, Ilaya!” – can be a = [1, 2, 3, 4, 5] or a
“0x” = range(2, 26)
oct(8)
‘010’ print(list(reversed(a)))

*ord() – returns the int equivalent of a character


passed as a parameter based or ASCII *round() – returns float of number rounded off
ord(“A”) syntax: round(num, significant – digits)
65
round(10)
10.0
round(110.4) *zip() – takes sequences and makes iterator that
collect or zips elements based on the sequences
110.0
num = [1, 2, 3]
round(8.225, 2) - 2 is the decimal places
string = [“one”, “two”, “three”]
8.26
result = zip(num, string)
*set() – counters a python set
print(list(result))
list1=[1, 2, 4, 5, 5, 6, 67, 4, 8, 9]
print(set(list))
set([1, 2, 67, 4, 5, 6, 8, 9])

*slice() – cuts parts of an object; makes use of


parameters similar to range
slice(start, stop, interval) – optional
a = “Python”
index = slice(3) - slice(1, 3) – slice(1, 5,
2)
print(a[index])

*sorted() – returns a sorted list


a = [“a”, ”s”, ”e”, ”w”, ”q”, ”r”]
print(sorted(a))
sorted(list, reverse = true/false)
print(sorted(a, reverse= true))

*sum()- adds element of a sequence and returns


sum
a = [45, 24, 6, 57, 4, 5, 656, 1]
a_sum = sum(a)
print(a_sum)
sum(a, initial value)
a is either a list or a sequence
*tuple() – creates a tuple in python
tuple = () and str – immutable
list = []
set = {}

You might also like