Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.

Slides:



Advertisements
Similar presentations
More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Advertisements

Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Chapter 19, 20 Object Oriented Programming (OOP) Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Classes, methods, and conditional statements We’re past the basics. These are the roots.
C++ fundamentals.
Introduction to Methods
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Programming Languages: Telling the Computers What to Do Chapter 16.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Python
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
C Language: Introduction
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Programming in Java CSCI-2220 Object Oriented Programming.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
By Mr. Muhammad Pervez Akhtar
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Lesson 06: Functions Class Chat: Attendance: Participation
Writing Functions( ) (Part 4)
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Introduction to Computer Science
CISC101 Reminders Assignment 3 due today.
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011

Outline File I/O Function Modules Object Oriented Programming (OOP)

Files File is also one of built-in type in Python The built-in OPEN function creates a Python file object, which servers as a link to a file After calling open, you can read or write the associated external file

Common FILE operations INPUT Input=open(‘file_name.txt’, ‘r’) Input.readlines() Input.close()

Split() function >>> string= ‘a b c d e’ >>> string.split() ['a', 'b', 'c', 'd', 'e']

Let’s try to read in a file!! input=open('file.txt','r') all=[] for line in input.readlines(): temp=line.split() all.append(temp) input.close()

File in Action Then we can calculate the average score for each student

File in Action for i in range(len(all)): sum=0.0 for j in range(1,len(all[i])): sum=sum+int(all[i][j]) average=sum/5 print average

File in Action Output Output=open(‘file_name.txt’, ‘w’) Output.write() Output.close()

File in Action input=open('file.txt','r') output=open('out_file.txt','w') all=[] for line in input.readlines(): temp=line.split() all.append(temp) for i in range(len(all)): sum=0.0 for j in range(1,len(all[i])): sum=sum+int(all[i][j]) average=sum/5 print average output.write(str(average)+'\n') input.close() output.close()

Outline File I/O Function Modules Object Oriented Programming (OOP)

Function In simple terms, a function is a device that groups a set of statements, so they can be run more than once in a program

Why Function? Functions serve two primary development roles: 1. Code Reuse: Functions allows us to group and generalize code to be used arbitrarily many times after it is defined. 2. Procedure decomposition: Functions also provide a tool for splitting systems into pieces---one function for each subtask.

“def” statement The def statement creates a function object and assigns it to a name. the def general format: def ( ): the statement block becomes the function’s body---the code Python executes each time the function is called

Function Example def star(): num=int(raw_input("please input a number")) for i in range(num+1): print '*' * i star() # we call “star” function once

“def” statement Now we try to see the def format with arguments: def (arg1, arg2, …, argN):

Function Example with 2 argument def multiply(x,y): value=x*y print value multiply(4,10) multiply(3,6) multiply(2,18)

Function Example with 3 argument def multiply(x,y,z): value=x*y*z print value multiply(4,10,1) #generate result 40 multiply(4,10) #error message: multiply() takes exactly 3 arguments (2 given)

“def” statement Now we try to see the def format with arguments and return function: def (arg1, arg2, …, argN): … return

Function Example def times(x,y): value=x*y return value aa = times(2,4) #aa=8 aa = times(3.14,4)#aa=12.56 aa = times(“ha”,4)#aa=“hahahaha” list=[1,2,3,4] aa = times(list,2) #aa=[1,2,3,4,1,2,3,4]

Outline File I/O Function Modules Object Oriented Programming (OOP)

Modules Modules are the highest level program organization unit, which packages program codes and data for reuse Actually, each “file” is a module (Look into Lib in Python24)

Modules Example >>> import random >>> random.choice(range(100)) 65 >>> random.choice(range(100)) 96 >>> random.choice(range(100)) 15

Why Use Modules? Modules provide an easy way to organize components into a system, by serving as packages of names Modules have at least three roles: 1. Code Reuse 2. Implementing shared services or data 3. Make everything “lives” in a Module

Module Creation To define a module, use your text editor to type Python code into a text file You may create some functions or variables in that file You can call modules anything, but module filenames should end in.py suffix

Modules Usage Clients can use the module file we just wrote by running “import” statement >>> import math >>> import random >>> import module1 # assume this is the file name we saved

Modules examples We create a file name module1: def print_out(aa): print aa*3

Modules Example now we write another file named module2 import module1 module1.print_out(“Hello World!! ”) # Use module1’s function The result of execute this program is: Hello World!! Hello World!! Hello World!!

Modules You may also use variables in a Module For example: >>> import math >>> math.pi

Outline File I/O Function Modules Object Oriented Programming (OOP)

What is OOP To qualify as being truly object-oriented objects generally need to also participate in something called an inheritance hierarchy Inheritance --- a mechanism of code customization and reuse, above and beyond anything we’ve seen so far

Class tree Two key words need to define: 1. Classes Serve as instance factory 2. Instances Represent the product generate from classes

Class tree

We usually call class higher in the tree (like c2 and c3) superclasses; classes lower in the tree (like c1) are known as subclasses The search procedure (try to look up some specific function belongs to which class) proceeds bottom-up, starting from left to right

Class tree Suppose we build up the tree If we say: I2.w It indicates we have an instance2 and it calls the function w now, we need to search where does the function w defined Therefore, the Python will search the linked objects in the order: I2, C1, C2, C3 and stop at the first.w it finds In OOP, I2 “inherits” w from C3

Class tree I1.x and I2.x both find x in C1 and stop, since C1 is lower than C2 I1.y and I2.y both find y in C1, since that’s the only y I1.z and I2.z both find z in C2, since C2 is more to the left than C3 I2.name finds name in I2, without climbing the tree at all

Class vs. Modules All of the class and instance objects we put in these trees are just package of names. If that sounds like modules, it should; The only difference here is that objects in class tree also have “automatically- search” links to other namespace objects.

Coding the Class Tree Each “class” statement generates a new class object Each time a class is called, it generates a new “instance” object Instances are automatically linked to the class they are created from Classes are linked to their superclasses

Coding the Class Tree To build the class tree we just saw, we would run Python code in this form: class C2: … class C3: … class C1 (C2,C3): … I1=C1() I2=C2()

A First Example

Class Tree

A Second Example

Class Tree

Python Official Sites Python Tutorial Python Official Site Download Python Latest Version