computer science 1md3 introduction to programming
DESCRIPTION
Winter 2014. Computer Science 1MD3 Introduction to Programming. About the questions. I would like to answer your questions about python which you cannot understand well or some programs questions related to 1MD3. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/1.jpg)
S
Computer Science 1MD3
Introduction to Programming
Winter 2014
![Page 2: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/2.jpg)
About the questions
I would like to answer your questions about python which you cannot understand well or some programs questions related to 1MD3.
You can ask me your questions about the assignments, for example, you cannot understand the assignment questions themselves, or if you need some help when you are doing the python programs.
![Page 3: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/3.jpg)
How to get my help
Email me and describe your questions in your email and I will reply to you as soon as possible when I read your email. But you need describe your questions clearly. Gmail Chat ? Maybe Not a good way to explain program……
Meet with me. If you hope to meet with me and ask the questions, you are welcome! But before that, please email me and we may know when is the available time for us, we can make sure the meeting time.
My email: [email protected] or [email protected]
My office: ITB 206
You can also contact other TA to get help. Other TA’s email, please see the website of 1MD3
![Page 4: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/4.jpg)
Assignment 1
Due date: by Thu. Jan 30 23:00:00 2014
![Page 5: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/5.jpg)
Assignment 2
Due date: by Wed Feb 26 23:00:00 2014
![Page 6: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/6.jpg)
Introduction
Syntax and semantics of programming language
Syntax and semantics of python Assignment statements Conditional statements Loop statements Function
Exception (related to file I/O and object)
Assignment: Main points of Assignment 1 Main points of Assignment 2
![Page 7: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/7.jpg)
Language and programming language
Language: [1]Language provides a means of communication by sound and writtensymbols. Human beings learn language as a consequence of their lifeexperiences.
Natural language:[1] We communicate our thoughts and feelings Sometimes, it’s not very exactly in using science
Programming language:[1] Can be viewed as artificial languages defined by men and women
initially for the purpose of communicating with computers but, as importantly, for communicating algorithms among people.
![Page 8: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/8.jpg)
Syntax of programming language
Syntax refers to the ways symbols may be combined to create well-formed sentences (or programs) in the language. [1]
Syntax defines the formal relations between the constituents of a language, thereby providing a structural description of the various expressions that make up legal strings in the language. [1]
Syntax deals solely with the form and structure of symbols in a language without any consideration given to their meaning.[1]
![Page 9: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/9.jpg)
Semantics of programming language
Semantics reveals the meaning of syntactically valid strings in a language.[1]
For natural languages, this means correlating sentences and phrases with the objects, thoughts, and feelings of our experiences. [1]
For programming languages, semantics describes the behavior that a computer follows when executing a program in the language. [1]
We might disclose this behavior by describing the relationship between the input and output of a program or by a step-by-step explanation of how a program will execute on a real or an abstract machine. [1]
![Page 10: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/10.jpg)
Program
Program: A computer program, or just a program, is a sequence
of instructions, written to perform a specified task with a computer. [2]
Software: A collection of computer programs and related data is
referred to as the software. [2]
Math + logic + English (we need algorithms in programs)
![Page 11: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/11.jpg)
www.michaelliut.ca/cs1md3
Programming Languages
How many programming Languages exists?
Parts of them are here:
http://en.wikipedia.org/wiki/List_of_programming_languages
You may know or will study in future:
C, Java, C++, Haskell, Ruby, Pascal, Visual Basic, Object Pascal(Delphi), …
![Page 12: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/12.jpg)
If Statements
Boolean Boolean value: true or false Boolean expression
if condition: print (……) elif condition: print (……) else
left associative : 5**2 **3 = (5**2)**3 Right associative Elif a+b+c ==a+b
![Page 13: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/13.jpg)
If Statements
This Flow Diagram is taken from [3]
![Page 14: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/14.jpg)
If statement
If examples
i1 = 1if i1: print("i1 if branch")else: print ("i1 else branch")i2 = 0if i2: print ("i2 if branch") else: print ("i2 else branch") print ("finish the program")
![Page 15: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/15.jpg)
Main points of assignment1
If..elif If a o b o c == (a o b) o c left associative If a o b o c == a o (b o c) right associative
We need “import calendar” from Eval (input(“”) to get the input year By calendar.month(year, month) to get the
month calendar
![Page 16: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/16.jpg)
• This Flow Diagram is taken from [3]
Loop
![Page 17: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/17.jpg)
Loop
Loop Type Description[3]
while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loopsYou can use one or more loop inside any another while, for or do..while loop.
![Page 18: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/18.jpg)
While Loop
This Flow Diagram is taken from [3]
![Page 19: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/19.jpg)
While Loop
While example
i=3while i>0: print("this is a positive number") print(i) i=i-1print("finish the while loop, i equal to 0 now")print(i)
![Page 20: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/20.jpg)
For Loop
This Flow Diagram is taken from [3]
![Page 21: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/21.jpg)
For Loop
for example
for i in range(3, 0, -1):
print("this is a positive number") print(i) print("finish the while loop")print(i)
![Page 22: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/22.jpg)
Main points of assignment1
Using while loop or for loop
% modulo operator: find the division remainder of a number
s =s + v
![Page 23: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/23.jpg)
Function
Why do we need functions in python?
Syntax of function
.
![Page 24: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/24.jpg)
Function
Function syntax
Syntax:def name( parameters ): function_body return [expression]
![Page 25: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/25.jpg)
Function
The example adapted from [3]
Function definition and call
def appendlist( l ): l.append(4); return
# call functionl = [1,2,3];appendlist( l );print (l)
![Page 26: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/26.jpg)
Exception
What is Exception? An exception is an event, which occurs during the execution of a program,
that disrupts the normal flow of the program's instructions.[3] When a Python script encounters a situation that it can't cope with, it raises
an exception. An exception is a Python object that represents an error.[3] When a Python script raises an exception, it must either handle the
exception immediately otherwise it would terminate and come out. [3]
Handling an exception: If you have some suspicious code that may raise an exception, you can
defend your program by placing the suspicious code in a try: block. [3] After the try: block, include an except: statement, followed by a block of
code which handles the problem as elegantly as possible.[3]
![Page 27: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/27.jpg)
Exception
The example adapted from [3]
Exception
try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!")except IOError: print ("Error: can\'t find file or read data")else: print ("Written content in the file successfully") fh.close()
![Page 28: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/28.jpg)
Exception
It related to class and object.
Detail about Class and object will be explained next time!
![Page 29: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/29.jpg)
Main points of Assignment 2
Input stored in a list
Judge the input format
get the date, month and year from the list
Judge the year, month and date is OK or not
print the right result
![Page 30: Computer Science 1MD3 Introduction to Programming](https://reader036.vdocuments.site/reader036/viewer/2022062721/568137ea550346895d9f9dd1/html5/thumbnails/30.jpg)
Reference
[1] http://homepage.cs.uiowa.edu/~slonnegr/plf/Book/Chapter1.pdf
[2] http://en.wikipedia.org/wiki/Computer_program
[3] http://www.tutorialspoint.com/python/