Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf ·...

Preview:

Citation preview

Introduction to Scripting Languages 

Tassadaq Hussain Riphah International University

Barcelona Supercomputing CenterUniversitat Politècnica de Catalunya

Introduction

Traditional programming languages are concerned with building a self contained environment that receives input and produces output.

Most “real-world” computing however involves the use of multiple programs.

Programming Langauges

Languages such as Java stress efficiency, portability and maintainability.

The programs are based upon hardware-level concepts.

Examples of this include: fixed sized integers, floating point numbers, characters and arrays.

So how can we re-write the problem?

Through the use of a scripting language.

Scripting Languages

Scripting languages stress flexibility, rapid development and dynamic checking.

Their type systems embrace very high level concepts such as tables, patterns, lists and files.

There a number of distinct groups that fall under the scripting language family.

Languages such as Perl and Python are known as ``glue’’ languages because they were designed to glue existing programs together.

There are other extensible types of scripting languages used in the WWW also.

Languages For Machine Learning & Data Science

● Python● Java● R● C++● C● JavaScript● Scala

Python Language

● Everything in Python is an object. ● The objects can be either mutable or

immutable.● A mutable object can be changed after it is

created, and an immutable object can’t.

Development Environments

1. PyDev with Eclipse 2. Komodo3. Emacs4. Vim5. TextMate6. Gedit7. Idle8. PIDA (Linux)(VIM Based)9. NotePad++ (Windows)10.BlueFish (Linux)

Python Keywords

● Data Types/Structure● Control flow● File I/O● Modules

Data Types/Structure

● Lists● Tuples● Set● Dictionary

List

A compound data type:[0][2.3, 4.5][5, "Hello", "there", 9.8][]Use len() to get the length of a list>>> names = [“Ben", “Chen", “Yaqin"]>>> len(names)3

Use [ ] to index items in the list>>> names[0]‘Ben'>>> names[1]‘Chen'>>> names[2]‘Yaqin'>>> names[3]Traceback (most recent call last):File "<stdin>", line 1, in <module>IndexError: list index out of range>>> names[-1]‘Yaqin'>>> names[-2]‘Chen'>>> names[-3]‘Ben'

smiles = "C(=N)(N)N.C(=O)(O)O">>> smiles.find("(O)")

Tuples

● A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

● tup1 = ('physics', 'chemistry', 1997, 2000);

set● The sets module provides classes for constructing and

manipulating unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.my_set = {1, 2, 3}

print(my_set)

# set of mixed datatypes

my_set = {1.0, "Hello", (1, 2, 3)}

print(my_set)

Dictionaries

Dictionaries are lookup tables.They map from a “key” to a “value”.symbol_to_name = {"H": "hydrogen","He": "helium","Li": "lithium","C": "carbon","O": "oxygen","N": "nitrogen"}Duplicate keys are not allowedDuplicate values are just fine

List, Tuple, Set and Dictionary● List: Use when you need an ordered sequence of homogenous

collections, whose values can be changed later in the program.● Tuple: Use when you need an ordered sequence of heterogeneous

collections whose values need not be changed later in the

program.● Set: It is ideal for use when you don’t have to store duplicates and

you are not concerned about the order or the items. You just want

to know whether a particular value already exists or not.● Dictionary: It is ideal for use when you need to relate values with

keys, in order to look them up efficiently using a key.

● Data Types/Structure● Control flow● File I/O● Modules

Repetitionfor x in range(1, 6, +1): # range(start, stop, step)

print x

Condition

● if continuation : print value

if gpa > 2 :

print gpa

if mode == "canonical": smiles = "canonical" elif mode == "isomeric": smiles = "isomeric” elif mode == "absolute": smiles = "absolute" else: raise TypeError("unknown mode")

● Data Types/Structure● Control flow● File I/O● Modules

Reading Files

f = open(“names.txt")>>> f.readline()results

● Data Types/Structure● Control flow● File I/O● Modules

import math as mt

mt.functions…

import math

math.cos

from math import cos, pi

cos

from math import *

● Data Types/Structure● Control flow● File I/O● Modules● Class● NLTK

● Data Types/Structure● Control flow● File I/O● Modules● Class● NLTK

Recommended