0% found this document useful (0 votes)
12 views27 pages

L01P

The document provides an introduction to programming with Python, covering fundamental concepts such as hardware and software, data storage, program execution, and the use of high-level languages. It explains the role of compilers and interpreters, particularly how Python uses an interpreter to execute code. Additionally, it includes examples of using the print function, performing calculations, and string manipulation in Python.

Uploaded by

mohaalhabshi515
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)
12 views27 pages

L01P

The document provides an introduction to programming with Python, covering fundamental concepts such as hardware and software, data storage, program execution, and the use of high-level languages. It explains the role of compilers and interpreters, particularly how Python uses an interpreter to execute code. Additionally, it includes examples of using the print function, performing calculations, and string manipulation in Python.

Uploaded by

mohaalhabshi515
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/ 27

Python

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

Figure 1 Executing a high-level program with an interpreter


Using Python
• Python must be installed and configured prior to
use
• One of the items installed is the Python interpreter
Displaying Output with the print
Function
• Function: piece of prewritten code that performs an
operation
• print function: displays output on the screen
• Argument: data given to a function
• Example: data that is printed to screen
• Statements in a program execute in the order that they
appear
• From top to bottom
print(‘Hello, world!’)
• Let’s look at this first program.
•print is a keyword predefined by Python library for output.
• () is indicating you are going to call a function.
• 'Hello, world!' is the message you tell the function to print.
Sample program that demonstrates the print function.

• # 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

• Hope to see you again


Comments
• Comments: notes of explanation within a program
• Ignored by Python interpreter
• Intended for a person reading the program’s code
• Begin with a # character
• End-line comment: appears at the end of a line of
code
• Typically explains the purpose of that line
Comments
• # This is a comment which will be ignored

• print(3 * 4) # The print function will be called, but this comment will
be ignored

• #Calculate and print the product of 4 and 3


• print(4 * 3)
Performing Calculations
 Math expression: performs calculation and gives a value
 Math operator: tool for performing calculation
 Operands: values surrounding operator
 Variables can be used as operands
 Resulting value typically assigned to variable

 Two types of division:


 / operator performs floating point division
 // operator performs integer division
 Positive results truncated, negative rounded away from zero
Operator Precedence and Grouping with
Parentheses
 Python operator precedence:
1. Operations enclosed in parentheses
 Forces operations to be performed before others
2. Exponentiation (**)
3. Multiplication (*), division (/ and //), and remainder (%)
4. Addition (+) and subtraction (-)

 Higher precedence performed first


 Same precedence operators execute from left to right
Summary of the Order of Operations
When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence. For mathematical
operators, Python follows mathematical convention. The
acronym PEMDAS is a useful way to remember the rules, i.e.

Parentheses first

Exponents next, followed by

Multiplication and Division (evaluate left to right), and lastly

Addition and Subtraction (evaluate left to right)


Special Cases- Division: Example
# Float division

result_float = 10 / 3
print(result_float) # Output: 3.3333333333333335

# Integer division (floor division)

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

 Remainder operator (%): Performs division and returns the


remainder
 a.k.a. modulus operator
 e.g., 4%2=0, 5%2=1
 Typically used to convert times and distances, and to detect odd or
even numbers
String Concatenation (1 of 2)

 To append one string to the end of another string

 Use the + operator to concatenate strings

>>> message = 'Hello ' + 'world'


>>> print(message)
Hello world
>>>
String Concatenation (2 of 2)
 You can use string concatenation to break up a long string literal

print('Enter the amount of ' +


'sales for each day and ' +
'press Enter.')

This statement will display the following:

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.

print("Monday's sales are", monday,


"and Tuesday's sales are", thuesday,
"and Wednesday's sales are", Wednesday)

total = (value1 + value2 +


value3 + value4 +
value5 + value6)
Implicit String Literal
Concatenation

print('Enter the amount of '


'sales for each day and '
'press Enter.')

This statement will display the following:

Enter the amount of sales for each day and press Enter.

You might also like