Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari: mauro.gaspari@unibo.it



Similar documents
Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

CSCI 3136 Principles of Programming Languages

CS 40 Computing for the Web

COMP150: Practical Programming (in Python) Jeffrey Elkner Allen B. Downey Chris Meyers Brendan McCane Iain Hewson Nick Meek

What is a programming language?


1/20/2016 INTRODUCTION

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Computer Science 217

Welcome to Introduction to Computers and Programming Course using Python

Think Java. How to Think Like a Computer Scientist. Allen B. Downey 5.1.2

CS 51 Intro to CS. Art Lee. September 2, 2014

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

How to Think Like a Computer Scientist. Java Version

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

How to Think Like a Computer Scientist

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

Semester Review. CSC 301, Fall 2015

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Programming Languages

How to Think Like a Computer Scientist How to Think Like a Computer Scientist: Learning with Python 2nd Edition documentation

Levels of Programming Languages. Gerald Penn CSC 324

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Copyright 2012 Pearson Education, Inc. Chapter 1 INTRODUCTION TO COMPUTING AND ENGINEERING PROBLEM SOLVING

How to Think Like a Computer Scientist: Learning with Python Documentation

CSE 307: Principles of Programming Languages

Python Programming: An Introduction to Computer Science

Image credits:

Chapter 13: Program Development and Programming Languages

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Chapter 13: Program Development and Programming Languages

How to think like a computer scientist. Allen B. Downey

River Dell Regional School District. Computer Programming with Python Curriculum

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

OAMulator. Online One Address Machine emulator and OAMPL compiler.

Python Programming: An Introduction to Computer Science

Fall 2012 Q530. Programming for Cognitive Science

Let s put together a Manual Processor

Introduction to the course, Eclipse and Python

Administrative Issues

Computer Programming I & II*

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Introduction to programming

1.1 WHAT IS A PROGRAMMING LANGUAGE?

Beyond the Mouse A Short Course on Programming

The programming language C. sws1 1

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Chapter 12 Programming Concepts and Languages

Programming Languages & Tools

[Refer Slide Time: 05:10]

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

#820 Computer Programming 1A

Semantic Analysis: Types and Type Checking

CHAPTER 1 ENGINEERING PROBLEM SOLVING. Copyright 2013 Pearson Education, Inc.

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

3 SOFTWARE AND PROGRAMMING LANGUAGES

Fundamentals of Programming and Software Development Lesson Objectives

Language Processing Systems

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

Computational Mathematics with Python

Computer Programming I

Software: Systems and. Application Software. Software and Hardware. Types of Software. Software can represent 75% or more of the total cost of an IS.

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Algorithms are the threads that tie together most of the subfields of computer science.

Programming Languages CIS 443

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

High level code and machine code

Computer System: User s View. Computer System Components: High Level View. Input. Output. Computer. Computer System: Motherboard Level

Introduction to Computers and Programming

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Computer Programming

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

CSC230 Getting Starting in C. Tyler Bletsch

Programming Languages

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Compiler Construction

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

McGraw-Hill The McGraw-Hill Companies, Inc.,

CSE 130 Programming Language Principles & Paradigms

How to Think Like a Computer Scientist. Learning with Python

Lecture 9. Semantic Analysis Scoping and Symbol Table

Computational Mathematics with Python

Instructor Özgür ZEYDAN BEU Dept. of Enve. Eng. CIV 112 Computer Programming Lecture Notes (1)

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

CS 6371: Advanced Programming Languages

CA Compiler Construction

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Embedded Software development Process and Tools: Lesson-3 Host and Target Machines

Computers and Programs

Introduction to formal semantics -

Introduction to Software Paradigms & Procedural Programming Paradigm

CSC 2405: Computer Systems II

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

Transcription:

Computer Programming Course Details An Introduction to Computational Tools Prof. Mauro Gaspari: mauro.gaspari@unibo.it

Road map for today The skills that we would like you to acquire: to think like a computer scientist. Course organization. Exams. Course material. An introduction to computational tools. Getting started.

To think like a Computer Scientist... To understand computational tools and terminology. To use these basic tools to write small scale programs. To understand programs written by others. To understand both capabilities and limitations of computational tools. To map (qf) problems into computational solutions.

Python Computational solution will be implemented in Python a modern programming language. This course is not about Python (e.g. studying Python details), but it is about using it to think and build computational solutions. Warning!!! reading the textbook is not enough, exercises are fundamental.

