Python Scripting for Automation |Day ~1|

Python Scripting for Automation |Day ~1|

ยท

7 min read

Introduction

Introduction to Python

Hi, Welcome to this blog of python Scripting for automation. Python is very popular in different areas.

Application areas of Python

Now a days python is becoming more and more popular for doing

  • Web-Application Development
  • API Development
  • GUI Application Development
  • Data Analysis
  • AI (Artificial Intelligence)
  • Web Scraping & Text Processing
  • Selenium Testing with Python
  • Automation Not only these areas but there are many more as well but these are the major areas in which python is popular.

See, if Python is popular than definitely it has some unique features compare to other programming languages. or scripting languages. now let's take a look on what are those any why it is popular ?

  • Python is easy to learn because of its simple syntaxes.
  • Many modules (pre-defined and third party).

Now if you know python than it's OK if not then assume module as pre-defined python program or pre defined python script. just assume that working with database is a common scenario at you application development side. now suppose if you are going to develop your application and in that you need to write a code to connect with your database, to run some queries with you database. That is a common task that' why for this task we don't need to write a code. Whenever a task or common or repetitive for that type of task already someone has written a python script and that's actually we are calling as Module. Now we can use that module directly in our application development code. so we don't need to write the code from scratch to work with database. That means we are Saving sometime and going to reduce the number of lines in our code.

Note :- there are not only module to work with database there are so many module to work with different areas.

  • Code length is very short because of third party module
  • Platform Independent

Assume that you are going to work as a middleware admin, your work is to install and manage tomcat on different operating system for example Windows like OS and Unix like OS. now you have to automate some task on your tomcat and you don't know python. now first observe at windows side, here you need to write batch script or use PowerShell to automate the task, but same script did not work with your UNIX like system, if you come to unix side you have to again write some shell script to automate same task. Now you have observed that you written different script for same task for different OS. Now if you know python than you can just write one script in python and run on both the Operating system. Because of this we are calling Python as platform independent.

  • Python supports functional as well as OOPs concepts

What is Python?

Python is a powerful multi-purpose programming language created by Guido van Rossum.

Python Versions

There are two types of versions.

  1. 2.x
  2. 3.x
  • There is no any development with 2.x after 2020 so it is good choice to learn and practice python with 3.x version

Latest version of Python is 3.x is 3.10.6

Installing Python

Now I am assuming you can install python in your operating system without ant help.

If you have installed and configured python on you machine then check it using command python --version If python version is displayed than you are ready to move forward.

image.png I am using version 3.10.5 but yo can use the latest one.

So let's move forward.

Basics of print, indentation, comments and special characters

Simple Hello world Script

So here our intention is to write some program and get the output as Hello world. To print any string or message we can use print function. in the parentheses of the function we have to write any string or message with quotation.

Script :-

print("Hello World")

Save the file

You can give any name to the file but the extension must be .py so that python would know this is a python file.

Run the Script To run the script file we can use terminal and run

python filename.py

image.png

The above script printing only one line let's print more lines

Script :-

print("Hello World")
print("Welcome to Python Scripting")
print("My name is Gautam, and this is my 1st script")

Run the script :-

image.png

By this we understand that

  • we can print many lines with python script
  • Python script are executed line by line

Indentation in Python

What is indent?

Indent is nothing but a space.

If you know some other language like C, C++, JAVA or any bash script. Let's take example of C language. So in C language to represent some block of code we are using curly braces "{ }". The line between the curly braces are called a block, to represent some set of line as a separate code we were using curly braces in C language.

In python we don't have curly braces to represent a block of code instead of that we are using indentation. we are using space to represent a block of code.

Example :-

print("Indentation..")
if 4>2:
    print("Using if statement")
    print("4 is greater than 2")

Output :-

image.png

Don't worry if you don't know if statements in python. just observe the code and spaces.

I have given some space for two lines below if 4>2: and than written print statement. That two lines are block of code which is under "if" line. These spaces are called indentation.

The amount of indentation is up to us but it must be consistent throughout that block. If you are using 5 spaces for the indentation than we have to use only 5 spaces throughout that block not more nor less.

Let's see what happen to output if we give unnecessary indentation

print("Indentation..")
if 4>2:
    print("Using if statement")
        print("4 is greater than 2")

Output :-

image.png

See we are getting error as IndentationError: unexpected indent

Python Comments

Comments :- Comments are nothing but the lines in our code which is not executed by python.

There is only one type of comment in python i.e. single line comment we can use "#" as a single line comment Example :-

# printing my name
print("Gautam Jha")

In above code the line with # is a comment it is not going to executed by python.

Output :- image.png

# printing my name 
print("Gautam Jha")

# using string literal as multi-line comment
"""
python ignore string literals 
    that are
    not assigned to a variable
    so we can use it as a multi-line comment
"""

print("I am interested in DevOps")

Python ignores string literals that are not assigned to a variable so we can use it as a multi-line comment.

Output :-

image.png

  • String literals can be used with single quotes (''' ''') or double quotes (""" """) both.

Usage of special characters with print statement.

  • We always have to write special characters only inside the quotation maybe single or double quotes

(\n) ----> new line :- used to move to next line in python so instead of writing multiple print statement we can use \n in single print statement line.

Example :-

print("Welcome to 20 days with python automation \nwe 
are currently learning basics now")

Output :-

image.png

(\b) ----> back space :- \b is used to send the cursor 1 position back.

Example :-

print("Hello \bWorld")

Output :-

image.png

Here we have seen that space between Hello and World is not displayed.

If we write more \b then more backspace will be executed.

Example :-

print("Hello \b\b\bWorld")

Output :-

image.png

(\t) ----> tab space :- \t is used to provide tab space in the output. as number of \t is there in program that many tab spaces.

Example :-

print("Hello \tWorld")

Output :-

image.png

( \ ) ----> Escape character :- \ is used to escape the special character from the sequence. for example if we want to print (This is "Python" class). then we get an error

Example :-

print("This is "Python" class")

Output image.png

See, we are getting a syntax error because of the double quotes in between the statement. python is not able to determine which quote is closing quote. so here we need an escape sequence character so that python can escape th quotes between the statement.

print("This is \"Python\" class")

Output :-

image.png

There are some other special character while going forward in this series we will discuss them

For now that's all for day 1 meet you tomorrow. ๐Ÿ˜ƒ

Thank You for reading ๐Ÿ˜Š๐Ÿ˜Š

Did you find this article valuable?

Support Gautam Jha by becoming a sponsor. Any amount is appreciated!

ย