Python Tutorials

Python Built-in Methods – Python range() Function

Python range() Function Generates a sequence of numbers within a given range Usage The range() function generates a sequence of numbers from start up to stop, and increments by step. When used in a for loop, range() provides a simple way to repeat an action a specific number of times. for x in range(3): print(‘Hello!’) # Prints Hello! Hello! Hello! Syntax range(start,stop,step) Parameter Condition …

Python Built-in Methods – Python property() Function

Python property() Function Generates a property Usage You generate a property by calling the property() built-in function, passing in three methods (getter, setter and deleter) as well as the docstring for the property. If any argument is passed as None or omitted, that operation is not supported. Syntax attrib = property(fget,fset,fdel,doc) Parameter Condition Description fget Optional A function for …

Python Built-in Methods – Python pow() Function

Python pow() Function Raises a number to a power Usage The pow(x, y) function calculates the value of x to the power of y ( x y ). If a third argument z is specified, pow(x, y, z) function returns x to the power of y, modulus z  ( x y % z ). Syntax pow(x,y,z) Parameter Condition Description x Required The base y Required The exponent z Optional The …

Python Built-in Methods – Python list() Function

Python list() Function Creates a list from an iterable Usage The list() function creates a list from an iterable. The iterable may be a sequence (such as a string, tuple or range) or a collection (such as a dictionary, set or frozen set) There is another way you can create lists based on existing lists. It is called List comprehension. Syntax list(iterable) Parameter Condition Description iterable Required A sequence …

Python Built-in Methods – Python int() Function

Python int() Function Converts a string or number to an integer Usage The int() function converts the specified value to integer. A value can be a number or a string, except complex numbers. You can also specify the base (number formats like binary, hex, octal etc.) of the given value. Syntax int(value,base) Parameter Condition Description value Optional A number or a string to be converted into …

Python Built-in Methods – Python id() Function

Python id() Function Returns a unique id of an object Usage The id() function returns a unique id for the specified object. Syntax id(object) Parameter Condition Description object Required Any object (such as number, string, list, class etc.) What is ID of an Object? In Python, every object has its own unique id. Every time you create an object, a unique id is …

Python Built-in Methods – Python globals() Function

Python globals() Function Returns the global symbol table as a dictionary Usage The globals() function returns the global symbol table as a dictionary. It contains information about all global variables. Each key in the dictionary holds the name of the variable. This dictionary is also accessible from within functions and can be used to update global variables …

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 …