Computer Science 101 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 1: Introduction
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
COMP 4—Power Tools for the Mind1 PowerTools What’s in the Box? Turing 1: An Introduction to Programming You will learn elementary computer programming.
Chapter 2: Input, Processing, and Output
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Introduction to a Programming Environment
Introduction to C Programming
Pseudocode.
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Fundamentals of Python: First Programs
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to Programming with RAPTOR
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CISC105 – General Computer Science Class 2 – 6/7/2006.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Chapter 1: Introduction to Computers and Programming.
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Chapter 1: Introduction to computers and C++ Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lecture 1 Introduction Richard Gesick.
Revision Lecture
Variables, Expressions, and IO
Introduction to C++ Programming
CS111 Computer Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lecture3.
Class 2.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Introduction to C Programming
Programming for Business Computing Introduction
Presentation transcript:

Computer Science 101 Introduction to Programming

What Is a Program? Usually, one or more algorithms written in a programming language that can be translated to run on a real machine Usually, one or more algorithms written in a programming language that can be translated to run on a real machine We sometimes call programs software We sometimes call programs software

What Is a Programming Language? A programming language is somewhat like a natural language, but with a very limited set of statements and strict syntax rules. A programming language is somewhat like a natural language, but with a very limited set of statements and strict syntax rules. Has statements to implement sequential, conditional and iterative processing - algorithms Has statements to implement sequential, conditional and iterative processing - algorithms Examples: FORTRAN, COBOL, Lisp, Basic, Pascal, C, C ++, Java, C#, Python, … Examples: FORTRAN, COBOL, Lisp, Basic, Pascal, C, C ++, Java, C#, Python, …

Compiler A compiler is a program that converts a program written in a programming language into a program in the native language, called machine language, of the machine that is to execute the program. A compiler is a program that converts a program written in a programming language into a program in the native language, called machine language, of the machine that is to execute the program.

From Algorithms to Hardware (with compiler) Algorithm Program A real computer Translate (by a human being) Translate (by compiler program)

The Program Development Process (Data Flow) Editor Compiler A real computer Algorithm Program in programming language Program in machine’s language InputOutput

The Program Development Process (Control Flow) Edit Compile Run Syntax errors InputOutput Runtime errors

Three kinds of errors Syntax error : Some statement in the program is not a legal statement in the language. Syntax error : Some statement in the program is not a legal statement in the language. Runtime error : An error occurs while the program is executing, causing the program to terminate (divide by zero, etc.) Runtime error : An error occurs while the program is executing, causing the program to terminate (divide by zero, etc.) Logic error : The program executes to completion, but gives incorrect results. Logic error : The program executes to completion, but gives incorrect results.

Interpreter An alternative to a compiler is a program called an interpreter. Rather than convert our program to the language of the computer, the interpreter takes our program one statement at a time and executes a corresponding set of machine instructions. An alternative to a compiler is a program called an interpreter. Rather than convert our program to the language of the computer, the interpreter takes our program one statement at a time and executes a corresponding set of machine instructions.

Interpreter Edit Interpreter Syntax or runtime errors Input Output

Python Python is a real-world, production language that is freely available for most computers. Python is a real-world, production language that is freely available for most computers. If you want a copy of Python to use with this course, go to We are using JES (Jython Environment for Students) which has a lot of special multimedia functionality. If you want a copy of Python to use with this course, go to We are using JES (Jython Environment for Students) which has a lot of special multimedia functionality. Note: Our textbook covers a limited amount of Python. There are many excellent online tutorials. For example, see Note: Our textbook covers a limited amount of Python. There are many excellent online tutorials. For example, see

Python Python uses an interpreter. Not only can we write complete programs, we can work with the interpreter in a statement by statement mode enabling us to experiment quite easily. Python uses an interpreter. Not only can we write complete programs, we can work with the interpreter in a statement by statement mode enabling us to experiment quite easily. Python is especially good for our purposes in that it does not have a lot of “overhead” before getting started. Python is especially good for our purposes in that it does not have a lot of “overhead” before getting started. It is easy to jump in and experiment with Python in an interactive fashion. It is easy to jump in and experiment with Python in an interactive fashion.

Language terminology Syntax: The formal rules for legal statements in the language. Syntax: The formal rules for legal statements in the language. Semantics: The meaning of the statements - what happens when the statement is executed. Semantics: The meaning of the statements - what happens when the statement is executed.

Three major control constructs of programming (Execution flow of instructions) Sequential: Simply do steps one after the other in order they are listed. Sequential: Simply do steps one after the other in order they are listed. Conditional: Decide which statement to do next based on some true/false test. Conditional: Decide which statement to do next based on some true/false test. Iterative: A set of statements is repeated over and over until some condition is met. Iterative: A set of statements is repeated over and over until some condition is met.

Sequential Operations “Atomic” Input Input Computation Computation Output Output

