[go: up one dir, main page]

0% found this document useful (0 votes)
2 views40 pages

Unit 1 (PPS)

The document provides an overview of various built-in Python functions, including their descriptions and examples of usage. Functions covered include delattr(), dict(), dir(), divmod(), enumerate(), eval(), exec(), float(), format(), getattr(), hex(), id(), input(), int(), iter(), len(), list(), max(), min(), oct(), pow(), print(), range(), reversed(), round(), set(), setattr(), slice(), sorted(), str(), sum(), tuple(), type(), and zip(). Each function is accompanied by a brief explanation and sample code demonstrating its functionality.

Uploaded by

jesudosss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views40 pages

Unit 1 (PPS)

The document provides an overview of various built-in Python functions, including their descriptions and examples of usage. Functions covered include delattr(), dict(), dir(), divmod(), enumerate(), eval(), exec(), float(), format(), getattr(), hex(), id(), input(), int(), iter(), len(), list(), max(), min(), oct(), pow(), print(), range(), reversed(), round(), set(), setattr(), slice(), sorted(), str(), sum(), tuple(), type(), and zip(). Each function is accompanied by a brief explanation and sample code demonstrating its functionality.

Uploaded by

jesudosss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Unit 1

Python Built in Functions

Function Description
delattr() Deletes the specified attribute (property or method) from the
specified object

class Person:
name = "John"
age = 36
country = "Norway"

delattr(Person, 'age')

# The Person object will no longer contain an "age" property


Python Built in Functions

Function Description
delattr() Deletes the specified attribute (property or method) from the
specified object

Syntax
class Person:
name = "John" delattr(object, attribute)

age = 36
country = "Norway"

delattr(Person, 'age')

# The Person object will no longer contain an "age" property


Python Built in Functions

Function Description
dict() Returns a dictionary (Array)

x = dict(name = "John", age = 36, country = "Norway")

print(x)

OUTPUT:
{'name': 'John', 'age': 36, 'country':
'Norway'}
Python Built in Functions

Function Description
dir() Returns a list of the specified object's properties and methods

Display the content of an object: OUTPUT:


class Person:
['__class__', '__delattr__', '__dict__', '__dir__',
name = "John" '__doc__', '__eq__', '__format__', '__ge__',
age = 36 '__getattribute__', '__gt__', '__hash__', '__init__',
country = "Norway" '__init_subclass__', '__le__', '__lt__', '__module__',

'__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__',
print(dir(Person)) '__subclasshook__', '__weakref__', 'age', 'country',
'name']'age', 'country', 'name']
Python Built in Functions

Function Description
divmod() Returns the quotient and the remainder when argument1 is
divided by argument2

OUTPUT:
x = divmod(5, 2)
(2, 1)
print(x)

Python Built in Functions

Function Description
enumerate() Takes a collection (e.g. a tuple) and returns it as an enumerate
object

Convert a tuple into an enumerate object:


x = ('apple', 'banana', 'cherry')
y = enumerate(x)


OUTPUT:
[(0, 'apple'), (1, 'banana'), (2,
'cherry')]
Python Built in Functions

Function Description
enumerate() Takes a collection (e.g. a tuple) and returns it as an enumerate
object

Convert a tuple into an enumerate object:


x = ('apple', 'banana', 'cherry')
y = enumerate(x)


OUTPUT:
[(0, 'apple'), (1, 'banana'), (2,
'cherry')]
Python Built in Functions

Function Description
eval() Evaluates and executes an expression

Execute a block of code:


x = 'name = "John"\nprint(name)'
exec(x)

OUTPUT:
John
Python Built in Functions

Function Description
exec() Executes the specified code (or object)

Evaluate the expression 'print(55)':


x = 'print(55)'
eval(x)

OUTPUT:
55
Python Built in Functions

Function Description
exec() Executes the specified code (or object)

Evaluate the expression 'print(55)':


x = 'print(55)'
eval(x)

OUTPUT:
55
Python Built in Functions

Function Description
exec() Executes the specified code (or object)

ages = [5, 12, 17, 18, 24, 32]

def myFunc(x): OUTPUT:


if x < 18:
return False 18
else: 24
return True 32


adults = filter(myFunc, ages)

for x in adults:
print(x)
Python Built in Functions

Function Description
float() Returns a floating point number

x = float(3)

print(x)


OUTPUT:
3.0
Python Built in Functions

Function Description
format() Formats a specified value

x = format(0.5, '%')

print(x)


OUTPUT:
50.000000%
Python Built in Functions

Function Description
getattr() Returns the value of the specified attribute (property or method)

class Person: OUTPUT:


name = "John"
36
age = 36

country = "Norway" ​

x = getattr(Person, 'age')

print(x)
Python Built in Functions

Function Description
hex() Converts a number into a hexadecimal value

x = hex(255) OUTPUT:

print(x) 0xff


Python Built in Functions

Function Description
id() Returns the id of an object

x = ('apple', 'banana', OUTPUT:


'cherry')
81084511
y = id(x)

print(y) ​

# This value is the memory


address of the object and will
be different every time you
Python Built in Functions

Function Description
input() Allowing user input

print("Enter your name:") OUTPUT:

x = input() Enter your name:


saint
print("Hello, " + x) Hello, saint

Python Built in Functions

Function Description
int() Returns an integer number

x = int(3.5) OUTPUT:

print(x) 3


Python Built in Functions

Function Description
iter() Returns an iterator object

x = iter(["apple", "banana", "cherry"]) OUTPUT:

print(next(x)) apple
banana
print(next(x)) cherry
print(next(x)) ​
Python Built in Functions

Function Description
len() Returns the length of an object

mylist = ["apple", "orange", "cherry"] OUTPUT:

x = len(mylist) 3
print(x)

Python Built in Functions

Function Description
list() Returns a list

x = list(('apple', 'banana', 'cherry')) OUTPUT:

print(x)
['apple', 'banana', 'cherry‘]


Python Built in Functions

Function Description
max() Returns the largest item in an iterable

x = max(5, 10) OUTPUT:


print(x) 10

Function Description
min() Returns the smallest item in an iterable

x = min(5, 10) OUTPUT:


print(x) 5
Python Built in Functions

Function Description
oct() Converts a number into an octal

x = oct(12) OUTPUT:

print(x) 0o14


Python Built in Functions

Function Description
pow() Returns the value of x to the power of y

x = pow(4, 3) OUTPUT:

print(x) 64


Python Built in Functions

Function Description
print() Prints to the standard output device

OUTPUT:

print("Hello World") Hello World


Python Built in Functions

Function Description
print() Prints to the standard output device

OUTPUT:

print("Hello World") Hello World


Python Built in Functions

Function Description
range() Returns a sequence of numbers, starting from 0 and increments by
1 (by default)

OUTPUT:

x = range(6) 0
1
for n in x: 2
3
print(n)

4
5
Python Built in Functions

Function Description
reversed() Returns a reversed iterator

OUTPUT:

alph = ["a", "b", "c", "d"] d


c
ralph = reversed(alph) b
a
for x in ralph:

print(x)
Python Built in Functions

Function Description
round() Rounds a numbers

OUTPUT:

x = round(5.76543, 2) 5.77
print(x)


Python Built in Functions

Function Description
round() Rounds a numbers

OUTPUT:

x = round(5.76543, 2) 5.77
print(x)


Python Built in Functions
Function Description
set() Returns a new set object

x = set(("apple", "banana", "cherry"))

print(x) OUTPUT:

# Note: the set list is unordered, so the result {'banana', 'cherry', 'apple'}

will display the items in a random order.



Python Built in Functions
Function Description
setattr() Sets an attribute (property/method) of an object
class Person:

name = "John"
OUTPUT:
age = 36
40
country = "Norway"

setattr(Person, 'age', 40)


# The age property will now have the value: 40

x = getattr(Person, 'age')

print(x)
Python Built in Functions

Function Description
slice() Returns a slice object

OUTPUT:
a = ("a", "b", "c", "d", "e", "f", "g", "h")
(‘a’, ‘b’)
x = slice(2)

print(a[x]) ​
Python Built in Functions

Function Description
sorted() Returns a sorted list

OUTPUT:
a = ("b", "g", "a", "d", "f", "c", "h", "e")
x = sorted(a) ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(x)


Python Built in Functions

Function Description
str() Returns a string object

OUTPUT:
x = str(3.5)
3.5
print(x)


Python Built in Functions

Function Description
sum() Sums the items of an iterator

OUTPUT:
a = (1, 2, 3, 4, 5)
x = sum(a) 15
print(x)


Python Built in Functions

Function Description
tuple() Returns a tuple

OUTPUT:
x = tuple(("apple", "banana", "cherry"))
('banana', 'cherry', 'apple')
print(x)


Python Built in Functions

Function Description
type() Returns the type of an object

a = ('apple', 'banana', 'cherry')


b = "Hello World" OUTPUT:
c = 33 <class 'tuple'>
x = type(a) <class 'str'>
y = type(b) <class 'int'>
z = type(c) ​

print(x)
print(y)
print(z)
Python Built in Functions

Function Description
zip() Returns an iterator, from two or more iterators

a = ("John", "Charles", "Mike")


b = ("Jenny", "Christy", "Monica")
x = zip(a, b)

#use the tuple() function to display a


readable version of the result:​

print(tuple(x)) OUTPUT:

(('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'))

You might also like