0% found this document useful (0 votes)
13 views

python lecture note one

The document outlines the problem-solving life cycle in computer programming, which includes development, documentation, and maintenance phases. It details the steps involved in each phase, such as requirement analysis, coding, testing, and documentation needs. Additionally, it covers programming basics, types of programming languages, and fundamental concepts like variables, data types, and control structures in Python.

Uploaded by

Surafel Yosef
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

python lecture note one

The document outlines the problem-solving life cycle in computer programming, which includes development, documentation, and maintenance phases. It details the steps involved in each phase, such as requirement analysis, coding, testing, and documentation needs. Additionally, it covers programming basics, types of programming languages, and fundamental concepts like variables, data types, and control structures in Python.

Uploaded by

Surafel Yosef
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Problem Solving and Computer

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…

input always returns an str, must cast if working


with numbers
num1 = input("Type a number: ")
print(5*num1)
num2 = int(input("Type a number: "))
print(5*num2)
Cont…
Input always returns an str, must cast if working
with numbers
num1 = input("Type a number: ")
print(5*num1)
num2 = int(input("Type a number: "))
print(5*num2)
Cont…
Input always returns an str, must cast if working
with numbers
num1 = input("Type a number: ")
print(5*num1)
num2 = int(input("Type a number: "))
print(5*num2)
Cont…
Input always returns an str, must cast if working
with numbers
num1 = input("Type a number: ")
print(5*num1)
num2 = int(input("Type a number: "))
print(5*num2)
CONDITIONS for BRANCHING
• BINDING VARIABLES and VALUES
In CS, there are two notions of equal
Assignment and Equality test variable = value Change the
stored value of variable to value Nothing for us to solve,
computer just does the action
some_expression == other_expression
A test for equality
No binding is happening
Expressions are replaced by values and computer just does the
comparison
Replaces the entire line with True or False
COMPARISON OPERATORS
• i and j are variable names
• They can be of type ints, float, strings, etc.
Comparisons below evaluate to the type
Boolean
• The Boolean type only has 2 values: True and
False i > j, i >= j, i < j, i <= j ,i == j
• equality test, True if i is the same as j i != j
inequality test, True if i not the same as j
LOGICAL OPERATORS on bool
• a and b are variable names (with Boolean
values) not a
• True if a is False
• False if a is True a and b
• True if both are True a or b True if either or
both are True.
COMPARISON EXAMPLE
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time)
derive = True drink = False both = drink and
derive print(both
WHY bool?
• When we get to flow of control, i.e. branching
to different expressions based on values, we
need a way of knowing if a condition is true
E.g., if something is true, do this, otherwise do
that
BRANCHING IN PYTHON
if <condition>:
<code>
<code>
….
<rest of program>
has a value True or False
Indentation matters in Python!
Do code within if block if condition is True
BRANCHING IN PYTHON
if <condition>:
<code>
<code>
….
<rest of program>
if<condition>:
<code>
<code>

else:
<code>
<code>

<rest of program>
Cont….

<condition>has a value True or False


Indentation matters in Python!
Do code within if block when condition is True
or code within else block when condition is
False.
BRANCHING IN PYTHON
If <condition>:
<code>
<code>

<rest of program>
if <condition>:
<code>
<code>

elif <condition>:
<code>
<code>
….
<rest of program>
Cont..
• <condition> has a value True or False
• Indentation matters in Python!
• Run the first block whose corresponding is
True<condition>is true
The else block runs when no conditions were
True
INDENTATION and NESTED BRANCHING
• Matters in Python
• How you denote blocks of code
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y:
print("x and y are equal")
if y != 0:
print("therefore, x / y is", x/y)
elif x < y:
print("x is smaller")
else:
print("y is smaller")
print("thanks!")

You might also like