Automation Script Writing
Introduction To Groovy Programming
Starting with the first program printing "Hello World". There is no interactive shell available like other scripting langugages tcl, python etc.,
There are two ways to code groovy script, one is standard way as below. Following coding guidelines is important in professional development projects.
virtual-machine:~/groovy$ cat hello.groovy
class Hello {
static void main(String[] args) {
println('Hello World \n');
}
}
virtual-machine:~/groovy$ groovy hello.groovy
Hello World
The other way is non standard which doesn't follow any coding structure. However this is helpful to understand the groovy commands and usage. Beginners are suggested to follow this to ensure they learn commands pretty quickly. If you observe there is no change during execution for such simple code.
virtual-machine:~/groovy$ cat hello.groovy
println('Hello World \n');
virtual-machine:~/groovy$ groovy hello.groovy
Hello World
Some key rules to follow while writing groovy are.
1) Like other programming languages, not scripting!
2) Use semicolons for each program statement so as to distinguish the code and separate each statement.
3) Curly braces are used to start code block for void main(), conditional blocks, functions etc.,
4) Variable declaration is also similar. Use identifiers to define the variables.
This is discussed in detail going forward.
Python Standard Data Types
Traditional data types includes strings, Numbers, lists or arrays but Python has two more distinct data types. Those are tuple and dictionary.
String data type is similar to other programming. String processing and manipulation is quite simple in scripting languages like python, perl and php. Discussing few examples will give a clear picture of how a python string variable works. Continuous series of characters defined in quotes and assigned to a variable name is nothing but a string variable.
>>> test1 = "john"
>>> test2 = "Abraham"
>>>
>>> test1+test2
'johnAbraham'
Number variable is defined to have various types in it like int, long, float, complex.
>>> test1 = 20
>>> test2 = 10
>>> test1+test2
30
List variable holds a group of comma separated items in square braces. There are several commands to add, remove, select, insert, slice the list variable.
test1 = [1, 2, 3 ,4, 5]
>>> test1
[1, 2, 3, 4, 5]
Tuple is like a list holding group items in parentheses instead of square braces but it can only be read only. It means tuple cannot be manipulated in any way, it just a read only.
>>> test2 = (1, 2, 3, 4, 5)
>>> test2
(1, 2, 3, 4, 5)
In extension to the list there is another variable which holds multiple key value pair. This gives flexibility to refer an element using the key associated to it.
>>> test
{'name': 'John', 'age': '32', 'country': 'AFRICA'}
>>> test['name']
'John'
String data type is similar to other programming. String processing and manipulation is quite simple in scripting languages like python, perl and php. Discussing few examples will give a clear picture of how a python string variable works. Continuous series of characters defined in quotes and assigned to a variable name is nothing but a string variable.
>>> test1 = "john"
>>> test2 = "Abraham"
>>>
>>> test1+test2
'johnAbraham'
Number variable is defined to have various types in it like int, long, float, complex.
>>> test1 = 20
>>> test2 = 10
>>> test1+test2
30
List variable holds a group of comma separated items in square braces. There are several commands to add, remove, select, insert, slice the list variable.
test1 = [1, 2, 3 ,4, 5]
>>> test1
[1, 2, 3, 4, 5]
Tuple is like a list holding group items in parentheses instead of square braces but it can only be read only. It means tuple cannot be manipulated in any way, it just a read only.
>>> test2 = (1, 2, 3, 4, 5)
>>> test2
(1, 2, 3, 4, 5)
In extension to the list there is another variable which holds multiple key value pair. This gives flexibility to refer an element using the key associated to it.
>>> test
{'name': 'John', 'age': '32', 'country': 'AFRICA'}
>>> test['name']
'John'
Basic Rules To Follow While Writing Python Code
<b>First:</b> Python keywords are very basic at the stage of learning this scripting language. These are most commonly and frequently used while coding.
Purpose of each keyword is known clearly while applying it in programming.
and lambda not
assert continue or
break for pass
class yield print
finally global except
del if with
while import try
elif in def
else is return
raise exec from
<b>Second:</b> Indentation is really interesting thing in python. This is optional and nice to have indented code blocks in other programming languages.
Whereas in python this is mandatory, otherwise the program execution is failed. At various steps indentation is required like after conditional statements, functions, modules etc. Error condition triggered without indentation is shown below.
>>> if x > y:
... print "x is greater"
File "<stdin>", line 2
print "x is greater"
^
IndentationError: expected an indented block
<b>Third:</b> String assignment is done using "equal to" or = operator. But the string text is defined in single, double and triple quotes. One example can give a better picture.
>>> x = 'python'
>>> y = "Python2"
>>> z = """Python3"""
>>> x
'python'
>>> y
'Python2'
>>> z
'Python3'
>>>
Purpose of each keyword is known clearly while applying it in programming.
and lambda not
assert continue or
break for pass
class yield print
finally global except
del if with
while import try
elif in def
else is return
raise exec from
<b>Second:</b> Indentation is really interesting thing in python. This is optional and nice to have indented code blocks in other programming languages.
Whereas in python this is mandatory, otherwise the program execution is failed. At various steps indentation is required like after conditional statements, functions, modules etc. Error condition triggered without indentation is shown below.
>>> if x > y:
... print "x is greater"
File "<stdin>", line 2
print "x is greater"
^
IndentationError: expected an indented block
<b>Third:</b> String assignment is done using "equal to" or = operator. But the string text is defined in single, double and triple quotes. One example can give a better picture.
>>> x = 'python'
>>> y = "Python2"
>>> z = """Python3"""
>>> x
'python'
>>> y
'Python2'
>>> z
'Python3'
>>>
Introduction to Python Programming
Similar to other scripting languages Python has got more support online from technical forums and strong base made it popular.
It is easy to code in Python rather than most other programming or scripting languages.
One can test simple python commands on python shell invoked on any OS prompt like DOS shell or terminal on windows.
virtual-machine:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Note: This is Python3 course, so there are slight differences in python2 and python3. Please be aware of it.
A simple and famous "Hello World" program in python is as simple as below.
>>> print ("Hello World \n")
Hello World
>>> exit()
Above exit function is to close the interactive shell.
To write code in a file and execute it then follow the steps below. Single step code is written in a file test.py and executed using "python3 test.py" on linux terminal.
virtual-machine:~/python3_examples$ cat test.py
print ("Hello World \n")
virtual-machine:~/python3_examples$ python3 test.py
Hello World
It is easy to code in Python rather than most other programming or scripting languages.
One can test simple python commands on python shell invoked on any OS prompt like DOS shell or terminal on windows.
virtual-machine:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Note: This is Python3 course, so there are slight differences in python2 and python3. Please be aware of it.
A simple and famous "Hello World" program in python is as simple as below.
>>> print ("Hello World \n")
Hello World
>>> exit()
Above exit function is to close the interactive shell.
To write code in a file and execute it then follow the steps below. Single step code is written in a file test.py and executed using "python3 test.py" on linux terminal.
virtual-machine:~/python3_examples$ cat test.py
print ("Hello World \n")
virtual-machine:~/python3_examples$ python3 test.py
Hello World
Subscribe to:
Posts (Atom)
Featured Post
Introduction To Groovy Programming
Starting with the first program printing "Hello World". There is no interactive shell available like other scripting langugages t...
-
Traditional data types includes strings, Numbers, lists or arrays but Python has two more distinct data types. Those are tuple and dictionar...
-
<b>First:</b> Python keywords are very basic at the stage of learning this scripting language. These are most commonly and frequ...
-
Similar to other scripting languages Python has got more support online from technical forums and strong base made it popular. It is easy to...