Course Organization Interactive approach learning by doing Interactive Lectures: Exercises solved in class (workgroup sessions) Individual support (tutor: Dr. Giulio Pellitta giulio.pellitta2@unibo.it) Home assigments (warning home work is mandatory!!!)

Course Material TextBook: How to Think Like a Computer Scientist: Learning with Python, by Allen Downey, Jeff Elkner and Chris Meyers. Gree Tea Press (available online here: http://thinkpython.com/). Web site: http://www.cs.unibo.it/~gaspari/www/teaching/ Slides Readings and Links Online software and documentation: Python site: http://www.python.org/ There is an Italian version too: http://www.python.it/

Exam The Exam of Computer Programming is organized in two sequential steps: A PROGRAMMING TEST (yes/no) A DISCUSSION OF A PROJECT Passing the PROGRAMMING TEST is mandatory for the PROJECT assignment and thus for starting the second step. Home work assignments are also evaluated: They are assigned at the end of each week (Thursday); They must be completed and sent by email to mauro.gaspari@unibo.it and giulio.pellitta2@unibo.it before the first lesson of the next week (11am Wednesday).

Exam: discussion of a (qf) project Procedure: Discussion: Set up a group (2 or 3 students for each group). Propose a (qf) problem and submit a specification of what the program will do. When approved implement a computational solution. Group discussion (problem and solution) => GroupVote Individual verifications for each student (s) in the group => Decrement(s) Individual home work assignment ( 1 penalty for each home work not completed) for each student (s) => HWP(s) Vote(s) = GroupVote Decrement(s) HWP(s)

Computational tools and problem solving Computational tools are used for problem solving. Humans are problem solvers: we use knowledge to solve problems. What is knowledge? We can divide knowledge into at least two categories: Declarative knowledge Imperative knowledge

Example: Greatest Common Divisor DECLARATIVE KNOWLEDGE: A, B are positive integer, X = GCD(A,B) then: The remainder of both A/X and B/X is equal to 0 and, If exists Y X such that the remainder of both A/Y and B/Y is equal to 0 then X > Y IMPERATIVE KNOWLEDGE (EUCLID) IF A<B, exchange A and B. Divide A by B and get the remainder, R. If R=0, GCD(A,B) = B. Replace A by B and replace B by R. Return to the previous step. IMPERATIVE KNOWLEDE IS THE RECIPE!

What is a Computation? A computation is a recipe: a sequence of (simple) instructions (this is also called program). Is it possible to build a mechanical process to capture that set of computations? Yes, for example building a simple circuit to do this: fixed-program computer (for example a dishwasher program). General purpose computer are more complex, they are circuit able to take a recipe (list of simple instructions) as an input, reconfigure themselves and act as the recipe.

How they work? Registers IR CU PC Program instr1 instr2...... instrn PC ALU R A M Capacity in Bytes (8 bit) Von Neumann Machine CPU MEMORY BUS Op code. op1 op2 ris Speed in Hertz Instructions How many instructions in a second

What is a program? Programs are written using programming languages. Source code: instructions of a given program. Program Tombstone Diagrams A program P written In a Language L P L Source code of a program P written In language L is usually stored in file and loaded in memory............

Which Programming Languages? High level Languages Python, C, C++, Perl, Java, Lisp, Prolog. Thus there is also a low level Machine language Assembler

High-level vs low-level High-level Languages... print b*h/2... Low-level Languages LOAD r1,b LOAD r2,h MUL r1,r2 DIV r1,#2 RET 00010010010001010010 01001110110010101101 001.. Assembler Machine Language

Machine Language Computer are only able to execute machine language instructions. Different computers may have different machine languages. Computer Tombstone Diagram A computer having a machine language M is represented with the following tombstone diagram M

Program execution A computer can only execute programs written in its machine language P M M

Problems of machine languages Write programs in machine language is almost impossible. The first computers were programmed in this way. Now low level languages are used only for a few specialized applications. Almost all programs are written in high level programming languages. Rapid development Portable code

However Apparently high level languages cannot be directly executed on computers. A program P written in L cannot be executed on a machine M. P L M

Programming Language Processors Specific tools to support the execution of highlevel programming languages: Interpreters. Compilers.

Interpreter An interpreter for a language L is a program that takes as input a program written in L and executes it instraction by instraction. An interpreter can be written in machine language. Interpreter Tombstone Diagram An interpreter for a language L written in M. L M

Executing Python programs The python interpreter allows a user to execute a program P written in Python in computer M. This means that Python an interpreted language. P Python Python M M

Compilers A compiler is a program which translates source code (usually written in a given high level language) into object code usually written in machine language. Compiler Tombstone Diagram A compiler which translates source code written in a langusge L into object code written in machine language M. L -> M M

Using a compiler Source code P L L -> M P M Executable object code Note that a compiler is a program, thus it must be executed on the right machine as other programs are. M M M

Instructions input: Get data from the keyboard, a file, or some other device. output: Display data on the screen or send data to a file or other device. math: Perform basic mathematical operations like addition and multiplication. conditional execution: Check for certain conditions and execute the appropriate code. repetition: Perform some action repeatedly, usually with some variation. Programming = the process of breaking a complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions.

Bugs and Debugging! Bugs Debugging (the process of tracking down bugs) Why we introduce this concept here? Is an essential skill associated to computer programming. In principle all the programs may have bugs! Bugs are frequent in programming, but they can be solved with debugging!

Different kinds of errors Syntax errors: Python can only execute a program if the syntax is correct; otherwise, the interpreter displays an error message. Syntax refers to the structure of a program and the rules about that structure. If there is a single syntax error anywhere in your program, Python will display an error message and quit. Runtime errors: these errors do not appear until after the program has started running. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened. Semantic errors: If there is a semantic error in your program, it will run successfully (without error messages), but it will not do the right thing. It will do something else. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be very tricky.

Debugging Debugging is like detective work: you are confronted with clues, and you have to infer the processes and events that led to the results you see. Experimental science; you have an idea about what is going wrong, you modify your program and try again. If your hypothesis was correct, then you can predict the result of the modification, and you take a step closer to a working program. If your hypothesis was wrong, you have to come up with a new one. Sherlock Holmes When you have eliminated the impossible, whatever remains, however improbable, must be the truth. Programming => debugging: programming is the process of gradually debugging a program until it does what you want.

Formal languages Natural languages are the languages people speak, such as English, Spanish, and French. They were not designed by people they evolved naturally. Formal languages are languages that are designed by people for specific applications. For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Programming languages are formal languages that have been designed to express computations.

Syntax Formal languages tend to have strict rules about syntax. For example, 3 + 3 = 6 is a syntactically correct mathematical statement, but 3+ = 3$6 is not. Two levels of syntax rules: Tokens are the basic elements of the language, such as words, numbers, and chemical elements. One of the problems with 3+ = 3$6 is that $ is not a legal token in mathematics. Structure of a statement: the way the tokens are arranged. The statement 3+ = 3 is illegal because even though + and = are legal tokens, you can t have one right after the other. Parsing: is the process that analyse a sentence finding and understanding its structure.

Formal Languages vs Natural Languages Although formal and natural languages have many features in common tokens, structure, syntax, and semantics there are some differences: ambiguity: Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous. redundancy: In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise. literalness: Natural languages are full of idiom and metaphor. Formal languages mean exactly what they say.

Semantics Semantics: concerns the meaning of instructions, thus the meaning of the constructs of the language. Semantics of programming languages: Informal semantics (manual). Formal semantics (computer scientists): useful for building programming languages processors.

Getting Started Python installation http://www.python.org/download/ you should install the last 2.7 version The last version of python is 3.3. and it is slightly different from version 2.7. The most significant differences are reported in the textbook. Python interpreter Python IDE: include a text editor specific for programming and debugging tools.

The python interpreter Python is an interpreted language. There are two ways to use the interpreter: Interactive mode: you type Python programs and the interpreter displays the result: >>> 1 + 1 2 Script mode: you can store code in a file and use the interpreter to execute the contents of the file (script). By convention, Python scripts have names that end with.py.

Script execution To execute the script, you have to tell the interpreter the name of the file. If you have a script named hello.py alond you are working in a UNIX command window, you type: python hello.py In other development environments, the details of executing scripts are different. Environment based on graphical user interfaces usually have a specific run botton or menu item to execute the script. You can find instructions for your environment at the Python website http://python.org

The first program Traditionally, the first program you write in a new language is called Hello, World! because it just prints this sentence. In Python, the implementation is very simple: print 'Hello, World!' This is an example of a print statement, which displays a value on the screen (output statement). The quotation marks in the program mark the beginning and end of the text to be displayed.

Exercises Try the interpreter. Run the Hello World program both in the interpreter and as a script.