Skip to main content

Posts

Showing posts from January, 2021

What is the use of setdefault() method in Python

setdefault() setdefault() method returns the value from the dictionary for the key passed if already present in the dictionary. If not already present, it inserts a new entry with the key and value passed (if not passed assigns 'None' by default).  There are different ways to add new key to the dictionary and to retrieve the data present in the dictionary for a specified key. How is setdefault() different from these ? Let's take an example to understand the difference better. E.g.: When working with large data sets, It becomes essential to check if a key is present in the dictionary and insert a new key if not already present.  Below is one way of doing this without using setdefault() method. Check the data present in the dictionary by passing the key (with in the square brackets).  Monitor for the Key Error by using try and except and assign the value to the specific key.  If we don't check for the existence of key in dictionary and directly assigns the data, it wil...

RPG's new Built-in Function %LIST and New Operation Code FOR-EACH - IBM i

RPGLE RPG is the primary programming language for development on IBM i. And, IBM continues to provide enhancements to RPG.  In this post, we will see couple of recent additions to RPG.  Built-in Function %LIST Operation Code FOR-EACH Click Here to read my previous post on new BIF %RANGE and Operator IN.  %LIST New Built-in Function %LIST provides an easy way to populate the data into an array. %LIST accepts list of values and returns a temporary array. %LIST can be directly used where arrays are allowed. This provides an option to developer to use %LIST instead of having to create an array where ever possible. There are some exceptions to this, %LIST cannot be used with SORTA (Sort an Array), %LOOKUP and %SUBARR.  Syntax: %LIST(item1 : item2 : item3 : ...)  Below are some of the important points to note while working with %LIST.  Minimum of one operand to be passed.  All Operands must be of same type (like, %LIST with only numeric values or %LIST with...

Working with Sets in Python

Sets in Python Set is a collection of data (of different data types) and is specified using curly brackets.  Set is one of the Sequence data types in Python and has unique features compared to the other data types (list, tuple and dictionary). Below are some of the features of Sets.  Sets can hold data of different data types. Data in the set can only be added or removed and cannot be amended. Data in the set is unordered (i.e., every time a program is run, same data is stored in different order) and cannot be accessed by using index. Duplicate data is not allowed in Sets. In this post, we will see how to access, add or remove the data from sets and the use of different set methods.  Creating a set in Python Before we go on to different methods of a set, let us see How to create a set in Python . A set is created when the data is assigned with in curly brackets (Unlike dictionaries, no key value would be present in Sets).  Syntax: new_set = {"Value1", "Value2",...} ...

RPG's new Built-In Function %RANGE and new Operator IN - IBM i

RPGLE Keeping the debate aside on whether the RPG is a dead programming language, Most of the recent surveys showed RPG is the primary program language for development on IBM i. And, IBM continues to provide enhancements to RPG. In this post, we will see couple of recent additions to RPG.  Built-in Function %RANGE Operator IN %RANGE In SQL, we often use BETWEEN - AND in WHERE condition to check if a value is present in a particular range (specified using BETWEEN - AND).  %RANGE BIF (Built-In Function) works in a similar way and used to check if a value is present in a particular range. This can be used to compare the data of any data type (and data we are comparing should be of same data type as the data mentioned in the RANGE) Syntax: %RANGE(lower-limit : upper-limit) ; %RANGE accepts two arguments - Lower limit and Upper limit. Both Lower limit and Upper limit would be considered.  %RANGE doesn't return any value and should always be used with operator 'IN'. IN IN opera...

Working with Dictionaries in Python - Contd.

Dictionaries In the last post , I have explained how to create dictionary, add new values to the dictionary and remove the values from dictionary.  We have also seen the below methods.  setdefault() - Retrieve the data associated with the specific key passed. If key isn't present in the dictionary, new entry would be added.  pop() - Returns the data associated with the key (passed) and deletes the entry from dictionary. popitem() - Returns the last key and corresponding value from the dictionary. Accepts no parameters.  Click Here to go through about these in detail. We will see about some more methods associated with dictionaries in this post.  keys() - Retrieve the keys present in the dictionary. get() - Retrieve the value associated with the key passed.  clear() - Clears the dictionary. copy() - Copy the dictionary to other. items() - Returns the items (key & value) present in the dictionary. update() - Updates the dictionary with the elements ...

Display Journal from SQL - IBM i

Display Journal Journals play very important role on IBM i and are helpful to identify who has updated the data and/or to retrieve the data before the update or delete from the tables and so on.  Data from the journals can be retrieved by using DSPJRN command. One other way of retrieving the data from journals is by using SQL table function DISPLAY_JOURNAL. Data returned from this function is similar to the data returned by DSPJRN command.  We will see few examples to see how DISPLAY_JOURNAL works.  In the above example,  We are passing two mandatory parameters Journal Name and Library. 'JOURNAL_NAME' - Name of the Journal. 'JOURNAL_LIBRARY' - Name of the Library Journal is present in. Below are the few columns we are retrieving from Journal. This function returns much more data and full list can be found in this link  on IBM Knowledge center.  'ENTRY_TIMESTAMP' - Time when the Journal entry was captured (E.g.: Time when the data was written, updated or del...

Working with Dictionaries in Python

Dictionaries Dictionaries (dict) are used to store the data based on a key value. Unlike Lists and Tuples , data in the dictionaries isn't stored based on the index. And, data in the dictionaries are specified using curly brackets with the key and it's corresponding value ({key1 : value1, key2 : value2,...} Below are some of the features of dictionaries in Python.  Dictionaries can hold data of different data types. Data in the Dictionary can be amended.  Data in the Dictionary needs to be accessed by using the key. No duplicate key values are allowed in Dictionary (duplicate data can be present with different key). In this post, we will see how to access the data from dictionary, amend the data for a specific key value and different methods associated with dictionary.  Creating a dictionary in Python A dictionary is created when the data (and corresponding key value) in curly brackets is assigned to a variable. In the above example, 1, 2, "THREE", "FOUR" a...