Beginner’s Guide to Python

Python Built-in Methods – Python enumerate() Function

Python enumerate() Function Adds a counter to an iterable Usage The enumerate() function adds a counter to an iterable and returns it as an enumerate object. By default, enumerate() starts counting at 0 but if you add a second argument start, it’ll start from that number instead. Syntax enumerate(iterable,start) Parameter Condition Description iterable Required An iterable (e.g. list, tuple, string etc.) start Optional A number to start counting …

Python Built-in Methods – Python divmod() Function

Python divmod() Function Returns quotient and remainder of division Usage The divmod() function returns a tuple containing the quotient and the remainder when dividend is divided by divisor. Syntax divmod(dividend,divisor) Parameter Condition Description dividend Required The number being divided divisor Required The number doing the dividing Basic Example # Return quotient and remainder when 5 is divided by 2 x …

Python Built-in Methods – Python complex() Function

Python complex() Function Converts a string or number to a complex number Usage The complex() function creates a complex number when real and imaginary parts are specified. It can also convert a string to a complex number. The complex number is returned in the form of real + imaginary, where the imaginary part is followed by a j. Syntax complex(real,imaginary) Parameter Condition Description real Optional The …

Python Built-in Methods – Python chr() Function

Python chr() Function Returns the Unicode character Usage The chr() function returns the character that represents the specified number (unicode code point). The valid range for the number is from 0 to 1,114,111 (0x10FFFF). ValueError will be raised if you specify number outside that range. You can convert it back to unicode using the ord() function. Syntax chr(number) Parameter Condition Description number Required An …

Python Built-in Methods – Python bin() Function

Python bin() Function Convert an integer number to a binary string Usage The bin() function converts an integer number to a binary string. The result will always be prefixed with ‘0b’. Syntax bin(number) Parameter Condition Description number Required Any integer Examples x = bin(42) print(x) # Prints 0b101010 You can pass a negative number to the function. x = bin(-42) …

Python Built-in Methods – Python ascii() Function

Python ascii() Function Returns a printable version of an object Usage The ascii() function returns a string containing a printable version of an object. It replaces any non-ascii characters with escape characters x, u or U For example, German letter ß will be replaced with xdf. Syntax ascii(object) Parameter Condition Description object Required Any object (such as string, list, tuple, dictionary etc) Basic Example # Return a printable version by …

Python Built-in Methods – Python any() Function

Python any() Function Determines whether any item in an iterable is True Usage The any() function returns True if any item in an iterable is True. Otherwise, it returns False. If the iterable is empty, the function returns False. Syntax any(iterable) Parameter Condition Description iterable Required An iterable of type (list, string, tuple, set, dictionary etc.) Falsy Values In Python, all the following values …

Python Built-in Methods – Python all() Function

Python all() Function Determines whether all items in an iterable are True Usage The all() function returns True if all items in an iterable are True. Otherwise, it returns False. If the iterable is empty, the function returns True. Syntax all(iterable) Parameter Condition Description iterable Required An iterable of type (list, string, tuple, set, dictionary etc.) Falsy Values In Python, all the following values …

Python Built-in Methods – Python abs() Function

Python abs() Function Returns the absolute value of a number Usage The abs() method returns the absolute value of a number. Syntax abs(number) Parameter Condition Description number Required Any number (integer, float or a complex number) Examples # Find absolute value of -10 x = -10 print(abs(x)) # Prints 10 The number may be a floating point number. x = -20.5 print(abs(x)) …