Types of Scripting Language

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 9

Types in Scripting Languages

CS 351 – Programming Paradigms


Data Types in Python
 Scripting languages don’t generally require or allow the
declaration of types ofr variables.
 Most perform a series of run-time checks to make sure
that values are used in an appropriate way.
 Python is strongly typed with operations enforced at
run-time.
 The type system is Python is known as ``duck typing’’.
 This informally allows the type of a value to be
determined via the following rule:
 “If looks like a duck and quacks like a duck, it is a duck”
Data Types in Python
 Python uses the reference model for its types.
 Some built-in types in Python include:

Type Kind Syntax Example


str String ‘hello‘
list Sequence [4.0, 'string', True]
tuple Sequence (4.0, 'string', True)
set Set set([4.0, 'string', True])
dict Mapping {'key1': 1.0, 'key2': False}
int Integer 42
float Number 3.1415927
Examples
 s = “hello”
 r = 56
 l = [1,2,3]
 t = (56,78,54)
 dict = { ‘one’ : 1, ‘two’:2 }

 We can just initialise the types and the interpreter


takes care of the rest.
Lists in Python
 Lists can form the basic blocks of most complex data
structures in Python.
 l = [1,2,3,4]
 l.append(5)
 print l # prints [1,2,3,4,5]
 l.pop
 print l # prints [1,2,3,4]
 l.pop(0)
 print l # prints [2,3,4]
Lists and the filter and map
functions
 We can define an arbitrary function
 def f(x): return x%2==0
 filter ( f, range ( 2, 100 ) )

 Defining another function:


 def g(x): return x+4
 map ( g, range (4, 16) )

 What are doing here?


 What paradigm is this?
Object Orientation
 Python is explicitly object-oriented.
 To define a class in Python we use the following
syntax:

class myclass:
“This class shows Python Syntax”
intval = 12345
def printsomething(self):
return “hello world”

 We can create objects simply:


x = myclass()
print x.printsomething()
Object Orientation
 The constructor for a Python class has special syntax.

class myclass:
def __init__ (self, list ) :
self.data = list # assign list to data

x = myclass ( [1,2,3,4,5] )
print x.data # what is printed?
Object Orientation
class Shape:
def __init__ (self) :
self.name = “Shape”

def printName (self) :


return self.name

def getArea (self ) :


return 0

You might also like