Tuesday, July 26, 2022

Numeric data types in Python

Numeric Data types

There are three different data types to store numeric data. 
  • int
  • float
  • complex

int (integer)

Data type 'int' is used to hold the whole numbers (i.e., no decimal positions). This number can either be positive or negative. 

1

2

3

4

5

6

7

# int - used to hold whole numbers

# Positive numbers

positive_number = 1


# Negative numbers

negative_number = -2


It is possible that an integer data can be stored in a string and we might need to convert it to the int type. We can do that using built-in method int(). 

1

2

3

4

# String to int

number_as_string = "10"

number = int(number_as_string)

 


int() method can also be used to convert floating point numbers to integer. This removes any decimal values and moves whole number to integer variable. 

1

2

3

4

5

6

7

# Float to int

float_value = 10.0

number = int(float_value) # 10

 

float_value = 10.50

number = int(float_value) # 10

 


float (floating point numbers)

Data type 'float' is used to hold the floating point numbers (i.e., numbers with at least one decimal point). This number can either be positive or negative. 

1

2

3

4

5

6

7

# float - used to hold floating point numbers

# positive floating point number

positive_float = 10.123

 

# negative floating point number

negative_float = -10.123

 


We can also specify floating point numbers with 'e' to indicate power of 10. 

1

2

3

4

# float with 'e' to indicate power of 10

float_value = 1.245e10

print(float_value) # 12450000000.0

 


In a similar way, if we have large floating point value and when we print the data, it returns the data with power of 10. 

1

2

3

4

# large floating point value

float_value = 1234567890123456789.012345

print(float_value) # 1.2345678901234568e+18

 


Result of a division operation is always returned as float, even though there are no decimal places in the result. 

1

2

3

result = 10/5

print(result) # 2.0

 


Built-in float() method can be used to convert floating point number from string or int. 

1

2

3

4

5

6

7

8

9

10

# string to float

float_as_string = "12.34"

float_value = float(float_as_string)

print(float_value) # 12.34

 

# int to float

integer_value = 12

float_value = float(integer_value)

print(float_value) # 12.0

 


complex

Data type 'complex' is used to hold imaginary numbers (indicated by 'j'). This can be a combination of real numbers and imaginary number (multiplied by 'j'). value of 'j' is equal to square root of -1. 

1

2

3

4

5

# complex values

imaginary_value = 1j

 

real_and_imaginary = 4 + 1j

 


Like any other numbers, we can do addition or subtraction of complex numbers. 

1

2

3

4

5

6

7

8

# complex values

imaginary_value = 1j

real_and_imaginary = 4 + 1j

 

# Addition and subtraction of complex values

res1 = imaginary_value + real_and_imaginary # (4+2j)

res2 = real_and_imaginary - imaginary_value # (4+0j)

 


Built-in complex() method can be used to convert string, int or float values to complex. 

1

2

3

4

5

6

7

8

9

10

# string, int or float to complex

int_value = 10

complex_value = complex(int_value) # (10+0j)

 

float_value = 10.0

complex_value = complex(float_value) # (10+0j)

 

string = "10+j"

complex_value = complex(string) # (10+1j)

 


We have seen different numeric data types (int, float and complex) and how to convert the data from one type to another numeric data type. Hope this has been helpful in understanding a bit about numeric data types. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Sunday, July 24, 2022

Retrieve list of Spooled files on System from SQL - IBM i

Spooled Files

Spooled files are generated when when system writes (prints) the data to an Output Queue. These could be Reports generated by the programs using Printer File (PRTF), System dumps or Job logs. 

Traditional way of getting the list of spooled files is by using WRKSPLF command, which by default displays the list of spooled files for the current user. 

Same can be retrieved from SQL with the use of table function SPOOLED_FILE_INFO. This is available in QSYS2. 

SELECT * FROM TABLE(QSYS2.SPOOLED_FILE_INFO());


Above query is simple and straight forward which returns the list of spooled files for the current user. Information returned is same as WRKSPLF (Work with Spooled files) and API (QGYOLSPL). 

Let's have a look at how output of the query looks like. 

SPOOLED_FILE_INFO


This query returns a full list of columns (we will see all the columns towards the end) for the current user who is running the query. 

We can make use of the parameters of the table function to retrieve spool files of all/other users and/or to narrow down the selection of the spool files. 

