introduction to scripting languagesucerd.com/my_uploads/course/processor_system/scripting.pdf ·...

31
Introduction to Scripting Languages Tassadaq Hussain Riphah International University Barcelona Supercomputing Center Universitat Politècnica de Catalunya

Upload: others

Post on 23-Jan-2020

91 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Introduction to Scripting Languages 

Tassadaq Hussain Riphah International University

Barcelona Supercomputing CenterUniversitat Politècnica de Catalunya

Page 2: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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.

Page 3: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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.

Page 4: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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.

Page 5: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Languages For Machine Learning & Data Science

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

Page 6: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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.

Page 7: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and
Page 8: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and
Page 9: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and
Page 10: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and
Page 11: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Development Environments

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

Page 12: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Python Keywords

Page 13: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 14: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Data Types/Structure

● Lists● Tuples● Set● Dictionary

Page 15: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 16: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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'

Page 17: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 18: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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);

Page 19: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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)

Page 20: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 21: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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.

Page 22: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 23: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

print x

Page 24: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Condition

● if continuation : print value

if gpa > 2 :

print gpa

Page 25: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 26: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 27: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

Reading Files

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

Page 28: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 29: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

import math as mt

mt.functions…

import math

math.cos

from math import cos, pi

cos

from math import *

Page 30: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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

Page 31: Introduction to Scripting Languagesucerd.com/my_uploads/course/processor_system/Scripting.pdf · Scripting Languages Scripting languages stress flexibility, rapid development and

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