The Big Plan We want to get some experience of programming simple algorithms in a real programming language. This gives us an understanding of how software is written and allows us to test our algorithms to see if they work. We want to get some experience of programming simple algorithms in a real programming language. This gives us an understanding of how software is written and allows us to test our algorithms to see if they work. We’ll first work with programs where the variables have numbers as values. We’ll first work with programs where the variables have numbers as values. Later we’ll work with programs dealing with pictures and sound. Later we’ll work with programs dealing with pictures and sound. In lab we’ll work with some simple statements and small programs. In lab we’ll work with some simple statements and small programs.

The Basic Pattern Most of our programs will use the basic pattern of Most of our programs will use the basic pattern of Get some user inputGet some user input Perform some algorithm on the inputPerform some algorithm on the input Provide results as outputProvide results as output

Identifiers Identifiers are names of various program elements in the code that uniquely identify the elements. They are the names of things like variables or functions to be performed. They're specified by the programmer and should have names that indicate their purpose. Identifiers are names of various program elements in the code that uniquely identify the elements. They are the names of things like variables or functions to be performed. They're specified by the programmer and should have names that indicate their purpose. In Python, identifiers In Python, identifiers Are made of letters, digits and underscoresAre made of letters, digits and underscores Must begin with a letter or an underscoreMust begin with a letter or an underscore Examples: temperature, myPayrate, score2Examples: temperature, myPayrate, score2

Keywords Keywords are reserved words that have special meaning in the Python language. Because they are reserved, they can not be used as identifiers. Examples of keywords are if, while, class, import. Keywords are reserved words that have special meaning in the Python language. Because they are reserved, they can not be used as identifiers. Examples of keywords are if, while, class, import.

Variables in Python A variable has A variable has A name – identifier A name – identifier A data type - int, float, str, etc. A data type - int, float, str, etc. Storage space sufficient for the type. Storage space sufficient for the type.

Numeric Data Types int This type is for whole numbers, positive or negative. Examples: 23, int This type is for whole numbers, positive or negative. Examples: 23, float This type is for numbers with possible fraction parts. Examples: 23.0, float This type is for numbers with possible fraction parts. Examples: 23.0,

Integer operators The operations for integers are: The operations for integers are: + for addition + for addition - for subtraction - for subtraction * for multiplication * for multiplication / for integer division: The result of 14/5 is 2 / for integer division: The result of 14/5 is 2 % for remainder: The result of 14 % 5 is 4 % for remainder: The result of 14 % 5 is 4 *, /, % take precedence over +, - *, /, % take precedence over +, - x + y * z will do y*z first x + y * z will do y*z first Use parentheses to dictate order you want. Use parentheses to dictate order you want. (x+y) * z will do x+y first. (x+y) * z will do x+y first.

Integer Expressions Integer expressions are formed using Integer expressions are formed using Integer Constants Integer Constants Integer Variables Integer Variables Integer Operators Integer Operators Parentheses Parentheses

Python Assignment Statements In Python, = is called the assignment operator and an assignment statement has the form = In Python, = is called the assignment operator and an assignment statement has the form = Here Here would be replaced by an actual variable would be replaced by an actual variable would be replaced by an expression would be replaced by an expression Python: age = 19 Python: age = 19

Python Assignment Statement Syntax: = Syntax: = Note that variable is on leftNote that variable is on left Semantics: Semantics: Compute value of expression Compute value of expression Store this as new value of the variable Store this as new value of the variable Example: Pay = PayRate * Hours Example: Pay = PayRate * Hours Payrate 10 Hours 40 Pay 400

Assignment Example Before X3X3 Z 12 Y5Y5 After X3X3 Z 11 Y5Y5 Execute Z = X * 3 + Z / Y

Python Session

What about floats? When computing with floats, / will indicate regular division with fractional results. When computing with floats, / will indicate regular division with fractional results. Constants will have a decimal point. Constants will have a decimal point. 14.0/5.0 will give 2.8 while 14/5 gives /5.0 will give 2.8 while 14/5 gives 2.

Comments Often we want to put some documentation in our program. These are comments for explanation, but not executed by the computer. Often we want to put some documentation in our program. These are comments for explanation, but not executed by the computer. If we have # anywhere on a line, everything following this on the line is a comment – ignored If we have # anywhere on a line, everything following this on the line is a comment – ignored

Numerical Input To get numerical input from the user, we use an assignment statement of the form = input( ) To get numerical input from the user, we use an assignment statement of the form = input( ) Here Here would be replaced by a prompt for the user inside quotation marks would be replaced by a prompt for the user inside quotation marks If there is no prompt, the parentheses are still neededIf there is no prompt, the parentheses are still needed Semantics Semantics The prompt will be displayedThe prompt will be displayed User enters numberUser enters number Value entered is stored as the value of the variableValue entered is stored as the value of the variable

Print Statement For output we use statements of the form print For output we use statements of the form print Semantics Semantics Value of expression is computedValue of expression is computed This value is displayedThis value is displayed Several expressions can be printed – separate them by commas Several expressions can be printed – separate them by commas

Example - Fahrenheit to Centigrade We want to convert a Fahrenheit temperature to Centigrade. We want to convert a Fahrenheit temperature to Centigrade. The formula is C = (F -32) x 5/9 The formula is C = (F -32) x 5/9 We use type float for the temperatures. We use type float for the temperatures.

Python Session