SELECT SPOOLED_FILE_NAME, STATUS, CREATION_TIMESTAMP, JOB_NAME, JOB_USER, JOB_NUMBER FROM TABLE(QSYS2.SPOOLED_FILE_INFO(USER_NAME => 'REDDYP'));


In the above query,
  • Parameter USER_NAME is used to select all the spool files for the specific user. 
  • Selecting few columns to look at the data we are interested in. 
    • SPOOLED_FILE_NAME - Name of the spooled file. 
    • STATUS - Status of the spool file. Below are the valid statuses.
      • CLOSED
      • DEFERRED
      • DELETED
      • HELD
      • MESSAGE WAITING
      • OPEN
      • PENDING
      • PRINTING
      • READY
      • SAVED
      • SENDING
      • WRITING
    • CREATION_TIMESTAMP - Timestamp of the spooled file creation. 
    • JOB_NAME - Name of the job that created the spooled file.
    • JOB_USER - Name of the user created the spooled file.
    • JOB_NUMBER - number of the job that created the spooled file.
We could add additional parameters as required. 

SELECT SPOOLED_FILE_NAME, STATUS, CREATION_TIMESTAMP, JOB_NAME, JOB_USER, JOB_NUMBER FROM TABLE(QSYS2.SPOOLED_FILE_INFO         (USER_NAME => 'REDDYP',         STARTING_TIMESTAMP => '2022-06-01-00.00.00.000000',         ENDING_TIMESTAMP => '2022-06-30-23.59.59.999999'));


In the above query, we are using the parameters to retrieve the list of spooled files as per the selection. Below are the valid parameters for the table function. One thing to note here is filtering on multiple values for user name, output queue, user data or status makes the query to run slower.
  • USER_NAME - Name of the user profile. This can contain up to 20 user names separated by blanks. Special values *ALL (all users on the system) or *CURUSR (Current user, default value) can be specified.
  • STARTING_TIMESTAMP - Timestamp of the earliest spooled file to be returned. If not specified all the spooled files are returned (or less than ending_timestamp if specified). 
  • ENDING_TIMESTAMP - Timestamp of the latest spooled to be returned. If not specified all the spooled files are returned (or greater than starting_timestamp if specified). 
  • STATUS - Status of the spooled file. Full list of status values are specified above and '*' needs to be prefixed in order to retrieve the specific status (E.g.: To retrieve spooled files in CLOSED status *CLOSED needs to be specified). Special value of *ALL can be specified if all the statuses are to be considered, *ALL is the default value. 
  • JOB_NAME - Qualified job name. Special values '*' (Current job) and '*ALL' (All jobs matching with any other parameters specified, default) can be specified. 
  • OUTPUT_QUEUE - Name of the output queue to be specified followed by library name (library_name/output_queue_name). Up to 20 values can be specified separated by blanks. *ALL (default) can be used to consider all output queues.  
  • USER_DATA - Value of the user specified data. *ALL (default) can be specified to consider all. 
  • FORM_TYPE - Form type. *ALL (all form types, default) or *STD (Only files that specifies standard form type) can be specified. 
  • SYSTEM_NAME - Name of the system name in which spooled file is created on. Special values *CURRENT (Spooled files created on current system) or *ALL (All spooled files irrespective of system they were created on) can be specified. 
In addition to the parameters of the table function, data can be further filtered by using the columns of the table function. 

SELECT SPOOLED_FILE_NAME, STATUS, CREATION_TIMESTAMP, JOB_NAME, JOB_USER, JOB_NUMBER FROM TABLE(QSYS2.SPOOLED_FILE_INFO         (USER_NAME => 'REDDYP',         STARTING_TIMESTAMP => '2022-06-01-00.00.00.000000',         ENDING_TIMESTAMP => '2022-06-30-23.59.59.999999'))

WHERE STATUS = 'READY';


Full list of columns and more details on the parameters can e found here


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Sunday, July 17, 2022

Java Terminology

Java Terminology

Below are some key java terms which helps to understand how java works.
  • Java Virtual Machine (JVM)
  • Bytecode
  • Java Development Kit (JDK)
  • Java Runtime Environment (JRE)

Java Virtual Machine (JVM)

