python lecture note one
python lecture note one
programming
- Problem solving life cycle
Outline
• Problem solving life cycle
– Phase 1:Development
– Phase 2: Documentation
– Phase 3: Maintenance
• Basics of programming
– Low Level and High Level Languages
Software Development
• Computer program
– Self-contained set of instruction used to instruct a
computer to produce specific result.
• Problem Solving life Cycle
– Also called Software Development procedure
– Helps developers understand the problem, to be
solved and create and effective, appropriate
software solution.
Problem Solving Life Cycle
• Requirement
• Phase 1: Development
– Analysis
– Design
– Coding
– Testing
• Phase 2: Documentation
• Phase 3:Maintenance
Requirement
• Request for a program or statement of a
problem
• After a program requirement is received,
phase 1 begins
• In the real world, it is a complex task of
extracting and documenting requirements
Phase 1: Development
• Step 1: Analyze the problem
– Determine and understand the output items the
program must produce
– Determine the input items
Phase 1:Development
• Step2:Develop a Solution
– Select the exact set of steps ,called an
“algorithm”, to solve the problem
– Refine the algorithm
• Start with initial solution in the analysis step until you
have an acceptable and complete solution
– Check solution
***Read about Pseudo Code and Flowchart
Phase 1:Development
• Step 3:Code the solution
– Consists of actually writing a python program that
corresponds to the solution developed in Step2.
– Program should contain well-defined patterns or
structures of the following types:
• Sequence
• Selection
• Iteration
• Invocation
Phase 1:Development
• Step 3: code the solution
• Sequence: Defines the order in which
instructions are executed
• Selection: Allows a choice between different
operation , based on some condition
• Iteration: Allows the same operation to be
repeated based on some condition
Also called looping or repetition
• Invocation: Involves invoking a set of
Phase 1:Development
• Step 4: Test and correct the program
– Testing :Method to verify correctness and that
requirements are met.
– Bug: A program error
– Debugging: The process of locating an error, and
verifying the correction
– Testing may reveal errors ,but does not guarantee
the absence of errors
Phase II: Documentation
• Five main documents are needed:
– Program description
– Algorithm development and changes
– Well-commented program listing
– Sample test runs
– Users’ manual
Phase III: Maintenance
• Ongoing correction of newly discovered bugs
• Revisions to meet changing user needs
• Addition of new features
• Usually the longest phase
• Good documentation vital for effective
maintenance
Programming Languages
• Programming :process of writing a program, or
software
• Programming language
– Set of instructions used to construct a program
– Comes in a variety of forms and types
Low and High Level Languages
• Low –Level languages: Languages that use
instructions tied directly to one type of
computer.
– Machine languages ,assembly language
• High –Level Languages: Instruction resemble
written languages , such as English
– Can be run on a variety of computer types
– Visual Basic ,C,C++.Java
PYTHON PROGRAMS
• A program is a sequence of definitions and
commands
– Definitions evaluated
– Commands executed by Python interpreter
– Commands (statements) instruct
interpreter to do something
OBJECTS
Programs manipulate data objects
Objects have a type that defines the kinds of
things programs can do to them
30 Is a number
We can add/sub/mult/div/exp/etc
'Ana' Is a sequence of characters (ana a string)
Type of Data
• int – represent integers, ex. 5, -100
• float – represent real numbers, ex. 3.27, 2.0
• bool – represent Boolean values True and
False
• NoneType – special and has one value, None
TYPE CONVERSIONS (CASTING)
• Can convert object of one type to another
– float(3) casts the int 3 to float 3.0
– int(3.9) casts (note the truncation!) the float 3.9
to int 3
– Some operations perform implicit casts
– round(3.9)returns the int 4
EXPRESSIONS
• Combine objects and operators to form
expressions
– 3+2
– 5/3
• An expression has a value, which has a type
– 3+2 has value 5 and type int
– 5/3 has value 1.666667 and type float
– Python evaluates expressions and stores the
value.
OPERATORS on int and float
• i+j the sum
• i-j the difference
• i*j the product
• i/j division
• i%j the remainder when i is divided by j
• i**j i to the power of j
SIMPLE OPERATIONS
• Parentheses tell Python to do these operations
first
• Like math!
• Operator precedence
without parentheses **
* / % executed left to right, as appear in
expression
+ – executed left to right, as appear in expression
VARIABLES
• Computer science variables are different than
math variables
– Math variables
– Abstract Can represent many values e.g x * x = y
– CS variables Is bound to one single value at a
given time
– Can be bound to an expression (but expressions
evaluate to one value!)
– E.g a = b + 1 ,m = 10 ,F = m*9.9
BINDING VARIABLES to VALUES
• In CS, the equal sign is an assignment
One value to one variable name
Equal sign is not equality, not “solve for x”
An assignment binds a value to a name
Step 1: Compute the value on the right hand side (the
VALUE)
Value stored in computer memory
Step 2: Store it (bind it) to the left hand side (the VARIABLE)
Retrieve value associated with name by invoking the name
(typing it out)
STRINGS, INPUT/OUTPUT, and BRANCHING
• STRINGS
Think of a str as a sequence of case sensitive characters
Letters, special characters, spaces, digits
Enclose in quotation marks or single quotes
Just be consistent about the quotes
a = "me"
z = 'you'
Concatenate and repeat strings
b = "myself"
c=a+bd=a+""+b
silly = a * 3
STRING OPERATIONS
• len() is a function used to retrieve the length
of a string in the parentheses
• s = "abc" len(s)
• evaluates to 3 chars = len(s)
• Chars=len(s)
Cont…
Square brackets used to perform indexing into a string
to get the value at a certain index/position s = "abc“
s[0] evaluates to "a"
s[1] evaluates to "b“
s[2] evaluates to "c“
s[3] trying to index out of bounds, error
s[-1] evaluates to "c"
s[-2] evaluates to "b"
s[-3] evaluates to "a"
INPUT/OUTPUT
• PRINTING
Used to output stuff to console
In [11]: 3+2
Out[11]: 5
Command is print In [12]: print(3+2) 5
Printing many objects in the same command
Separate objects using commas to output them
separated by spaces
Concatenate strings together using + to print as single
object a = "the" b = 3 c = “word“
print(a, b, c) print(a + str(b) + c)
INPUT
• x = input(s)
– Prints the value of the string s
– User types in something and hits enter
– That value is assigned to the variable x
– Binds that value to a variable text = input("Type
anything: ")
– print(5*text)
Cont…
x = input(s)
Prints the value of the string s
User types in something and hits enter
That value is assigned to the variable x
Binds that value to a variable text = input("Type
anything: ") print(5*text
Cont…
• x = input(s)
– Prints the value of the string s
– User types in something and hits enter
– That value is assigned to the variable x
– Binds that value to a variable text = input("Type
anything: ") print(5*text)
Cont…
• x = input(s)
• Prints the value of the string s
• User types in something and hits enter
• That value is assigned to the variable x
• Binds that value to a variable text =
input("Type anything: ") print(5*text
Cont….
x = input(s)
Prints the value of the string s
User types in something and hits enter
That value is assigned to the variable x Binds
that value to a variable text = input("Type
anything: ") print(5*text)
Cont…