Python Example for Beginners

Python Built-in Methods – Python sorted() Function

Python sorted() Function Sorts the items of an iterable Usage The sorted() method sorts the items of any iterable You can optionally specify parameters for sort customization like sorting order and sorting criteria. Syntax sorted(iterable,key,reverse) The method has two optional arguments, which must be specified as keyword arguments. Parameter Condition Description iterable Required Any iterable (list, tuple, dictionary, set …

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 min() Function

Python min() Function Returns the smallest item Usage The min() function can find the smallest of two or more values (such as numbers, strings etc.) the smallest item in an iterable (such as list, tuple etc.) With optional key parameter, you can specify custom comparison criteria to find minimum value. Syntax min(val1,val2,val3… ,key) Parameter Condition Description val1,val2,val3… Required Two or more values to compare key …

Python Built-in Methods – Python max() Function

Python max() Function Returns the largest item Usage The max() function can find the largest of two or more values (such as numbers, strings etc.) the largest item in an iterable (such as list, tuple etc.) With optional key parameter, you can specify custom comparison criteria to find maximum value. Syntax max(val1,val2,val3… ,key) Parameter Condition Description val1,val2,val3… Required Two or more values to compare key …

Python Built-in Methods – Python len() Function

Python len() Function Returns the number of items of an object Usage The len() function returns the number of items of an object. The object may be a sequence (such as a string, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). Syntax len(object) Parameter Condition Description object Required A sequence or a collection. len() on Sequences # number of characters in a string S …

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 …