Python Scripting

 


PYTHON SCRIPTING

Introduction to python scripting: It is easy to handle and has rich libraries.

Scripting is a very common practice among Python programmers. It’s used for automation of daily tasks, reporting, server management, security, social media management, business growth and development, financial trading, automating software and many other intelligent solutions.

Characteristics of Python

Following are important characteristics of Python Programming −

  • It supports functional and structured programming methods as well as OOP.

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

  • It provides very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

  • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Applications of Python

The latest release of Python is 3.x. As mentioned before, Python is one of the most widely used language over the web. I'm going to list few of them here:

  • Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.

  • Easy-to-read − Python code is more clearly defined and visible to the eyes.

  • Easy-to-maintain − Python's source code is fairly easy-to-maintain.

  • A broad standard library − Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.

  • Interactive Mode − Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.

  • Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.

  • Extendable − You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.

  • Databases − Python provides interfaces to all major commercial databases.

  • GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

  • Scalable − Python provides a better structure and support for large programs than shell scripting.

Python environment setup:

Open a terminal window and type "python" to find out if it is already installed and which version is installed.

Commands in python:

  1. printing:

Tp print character: print (“string”)

To print integers: print (value)


2. Python files has extension of .py

3. Variables:

“=”is used to assign values to the variables.

syntax for numerical : var = value

print (var)

syntax for string: var = “string”

print(var)

syntax to delete a variable: var = value

del var

multiple assignment: we can assign one value to multiple variables.

syntax: var1=var2=var3 = value

Python Variable Names

Every Python variable should have a unique name like a, b, c. A variable name can be meaningful like color, age, name etc. There are certain rules which should be taken care while naming a Python variable:

  • A variable name must start with a letter or the underscore character

  • A variable name cannot start with a number or any special character like $, (, * % etc.

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

  • Python variable names are case-sensitive which means Name and NAME are two different variables in Python.

  • Python reserved keywords cannot be used naming the variable.


4. Data types:

Python has various built-in data types which we will discuss with in this tutorial:

  • Numeric - int, float, complex

  • String - str

  • Sequence - list, tuple, range

  • Binary - bytes, bytearray, memoryview

  • Mapping - dict

  • Boolean - bool

  • Set - set, frozenset

  • None - NoneType

i. numeric data type: var = value

Python supports integer, float, long, complex types of numericals.

To print the type of data : type(var) 

ii. string: var = “string”

we can slice the string using []. [] => is known as a slice operator.

[0]: prints 1st character of the string

[2:5] : prints 3rd to 5th character of the string

[2:] : prints 3rd to last character of the string

[:4] : prints from 1st character to 5th character of the string

*3 : prints string 3 times

var + “string”  : concatenates the string

iii. list : Python list contains a set of elements separated by “,” inside [].

iv. Tuples: Python list contains a set of elements separated by “,” inside ().

v. Python ranges: python uses “range” keyword to print range of values from list or tuples. 

syntax: for var in range(start, stop, step):

print var

vi. Dictionaries: It is like hash in perl. It consists of a pair of values and keys. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

vii. Boolean: Python boolean type is one of built-in data types which represents one of the two values either True or False. Python bool() function allows you to evaluate the value of any expression and returns either True or False based on the expression.


viii. data type conversion: 

syntax: var = type(value/string)

5.  Operators: To do specific mathematical or logical operations python supports few in-built operators.

i. Arithmetic operations: It does all operations like add (+) , sub (-) , mul(*) , div(/) , modulo (%) , power(**) , floor division (//) .

ii. comparison operators: It performs equality, less than, greater than checks.

iii. Assignment operator: Python assignment operators are used to assign values to variables.

iv. Bitwise operators: Bitwise operator works on bits and performs bit by bit operation.


Comments

Popular posts from this blog

PERL scripting

SHORT CHANNEL DEVICE