Beginner’s Guide to Python

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 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 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 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 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 …