Information Science & Programming
A. A. Acheampong
Geomatic Eng. Dept., KNUST
August 5, 2022
Introduction 2
▶ How do we communicate with the computer?
Programming languages
▶ How do we get computers to perform complicated tasks?
Tasks are broken down into a sequence of instruction
▶ Why Python?
Powerful, easy to download, write and read
Introduction 3
▶ the editor IDLE will be used to create programs.
▶ How did IDLE get its name?
Stands for Integrated DeveLopment Environment
▶ What is an interpreted language?
Uses an interpreter, translates high-level language one statement at a
time into machine language and then runs
Introduction 4
▶ What are the meanings of the terms “programmer” and “user”?
▶ Programmer: a person who solves problems by writing programs on a computer
▶ User: any person who runs a program
▶ What is the meaning of the term “code”?
Python statements that the programmer writes
Introduction – Python 5
▶ Are there certain characteristics that all programs have in common?
Input, processing, output
▶ How are problems solved with a program?
Step-by-step procedure devised to process given data and produce requested
output.
▶ What is a zero-based numbering system?
Numbering begins with zero instead of one
▶ Prerequisites to learning Python?
Be familiar with how folders and files are managed
Introduction – Programming 6
Program Development Cycle
Program Planning 7
▶ Analyze: Define the problem.
▶ Design: Plan the solution to the problem.
▶ Code: Translate the algorithm into a programming language.
▶ Test and correct: Locate and remove any errors in the program.
▶ Complete the documentation: Organize all the material that describes the
program.
Programming Tools 8
▶ Algorithms – a process or set of rules to be followed in calculations or other
problem-solving operations
▶ Flowcharts – a diagram of a process in sequential order.
▶ Pseudocode – uses structural conventions of a normal programming language, but is
intended for human reading rather than machine reading
▶ Hierarchy charts – (also known as a structure chart) shows the relationship
between various modules.
Pseudocode 9
▶ Abbreviated plain English version of actual computer code
▶ Symbols used in flowcharts replaced by English-like statements
▶ Allows programmer to focus on steps required to solve problem
Repetition Structure 10
▶ A programming structure that executes instructions many times
Repetition structure
Looping structure
▶ Need a test (or condition) to tell when the loop should end
▶ Check condition before each pass through loop
Repetition Structure 11
Sample Program 12
▶ Problem: Calculate and report the average grade for a class.
▶ Discussion: Average grade equals sum of all grades divided by number of students.
Need loop to read and then add (accumulate) grades for each student in class
Inside the loop, we also need to total (count) number of students in class.
▶ Input: Student grades.
▶ Processing: Find the sum of the grades; count the number of students; calculate
average grade = sum of grades / number of students.
▶ Output: Average
Core Objects, Variables, Input
& Output
Numbers 14
▶ Numbers are referred to as numeric literals
Two Types of numbers: ints and floats
▶ Whole number written without a decimal point called an int
▶ Number written with a decimal point called a float
▶ Arithmetic Operators
Addition, subtraction, multiplication, division, and exponentiation.
Numbers 15
▶ Result of a division is always a float
▶ Result of the other operations is a float if . . .
Either of the numbers is a float
Otherwise is an int.
The print Function 16
▶ Used to display numbers on the monitor
▶ If n is a number, print(n) displays number n.
▶ The print function can display the result of evaluated expressions
▶ A single print function can display several values
Variables 17
▶ In mathematics problems, quantities are referred to by names
▶ Names given to the values are called variables
▶ Example 2: Program uses speed, time ... calculates distance
Variables 18
Variables 19
▶ Variable names in Python
▶ Begin with letter or underscore
▶ Can only consist of letters, numbers, underscores
▶ Recommend using descriptive variable names
▶ Convention will be...
Begin with lowercase
Use cap for additional “word” in the name
Example: rateOfChange
Variables 20
▶ Variable names in Python
▶ Names in Python are case-sensitive
▶ There are words called reserved words (or keywords)
Have special meanings in Python
Cannot be used as variable names
Some Reserved Words 21
Variables 22
Order of Precedence 23
▶ Parentheses used to clarify meaning of an expression
▶ Order of precedence
▶ Terms inside parentheses (inner to outer)
▶ Exponentiation
▶ Multiplication,
▶ division (ordinary and integer),
▶ modulus
▶ Addition and subtraction
Objects in Memory 24
Strings 25
▶ Sequence of characters that is treated as a single item
▶ Written as a sequence of characters surrounded by either single quotes (’) or double
quotes (”)
”John Doe”
’5th Avenue’
’76-ggd’
”Say it, isn’t it, Kwamina”
▶ Opening and closing quotation marks must be the same type
Strings:- Indices & Slices 26
Strings:- Indices & Slices 27
Strings:- Indices & Slices 28
Strings:- Concatenation 29
▶ Two strings can be combined to form a new string
Consisting of the strings joined together
Represented by a plus sign
▶ Combination of strings, plus signs, functions, and methods can be evaluated Called a
string expression
▶ Asterisk operator can be used with strings to repeatedly concatenate a string with
itself
Strings:- Concatenation 30
The input Function 31
▶ Prompts the user to enter data
town = input(”Enter the name of your city”)
▶ User types response, presses ENTER key – Entry assigned to variable on left
Output 32
Escape Sequences 33
▶ Short sequences placed in strings
▶ Instruct cursor or permit some special characters to be printed.
▶ First character is always a backslash (\).
\t induces a horizontal tab
\n induces a newline operation
▶ Backslash also used to treat quotation marks as ordinary characters.
\” causes print function to display double quotation mark
\\ causes print function to display single backslash
Escape Sequences 34
Output 35
Number formatting
Output 36
## Demonstrate use of the format method
print(”The area of {0 : s}” is {1 :, d} square kilometers.”.format(”Kintampo”, 268820))
str1 = ”The population of {0 : s} is {1 : .2%} of the Ghana population”
print(str1.format(”Kintampo”, 268820 / 30100000))
[Run]
The area of Kintampo is 268,820 square kilometers
The population of Kintampo is 0.89% of the Ghana population