0% found this document useful (0 votes)
2 views36 pages

GE260 Python

The document provides an introduction to programming with a focus on Python, covering essential concepts such as programming languages, the development cycle, and programming tools like algorithms and flowcharts. It explains key programming elements including variables, data types, and the use of functions for input and output. Additionally, it highlights the importance of problem-solving through structured programming and the characteristics common to all programs.

Uploaded by

AJ Pristine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views36 pages

GE260 Python

The document provides an introduction to programming with a focus on Python, covering essential concepts such as programming languages, the development cycle, and programming tools like algorithms and flowcharts. It explains key programming elements including variables, data types, and the use of functions for input and output. Additionally, it highlights the importance of problem-solving through structured programming and the characteristics common to all programs.

Uploaded by

AJ Pristine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

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

You might also like