What is a program

Download Report

Transcript What is a program

1
Class # 2: Introduction to programming using Python
Course: Introduction to Computers and Programming using
Python
V22.0002, Section 3, Spring 2010
Professor: Sana Odeh
[email protected]
Office hours:
Mondays 2:00 pm - 4 pm, room 321 in Courant (this building), or at other
times by appointment
Course website:
http://www.cs.nyu.edu/courses/spring10/V22.0002003/index.html
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
2
Introduction
• • What are Programming Languages
• Introductions to Python Programming basics
•Introduce the basic structure of Python program
• How to use python software (Idle)
•How to design/create/edit/execute/run and debug
our Python programs
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
3
What is a program
• Program is a set of instructions that performs
calculations/computation
• You can also think of programs as software that
perform specific tasks allowing you to do the
following:
• Buy products on the web
• Type reports using MS Word
• Chat with friends using the iphone
• Post an album on FaceBook
• Listen to music using your ipod
• Play games
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
4
What is a program
• Programmers like to find recipe (algorithm) for every thing (including
intelligence)
• The focus of programming is to solve problems
•So we need to develop an algorithm (a solution)
•Solutions should be clear, efficient, concise (no ambiguity)
• So, the mathematical algorithm (recipe ) for finding the average for
2 numbers is (a+b)/2
• Now, we can write a program to find the average using this
algorithm
•We need to translate this algorithm to a high-level
programming language (sentences are written similar to
English)
average = (80 + 100)/ 2
• The above statement is similar to how you write a statement in
java, C, C++, Ruby, JavaScript, Perl
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
5
So let’s look at how we can write a program
A simple in python
# my first program in python: let’s print 'hello' on the screen
print ("hello")
This will print the following to the screen:
hello
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
6
Anatomy of a python program
• Comments
• Variables
• Statements
• Functions
• Identifiers
• keywords
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
7
Anatomy of a python program
•Comments (Good Style):
•Comments are ignored by the compiler.
•Makes program easy to read and debug (very important in software development).
• Provides information about your program and significant segments of code that performs
action/computation
•You should provide comments at the beginning of every program
and give the following information:
•Your name, assignment #, and a brief sentence about the purpose
of the program)
Here is an example of a first line(s) comment (multiple lines comment)
“”” Name: Barack Obama. Homework #1
Program provide an algorithm/solution to fix the US economy “”
• You should provide one line comments to explain a segment of code (every 5 lines or so
whenever you have to perform a specific and important action in you program)
# Compute the for average IQ for the last three US presidents
avgiq = (obamaiq + bushiq + Clintoniq)/3
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
8
Anatomy of a python program
• Statements
One line of code that performs a specific actions such as assignment
avgiq = (obamaiq + bushiq + Clintoniq)/3
Or
number = 20
Or
name = “Obama”
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
9
Functions
Functions are mini programs (small modules) that perform specific actions(s)
You can build functions
for sorting, searching, computing calculations such as average, and finding whether a
number is even or odd
1.
Functions can already defined and included with the python software (Built-in ).
•
Many useful functions are available for you to use in your programs such as print()
and input() functions.
•
You can simply use in your programs to perform a specific action such as print,
perform mathematical operations (square numbers, find sqrt and so on)
Example:
print(“ I hope you are NOT sleeping”)
Example2: To ask user to enter data (numbers, strings), you can use the following function:
input(“Mr President, Do you think you are doing a good job so far?”)
2. Functions can be defined by the programmer (User- Defined)
will learn how to do this later on
•
They can be called methods in other programming languages
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
10
Variables:
•
labels (a location in memory that points/holds one piece of data (one
value )
•
Locations in memory that holds one piece of data
• Numbers: (floating-point numbers and whole numbers)
•
•
-200 (integers- whole numbers without any decimal values)
•
20.90 (floating-point numbers )
Text: (String) One or more character
• name = “Avatar”
• message = “Hello there”)
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
Memory Concepts/Variables
Bucket Analogy
11
(simple definition)
• It is useful to think of a variable for now as a bucket
holding one piece of data ( a number or a string).
• The bucket has a unique name, and can only hold certain
kinds of data.
200.30
Price is a variable
containing the value
200.30 , and can contain
only integers.
price
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
12
Reserved Words
•Reserved words or keywords are words
that have a specific meaning to the
compiler and cannot be used in the
program.
• Reserved words in python:
–
–
–
–
–
–
if
elif
and
Or
for
while
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
13
Python Reserved Words
Reserved words in python version 3:
• Let’s print a list of keywords now
using python interactive commands
•(at the python prompt >>> , you can type
commands)
>>> import keyword
>>> print (keyword.kwlist)
'False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
14
Identifier
– Name or labels you can give to names of programs, variables
or functions called identifier ( in this case its hello)
• Series of characters consisting of letters, digits,
underscores ( _ )
• Must begin with a letter or underscore
• Does not begin with a digit
• Should not include spaces
• Letters are case sensitive (Name is not the same as name)
• Examples: Welcome1, value, _value, button7
– 2num is invalid
– Butt on is invalid
• Pythonis case sensitive (capitalization matters)
– a1 and A1 are different
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
15
Programming Errors
• Syntax Errors
– Detected by the compiler
• Runtime Errors
– Causes the program to abort
• Logic Errors
– Produces incorrect result
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
16
Syntax Errors
• Caused when the compiler cannot
recognize a statement.
• These are violations of the language
• The compiler normally issues an
error message to help the
programmer locate and fix it
• Also called compile errors or
compile-time errors.
print(“hello)
• For example, if you forget a
quotation mark or a parenthesis
needed for print(), you will get a
syntax error.
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
17
Run-time Errors
•
Other major kind of error
you’ll see
•
Happens when a program is
running
•
The compiler cannot identify
these errors at compile time.
•
Will talk more about these later
i = 1 / 0
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
18
Logic Errors
• Logic Errors
– Produces incorrect result
double average = 80.0 + 90.0 / 2
This program produces in correct answer of
90.0/2 + 80.0 = 45 + 80.0 = 125.0
Answer should be (80.0 + 90.0)/2
170.0/2 = 85.0
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
19
Arithmetic
+
Addition 2 +2 = 4
*
/
**
Multiplication 2 * 2 = 4
Subtractions 2 - 2 =0
Division 3/2 = 1.5
Power 2**3 = 8
//
Division but will truncates
decimal values (NO decimal
values)
3 // 2 = 1
%
MOD/ Moduls: Returns the
remainder from a division
3%2=1
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.
20
Simple program to add two numbers
# Program will initialize variables and add two numbers
# Specify values for num1 and num2 (initialization variables)
num1 = 2
num2 = 3
# Adding two numbers and storing result in a variable called "sum"
sum = num2 + num1
# Printing numbers and the sum of two numbers
print("num1 has a value of ", num1)
print("num2 has a value of ", num2)
print("The sum of num1 + num2 is ", sum)
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law.
* Professor Sana Odeh’s notes and programs listed here are copyrighted as well.