1 / 9

Types in Scripting Languages

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.

Télécharger la présentation

Types in Scripting Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Types in Scripting Languages CS 351 – Programming Paradigms

  2. 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”

  3. 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

  4. 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.

  5. 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]

  6. 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?

  7. 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()

  8. 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?

  9. Object Orientation class Shape: def __init__ (self) : self.name = “Shape” def printName (self) : return self.name def getArea (self ) : return 0

More Related