JVM is a program designed to run Java programs across different platforms. There are different versions of JVM for different operating systems (Windows, Mac and Linux). JVM allows running Java programs on any operating system. This makes Java platform independent language. This is one of the primary function of JVM. 

Another important function is Memory management. JVM manages memory through a process called Garbage collection, which continuously identifies and eliminates unused memory in Java programs. Garbage collection happens inside a running JVM. 

Bytecode

Java compiler compiles the Java programs into bytecode. Bytecode then can be executed by using JVM. Bytecode is saved in a .class file. 

Java Development Kit (JDK)

Java development kit provides the tools required to create, compile and run Java programs. JDK includes JRE, compilers and other tools like JavaDoc, Java debugger etc. 

Java Runtime Environment (JRE)

JRE is part of JDK. JRE contains JVM, browser plugins and applets support. 

JRE is to be installed on the system to be able to run the Java programs. In other words, JRE provides the environment to run Java programs. JRE alone is sufficient if the intention is only to run the Java programs and not create them. 

JDK contains both compiler and JRE and enables users to create java programs and run them on the system. 


Hope the above content has been of some help to you. 

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Saturday, July 16, 2022

Python Syntax & Comments

Syntax

One of the biggest advantages of Python is it's syntax. It is easy to read, write and understand. 

In this article, I'm going to cover the below in brief.
  • Comments - How to write single line and multi line comments in Python
  • Variable definition - How to define variables in Python
  • End of statement - How does Python identify the end of a statement
  • Indentation - How integrated is indentation in Python syntax.

Comments

Comments are important part of any programming language. Python has two different ways of writing comments in the code. 
  • Multi-line comments 
  • Single-line comments 
Multi-line comments can be written by mentioning three quotes (''') at the beginning and ending of the comment.  

E.g.:

'''
Multiple lines of comments
Usually written at the start of a program/function
describing usage of the program/function
'''

Multi-line comments are helpful when we need to write few lines of comments to describe about a block of code, a function or a program. 

Single-line comments can be written with # and comment would end with End of line. 

# Single line comments end with End of Line
print("Hello...") # Print Hello

Variable Definition

Python is dynamically typed language and doesn't require variables to be defined with a specific data type. Python identifies the data type automatically based on the data passed into the variable. 

E.g.: 

a = 10  # Integer
b = "B" # String

End Of Statement

Unlike most of other programming languages, Python doesn't use/require a semicolon (;) to indicate end of statement. End of line (CR LF) is usually considered and end of statement. 

E.g.: 

a = 10
b = "B"

As soon as end of line is detected, Python assumes the end of statement and considers the next line as the next statement. 

We can also terminate a statement with semicolon (;). This usually helpful when we need to write multiple statements in a single line. 

a = 10; b = "B"

What if the statement is too big and has to extend beyond a line? Using '\' at the end of the line would help if a statement is going beyond single line. 

c = "This is a big statement " \
"and has to extend beyond single line."

d = a/2 +\
100

Above two statements are equal to the below. 

c = "This is a big statement and has to extend beyond single line."

d = a/2 + 100

This can also be done by enclosing with in parenthesis. 

c = ("This is a big statement " +
"and has to extend beyond single line.")

d = (a / 2 +
100)

Indentation

Indentation is recommended in any programming language to make the code easy to understand. In Python, indentation is mandatory to indicate/write a block of code. 

There is no restriction on how many spaces are to be used, but number of spaces need to be consistent in a block of code. But, 4 spaces are widely used.

# This function uses 4 blank spaces
def printA():
a = 10
print(a)

# This function uses 2 blank spaces
def printB():
b = "B"
print(b)

But, we cannot have different spaces in the same block of code. 

# This function uses 4 blank spaces
def printA():
a = 10
print(a)

Having different number of spaces would cause the program to throw IndentationError (IndentationError: unindent does not match any outer indentation level) 

A block of code doesn't necessarily be a function, it can also be a condition or loop. 

# This function uses 4 blank spaces
def printA():
a = 10
b = 20

# This condition uses 2 blank spaces
if (a > b):
print("a is greater than b")
# Code with in else block uses 8 blank spaces
else:
print("b is greater than a")

In the above code, 
  • Statements with in printA function including if and else, are to follow the same indentation. 
  • Any code under if or else need to use the same indentation. 
One thing to note here is, A block of code would always be preceded by colon (:).


Hope the above content has been of some help to you. 

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...