Skip to main content

Posts

Showing posts from February, 2023

How to Iterate Over a List and Get Indexes with 'enumerate' Function in Python

'enumerate' function There are different ways of accessing the data from a list like using range and then index or accessing the element of a list directly from for loop and so on.  One other powerful way is by using 'enumerate' function. One major advantage of using 'enumerate' is this function returns both index and corresponding element at once. So, we don't have to maintain a counter to calculate the index or need to retrieve the element from a index.  This makes the code easy to maintain and understand.  In this post, we will see how to use 'enumerate' function and looping over a list to access index and elements as a tuple  and also by using tuple unpacking .  Syntax enumerate(iterable, start=0) 'enumerate' function accepts two parameters.  'iterable' - This is a mandatory parameter. An iterable (E.g.: list, tuple, string...) on which we need to run the loop and retrieve index and element. 'start' - This is an optional p

*OMIT vs *NOPASS: Understanding the Key Differences - IBM i

*OMIT v/s *NOPASS In RPGLE, *OMIT and *NOPASS are the two options used to specify the optional parameters for the procedures.  It is essential to understand the difference between *OMIT and *NOPASS because knowing which keyword to use for the specific case helps in writing the clean code.  Before we jump on to the key differences, let's have a look at the use of each of the keywords. *OMIT By using the *OMIT on the parameter while declaring a procedure, we can make this parameter an optional parameter.  We could either pass the data to this parameter or simply pass '*OMIT' to while calling the procedure.  Dcl-PR SampleProcedure;                                 MandatoryParameter1 Char(1);                          OptionalParameterWithOmit Char(1) Options(*Omit);     MandatoryParameter2 Packed(1);                      End-PR;                                                                                                     Dcl-Proc SampleProcedure Export;                   

Python File I/O: Reading, Writing, and Creating Files

Reading, Writing, and Creating Files in Python Python provides different built-in functions and methods for working with files. These features, widely known as file I/O (input/output), allows reading and writing data to and from the files located on local computer or network.  Reading and writing data from and to the files is essential for any  application, as it allows storing  and retrieving data that can be used by the programs/application. Some of the basic functions are opening and closing files, reading and writing data, and creating the new files. Let's start with opening and closing files before we move on to reading and writing.  Opening a file Before we get to reading and writing data from and to the file, we would first have to open a file.  We can use function 'open()' to open a file. We would need to pass two arguments to this function. Name of the file to be opened. Mode in which we need to open the file.  Syntax  file_object = open("sample_file.txt"