Page 454 - CITS - Computer Software Application -TT
P. 454
COMPUTER SOFTWARE APPLICATION - CITS
Built in Functions in Python
Certainly! Python comes with a wide range of built-in functions that are readily available for use without needing
to import any modules. These functions serve various purposes and are foundational to programming in Python.
Here are some of the most commonly used built-in functions in Python:
1 print(): Used to display the output to the standard output device (usually the console).
2 input(): Reads input from the user through the standard input device (usually the keyboard).
3 len(): Returns the length (the number of items) of an object. Works with sequences such as strings, lists,
tuples, etc.
4 type(): Returns the type of an object.
5 int(), float(), str(), list(), tuple(), dict(), set(): These functions are used for type conversion. They convert the
given value to the specified data type.
6 range(): Generates a sequence of numbers. It is often used with loops to iterate a certain number of times.
7 sum(): Returns the sum of all elements in a iterable (such as a list).
8 max(), min(): Returns the maximum or minimum value from a sequence or set of values.
9 abs(): Returns the absolute value of a number.
10 round(): Rounds a floating-point number to a specified number of decimal places.
11 sorted(): Returns a new sorted list from the elements of any iterable.
12 enumerate(): Returns an enumerate object that yields pairs of indexes and elements, useful for obtaining an
index along with each element from an iterable.
13 zip(): Combines elements from multiple iterables into tuples.
14 map(): Applies a function to all the items in an input list or any other iterable.
15 filter(): Constructs an iterator from elements of an iterable for which a function returns true.
16 all(): Returns True if all elements of an iterable are true (or if the iterable is empty).
17 any(): Returns True if any element of an iterable is true. If the iterable is empty, returns False.
18 eval(): Evaluates a Python expression passed as a string.
19 format(): Formats a specified value into a specified format.
20 getattr(), setattr(), delattr(): Used to get, set, or delete an attribute from an object by name.
These are just a few examples of the many built-in functions available in Python. They provide a solid foundation
for performing various operations and manipulations in Python programming.
File handling in Python
File handling in Python refers to the process of working with files on a computer’s filesystem. Python provides
several built-in functions and modules to facilitate file handling operations. Here’s an overview of the basic file
handling operations in Python:
• 1. Opening a File: To open a file in Python, you use the ‘open()’ function, which returns a file object. The
‘open()’ function requires at least one argument, which is the path to the file you want to open. Optionally, you
can specify the mode in which you want to open the file (read, write, append, etc.).
• 1. Reading from a File: Once you have opened a file, you can read its contents using various methods such
as’ read()’, ‘readline()’, or ‘readlines()’.
441
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137