21
Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input and output Lectures 7-8 Getting going with python in Linux Using python in Linux Object orientated programming

Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Lecture 7-8: Python ●Lectures 1-6: introduction to Linux

● Using the command line● Bash scripting● Conditional statements● Loops● Input and output

●Lectures 7-8 Getting going with python in Linux● Using python in Linux● Object orientated programming

Page 2: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Python

● Python is and interpreted language– The script (text document) is interpreted line by line as it is run

InterpreterText file Machine code

Python interpreters are available for Windows,Linux, OSX,android....

Page 3: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

#!/usr/bin/env python

Adding the line “#!/usr/bin/env python”Identifies this as a python script

This script can then be run the same as a bash script

Page 4: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Python interpreter in LinuxScript (text document)

Running the script with python

Stdin, stdout and stderr can be used the same as a bash script

Page 5: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Computing paradigms(The basic structure of thought underlying the programming activity)

Page 6: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Characteristics of OOP

● Discipline and idea– The theory of concepts, and models of human interaction with real world phenomena

● Data as well as operations are encapsulated in objects● Information hiding is used to protect internal properties of an object● Objects interact by means of message passing

– A metaphor for applying an operation on an object

● In most object-oriented languages objects are grouped in classes– Objects in classes are similar enough to allow programming of the classes, as

opposed to programming of the individual objects

– Classes represent concepts whereas objects represent phenomena

● Classes are organized in inheritance hierarchies– Provides for class extension or specialization

Page 7: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

What is an object?

Page 8: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

What is an object?

Page 9: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

An object is a member of a class, so what is a class?

Page 10: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

An object is a member of a class, so what is a class?

Class variable

Class Name

Documentation string

The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.

You declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you don't need to include it when you call the methods.

Page 11: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Creating instances of objects

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.

Page 12: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Accessing object properties using the ”.” operator

Page 13: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Alternative method for accessing object attributes

Page 14: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Built in class attributes

__dict__ : Dictionary containing the class's namespace.

__doc__ : Class documentation string or None if undefined.

__name__: Class name.

__module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.

__bases__ : Contains the base classes, in the order of their occurrence in the base class list.

Page 15: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Built in attributes

Page 16: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Inheritance

Page 17: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Derived class

You can create a class by deriving it from a pre existing class

The child class inherits the attributes of its parent class

Page 18: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Inheritance example

Output from the code:

Page 19: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Exercise 1 PolygonsThis script will draw a triangle using the module turtle

Write a class that store the properties of a regular polygon (size,angle,sides). The class should contain a function that will draw the object

Page 20: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Exercise 2 Koch snowflake

This Koch snowflake was discovered in 1904 by Swedish mathematician Helge von Koch

1)divide the line segment into three segments of equal length.2)draw an equilateral triangle that has the middle segment from step 1 as its base and points

outward.3)remove the line segment that is the base of the triangle from step 2.

Fig 1

Fig 2

Page 21: Lecture 7-8: Python - Trinity College Dublin … · Lecture 7-8: Python Lectures 1-6: introduction to Linux Using the command line Bash scripting Conditional statements Loops Input

Exercise 2 continued

a) Write a function using turtle that draws the path A-B-C-D-E from Figure 2.

b) Write a function that draws the second order Kosh curve by calling the function written in 2a.

c) Write a function that draws a Kosh curve to an arbitrary nth order by calling itself.

d) Write a class that store the properties of a Koch snowflake (size,angle,sides). The class should contain a function that will draw the object