Python built-in constructors

Share Your Love

There are several python built-in constructors that are available as part of the standard library or built-in types. These constructors are used to create objects of specific types or perform type conversions. Here are some commonly used built-in constructors:

int():

  • Used to create an integer object or convert a value to an integer.
  • Example:
    python num = int("10") print(num) # Output: 10

float():

  • Used to create a floating-point number object or convert a value to a float.
  • Example:
    python num = float("3.14") print(num) # Output: 3.14

str():

  • Used to create a string object or convert a value to a string.
  • Example:
    python text = str(42) print(text) # Output: "42"

list():

  • Used to create a list object or convert an iterable to a list.
  • Example:
    python my_list = list("hello") print(my_list) # Output: ['h', 'e', 'l', 'l', 'o']

tuple():

  • Used to create a tuple object or convert an iterable to a tuple.
  • Example:
    python my_tuple = tuple([1, 2, 3]) print(my_tuple) # Output: (1, 2, 3)

dict():

  • Used to create a dictionary object or convert an iterable of key-value pairs to a dictionary.
  • Example:
    python my_dict = dict([(1, 'one'), (2, 'two')]) print(my_dict) # Output: {1: 'one', 2: 'two'}

set():

  • Used to create a set object or convert an iterable to a set.
  • Example:
    python my_set = set([1, 2, 3]) print(my_set) # Output: {1, 2, 3}

bool():

  • Used to create a boolean object or convert a value to a boolean.
  • Example:
    python value = bool(0) print(value) # Output: False

Each constructor serves a specific purpose and allows you to create objects or perform type conversions based on your requirements.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter