Information Technology Tips

Innotechtips_logo_v4
Menu
  • Home
  • Linux
  • Programming
  • Other Tech Tips
    • Windows
    • Security
Youtube
Home Programming

Exploring Built-in Functions in Python

Inno by Inno
July 27, 2023
Exploring Built-in Functions in Python

Exploring Built-in Functions in Python

The Python interpreter has a number of functions and types built into it that are always available. These do not require an import statement. The Python built-in functions are always available to you in Python, in any app you are creating, as well as at the Python command prompt. These built-in functions are also part of the standard library.

In fact, if you do a web search for Python built-in functions, some of the search results will point directly to the Python documentation. On that page, you can click the name of any function to learn more about it. Examples of built-in functions:

The print() function

You can use it to display formatted messages on the screen. This is among the first functions encountered by most beginners of the Python programming language.

Example:

>>> print('Hi there')

Hi there

>>>

The len() function

The len() function returns the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Example:

>>> y = 'Hello'

>>> print(len(y))

5

>>>

The list() function

The list() function is used to create an empty list.

Example:

>>> mylist = list()

>>> print(mylist)

[]

>>>

The type([object]) function

The type() function in Python usually identifies the type of a piece of data. In this case, the data could be an item such as an integer, string, list, and dictionary. The below examples were run in Python’s IDLE (Integrated Development and Learning Environment).

Example 1:

>>> x = 3

>>> print(type(x))

<class 'int'>

>>>

The above output tells us that x is an integer and an instance of the int class from the standard library.

Example 2:

>>> y = 'Hello'

>>> print(type(y))

<class 'str'>

>>>

Here, y contains data that is of the type string (or str object or str instance), created by the Python str class.

Example 3:

>>> mylist = list()

>>> type(mylist)

<class 'list'>

>>>

The dir() function

The dir() function displays a list of all the attributes associated with a type. For example, in the preceding example, the result <class ‘list’> tells us that the data is the list data type. So we know that it is an instance of a class called list. If we are looking to find out the available attributes for a given object, we can use the dir() function. The function will return a list of the relevant attributes.

Example:

>>> mylist = list()

>>> dir(mylist)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>>

Names surrounded by double-underscores, such as __add__ and __class__, are sometimes called dunder-named items, where dunder is short for double underscores. (Dunder-named items are often referred to as special variables or magic methods). Each dunder-named item represents something built into Python that plays a role we do not necessarily access directly.

Using the help() function

We can get more detailed information by using help() rather than dir. The output will provide substantial information about the object in parentheses. For example, where dir(mylist) lists the names of attributes of that type, help(mylist.pop) provides more detail about each attribute such as the pop() method associated with list objects.

Example:

>>> mylist = list()

>>> help(mylist.pop)

Help on built-in function pop:

pop(index=-1, /) method of builtins.list instance

    Remove and return item at index (default last).

    Raises IndexError if list is empty or index is out of range.

>>>

Conclusion

In this article, we have explained what Python built-in functions are. We have provided examples of some of the available built-in functions. We have demonstated with examples how some of these functions can be used to achieve different desired results.

Inno

Inno

Related Posts

http://innotechtips.com
Programming

10 Reasons Why You Should Learn Python

July 17, 2023
Dictionaries and Sets in Python
Programming

Dictionaries and Sets in Python

July 27, 2023
List and Tuples in Python Programming – An Overview
Programming

List and Tuples in Python Programming – An Overview

July 27, 2023
Introduction to Functions in Python
Programming

Introduction to Functions in Python

July 27, 2023

Category

  • Linux
  • Other Tech Tips
  • Programming
  • Windows

Recommended.

How To Connect Kali Linux to Wi-Fi on VirtualBox

October 9, 2023

20 Linux Commands for Beginners

March 31, 2024

Trending.

How to Install and Enable Free VPN in Kali Linux

July 27, 2023

How to Connect Kali Linux on VMware Workstation to a Wi-Fi Network

July 27, 2023
How to Connect to Kali Linux from Window’s Command Prompt

How to Connect to Kali Linux from Window’s Command Prompt

July 27, 2023

How to Capture Network Traffic on a Wi-Fi Network using Kali Linux

July 27, 2023

IPv4 to IPv6 Transition Challenges

July 27, 2023

About us

This site is dedicated towards highlighting various important aspects of information technology. The key areas includes programming, Linux, software, and security. The content will include articles as well as videos.

Quick Links

Menu
  • Home
  • Linux
  • Programming
  • Other Tech Tips
    • Windows
    • Security

Privacy Policy

Menu
  • Privacy Policy
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}
No Result
View All Result
  • Cookie Policy (EU)
  • Home 1
  • Home 2
  • Home 3
  • Mytest Page
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.