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

10A Chapter11 Programming Python a99e7b226e0933e81e091b2abc57260d

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

10A Chapter11 Programming Python a99e7b226e0933e81e091b2abc57260d

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

Objectives

• Learn what a program is and how it can be developed

• Understand the difference between a low-level and


high-level language

• Be introduced to low-level languages using the


Assembly programming language as an example

• Learn about the structure of a program, including


algorithms and pseudocode

Connecting with Computer Science 2


Objectives (continued)
• Gain an understanding of the basics of high-level
programming languages using Python as an example
• Learn about variables and how they are used
• Be introduced to the Python operators
• Explore the different control structures used in
programming

Connecting with Computer Science 3


What Is a Program?
• A collection of statements that solve a problem
• Must be converted into a language that the computer
understands
– Algorithm: logically ordered set of statements
– Conversion process uses an interpreter or compiler
• Interpreter translates statements one-by-one
• Compiler reads all of the statements and creates a
finished program
Connecting with Computer Science 4
I Speak Computer
• Determine what language you want to use
– Assembly for controlling hardware
– Java and JavaScript for Internet applications
– Lisp for working with artificial intelligence
– Visual Basic for a simple yet powerful GUI programming
environment
– Python for AI applications
– Others include. C, C++, Smalltalk, Delphi, and ADA,
FORTRAN, and COBOL

Connecting with Computer Science 5


Types of
Programming Languages
• Low-level
– Geared towards computer – less understandable or
like human language
– Machine language is lowest-level language
– Assembly resides between lowest-level and higher-
level languages
• Assembler converts assembly code to machine
language
• High-level
– Human-friendly language

Connecting with Computer Science 6


Figure 11-1
Different types of programming languages

Connecting with Computer Science 7


Low-level Languages
• Machine language includes only binary numbers
• Assembly uses more English-like statements
– Each statement corresponds to one machine
instruction
– Programs run faster than programs in higher-level
languages
– Closely tied to particular CPU type
– Harder to read and understand than higher-level
languages
Connecting with Computer Science 8
Some examples of typical assembly
language statements

INC COUNT Increment the memory variable COUNT

MOV TOTAL, 48 Transfer the value 48 in the memory variable TOTAL

ADD AH, BH Add the content of the BH register into the AH register

AND MASK1, 128 Perform AND operation on the variable MASK1 and 128

ADD MARKS, 10 Add 10 to the variable MARKS


MOV AL, 10 Transfer the value 10 to the AL register

Connecting with Computer Science 9


Assembly Language Statements
• Consists of alphabetic instructions with operations
and register indications
– mov moves values around
mov cx, 8
– add adds one to value to another
mov cx, 3
mov dx, 8
add dx, cx
– sub subtracts one value from another

Connecting with Computer Science 10


Assembly Language Statements
(continued)
– inc increments a value in the register
inc dx
– cmp compares two values
mov cx, 4
mov dx, 7
cmp dx, cx (zero flag is set if dx - cx = 0)
– jnz jumps to a specific location in the program
jnz stop (Jumps to the section named stop if
the zero flag is set)
Connecting with Computer Science 11
High-level Languages
• Easier to write, read, and maintain than low-level
languages
• Accomplishes much more in a single statement
• Generally slower
– Must be either compiler or interpreted
• Many incorporate IDEs (integrated development
environment’s)
– Interface that includes an editor, compiler, graphic
designer, and more
Connecting with Computer Science 12
Visual Studio Code Interface

Connecting with Computer Science 13


Structure of a Program
• Program structure is based upon algorithms, and is
often represented using pseudocode
– Algorithm: consists of executable steps to solve a
problem

– Pseudocode: readable description of an algorithm


written in human language
• Template for what needs to be converted into
programming language syntax

Connecting with Computer Science 14


Example of Pseudocode
• Converting the temperature from Celsius to
Fahrenheit
Ask the user for a temperature in
Fahrenheit
Apply the entered temperature to the
formula Celsius
Temp = (5/9) * (Fahrenheit Temp - 32)
Display the result saying Fahrenheit
Temp ## converted to Celsius is XX

Connecting with Computer Science 15


Choosing and Testing
the Algorithm
• There can be many different ways to perform a task
or accomplish a goal
• Determine which algorithm is the best one to use for
the project based on a myriad of factors
• To test the algorithm, pretend that you are the end
user and trying to run the program
– Celsius conversion example: What if the user does not
enter a number value?
• Modify pseudocode to test for valid values

Connecting with Computer Science 16


Modifications to Pseudocode
Based on Testing
Ask the user for a temperature in Fahrenheit
If the value entered is numerical
Apply the entered temperature to the
formula
Celsius Temp =
(5/9) * (Fahrenheit Temp - 32)
Display the result saying Fahrenheit Temp
## converted to Celsius is XX
Else
Display a message stating that the value
entered is NOT allowed

Connecting with Computer Science 17


Syntax of a
Programming Language
• Writing a program can be compared to following a
recipe (the algorithm and pseudocode) to correctly
combine the ingredients to produce a result (program)
• Ingredients include
– Variables
– Operators
– Control Structures
– Objects

Connecting with Computer Science 18


Learning to Cook With Java
• Python is a high-level programming language
Familiar syntax (similar syntax to C++)
– Portable
• Can run on other computers without recompiling
– Powerful
• Rich library of routines for many tasks
– Popular
• Used to develop a variety of applications

