L01P
L01P
Introduction
Topics
• Introduction
• Hardware and Software
• How Computers Store Data
• How a Program Works
• Using Python
Introduction
• Computers can be programmed
• Designed to do any job that a program tells them to
• Program: set of instructions that a computer
follows to perform a task
• Commonly referred to as Software
• Programmer: person who can design, create, and
test computer programs
• Also known as software developer
Hardware and Software
• Hardware: The physical devices that make up a
computer
• Computer is a system composed of several components that
all work together
• Typical major components:
• Central processing unit
• Main memory
• Secondary storage devices
• Input and output devices
How Computers Store Data
• All data in a computer is stored in sequences of 0s
and 1s
• Byte: just enough memory to store letter or small
number
• Divided into eight bits
• Bit: electrical component that can hold positive or negative
charge, like on/off switch
• The on/off pattern of bits in a byte represents data stored in
the byte
How a Program Works
• Program must be copied from secondary memory
to RAM each time CPU executes it
• CPU executes program in cycle:
• Fetch: read the next instruction from memory into CPU
• Decode: CPU decodes fetched instruction to determine
which operation to perform
• Execute: perform the operation
From Machine Language to Assembly
Language
• Impractical for people to write in machine language
• Assembly language: uses short words
(mnemonics) for instructions instead of binary
numbers
• Easier for programmers to work with
• Assembler: translates assembly language to
machine language for execution by CPU
High-Level Languages
• Low-level language: close in nature to machine
language
• Example: assembly language
• High-Level language: allows simple creation of
powerful and complex programs
• No need to know how CPU works or write large number of
instructions
• More intuitive to understand
Compilers and Interpreters
• Programs written in high-level languages must be
translated into machine language to be executed
• Compiler: translates high-level language program
into separate machine language program
• Machine language program can be executed at any time
Compilers and Interpreters
• Interpreter: translates and executes instructions in
high-level language program
• Used by Python language
• Interprets one instruction at a time
• No separate machine language program
• Source code: statements written by programmer
• Syntax error: prevents code from being translated
Compilers and Interpreters
• # Prints 7.
• print(3 + 4)
• # Prints “Hello World!” in two lines.
• print("Hello")
• print("World!")
• # Prints multiple values with a single print function call.
• print("My favorite numbers are", 3 + 4, "and", 3 + 10)
•
• # Prints three lines of text with a blank line.
• print("Goodbye")
• print()
• print("Hope to see you again")
Program Run
•7
• Hello
• World!
• My favorite numbers are 7 and 13
• Goodbye
• print(3 * 4) # The print function will be called, but this comment will
be ignored
Parentheses first
result_float = 10 / 3
print(result_float) # Output: 3.3333333333333335
result_integer = 10 // 3
print(result_integer) # Output: 3
It's important to note that the behavior of integer division (//) can vary depending on the
operands. If both operands are integers, the result will be an integer. However, if one or both
operands are floating-point numbers, the result will be a floating-point number.
The Exponent Operator and the Remainder
Operator
Exponent operator (**): Raises a number to a power
x ** y = xy
Enter the amount of sales for each day and press Enter.
Breaking Long Statements into Multiple Lines
Any part of a statement that is enclosed in parentheses can be
broken without the line continuation character.
Enter the amount of sales for each day and press Enter.