Connecting with Computer Science 19


Variables
• Variable: name used to identify a certain location and
value in computer memory
– A variable is a named reference to a value in memory.
– In Python, variables are dynamically typed, meaning
you don’t need to declare their type explicitly.

Connecting with Computer Science 20


Connecting with Computer Science 21
Variables in Python
• Dynamic Typing: Variables can change type during
execution.
• No Declaration Needed: Just assign a value to create
a variable.

Connecting with Computer Science 22


Operators
• Symbols used to manipulate data
• Classified by data type
– Math operators for addition, subtraction,
multiplication, division, and modulus
– Mathematical shortcuts for binary arithmetic
shortcuts
iFirstNum = iFirstNum + iSecondNum;
is the same as
iFirstNum += iSecondNum;

Connecting with Computer Science 23


Connecting with Computer Science 24
Connecting with Computer Science 25
Connecting with Computer Science 26
Connecting with Computer Science 27
Precedence
• The order in which operators appear can determine
the output

• Symbols that have a higher precedence are executed


before those with a lower precedence

(2+3) * 4 outputs 20

2-5*2 outputs -8

Connecting with Computer Science 28


Figure 11-5
Order of relational and mathematical precedence

Connecting with Computer Science 29


Python Control Structures
and Program Flow
• A control structure is an instruction that dictates the
order in which program statements are executed
• Four type of control structures in high-level
languages
– Invocation
– Top down
– Selection
– Repetition

Connecting with Computer Science 30


Top Down (Also Called Sequence)
• Program statements are executed in series, from the
top line to the bottom line one at a time

• Most common form of programming control


structure, found in every programming language

• Implemented by typing in statements that do not call


other pieces of source code

Connecting with Computer Science 31


Blocks of Code
• A single block statement encloses several statements
with an opening and closing brace
– Enclosed statements are related in functionality
– Leaving out braces can cause your program to function
incorrectly
– Braces are required in some circumstances
• Most often used when working with control structures
such as invocation, selection, and repetition

Connecting with Computer Science 32


Selection
• if statement syntax
if (condition)
{ one or more statements }
• if-else statement syntax
if (condition)
{ one or more statements }
else
{ one or more statements }

Connecting with Computer Science 33


Selection (continued)
• if-else-if statement syntax

Connecting with Computer Science 34


Repetition (Looping)
• for statement syntax
for (variable declaration; expression;
increment/decrement)
{ statements(s); }
– Post- or pre-operations are commonly used when updating
the variable used as the counter in the for loop
for (iCount = 1; iCount <= 5; iCount++)
• for and while loops are precondition loops
– The expression is checked before any code is executed
within the loop

Connecting with Computer Science 35


Repetition (continued)
• while statement syntax
while (expression)
{ statements; }
• do while statement syntax
do
{
statement(s);
} while (expression);
– do while loops are postcondition loops
• Executes at least once before expression is evaluated

Connecting with Computer Science 36


Ready, Set, Go!
• Purchase and download Java
– Sun Microsystems offers a version of Java and the
JDK for free
• Choose an editor to write the program
– Use an IDE or a simple text editor such as NotePad
• Compile the program with the javac command
javac MyProg1.java
• Execute the program with the java command
java MyProg1

Connecting with Computer Science 37


Choosing a
Programming Language
• Considerations
– Functionality
– Vendor stability
– Popularity
– Job market
– Price
– Ease of learning
– Performance

Connecting with Computer Science 38


One Last Thought
• A program will do whatever you tell it to do
• In most cases, if the program doesn’t work correctly, it
is the fault of the person who wrote the program, not
the computer
– Be a responsible programmer
• You can create new and wonderful programs to help
society
• Or…the program you write might have serious
ramifications on society

Connecting with Computer Science 39


Summary
• A program is only as good as the programmer(s)
who wrote it
• Programs are used everywhere and in almost
everything you do
• A program can either be interpreted or compiled
• Low-level languages are more closely related to the
machine languages that a computer understands
• Assembler is a low-level programming language

Connecting with Computer Science 40


Summary (continued)
• High-level languages are more closely related to
human language
• Algorithms are created for solving problems through
some logical method
• Pseudocode is a way to use human language to map
out how a program is suppose to work
• Creating the algorithm is one of the most important
steps in writing a program

Connecting with Computer Science 41


Summary (continued)
• Java is a high-level programming language that was
initially designed for the Internet
• Variables are temporary storage locations with a
specific data type
– Used for calculations and storage
• Python uses mathematical, relational, and logical
operators
• Four control structures used within a program: top
down, invocation, selection, and repetition

Connecting with Computer Science 42


Practice
• Create a number memorization game with Python.
- The computer generates 3 random integers (from 0 to
9), waits for 2 seconds, and then asks the user to retype
those numbers in the same order.
+If the input numbers are correct, proceed to the
next level and increase the number of random integers by
1.
+ If the input numbers are incorrect, terminate the
game and display the user's score. The score is the
number of random integers generated in the last round.

Connecting with Computer Science 43


Practice
• import random
• import os
• import time
• time.sleep(2) # to wait for 2 second
• os.system('cls') # to clear the screen
• random_int = random.randint(0, 9) # to generate a
random integer form 0 to 9

Connecting with Computer Science 44

You might also like