0% found this document useful (0 votes)
15 views50 pages

Chapter 1

Uploaded by

Dana
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)
15 views50 pages

Chapter 1

Uploaded by

Dana
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/ 50

Introduction To

Python
Chapter 1

Deanship of Preparatory Programs

H Alhussaini 1
Outlines
■ What is a Computer?
■ What is a Program?
– Programming Languages
■ What is Python?
– Python’s History
– Python in Real-World Scenarios
■ Getting Started with Python
■ Modes of Python Interpreter
■ A Simple Python Program
– Excersice #1
– Excersice #2
■ Anatomy of a Python Program
■ Programming Errors
– Excersice #3

H Alhussaini 2
What is a Computer?
■ A computer is an electronic device that stores and processes data

■ A computer includes both hardware and software

■ In general, hardware comprises the visible, physical elements of the computer, and
software provides the invisible instructions that control the hardware and make it
perform specific tasks

H Alhussaini 3
What is a Program?
■ Computer programs, known as software, are instructions to tell the computer what
to do
■ Without programs, a computer is an empty machine
■ Computers do not understand human languages, so you need to use computer
languages to communicate with them
■ Programs are written using programming languages

H Alhussaini 4
Programming Languages
1. Machine Language (Low-Level Language, Binary Language)

2. Assembly Language

3. High-Level Language

H Alhussaini 5
1. Machine Language
■ Machine language is a set of primitive instructions in forms of 0 and 1 that a
computer can read and understand

■ All languages at the end will be translated to binary

■ Programs in binary languages are highly difficult to read and modify

H Alhussaini 6
1. Machine Language
■ Can you write your name in Binary Language?
– Lets try to write a short name “Ali”
– Ali = 01000001 01101100 01101001

■ How about writing a full sentence


– I will leave it to your imagination

H Alhussaini 7
2. Assembly Language
■ Assembly languages were developed to make programming easy
■ Since the computer cannot understand assembly language, however, a program
called assembler is used to convert assembly language programs into machine code

■ For example, to add two numbers, you might write an instruction in assembly code
like this:

ADD 2, 3, result

H Alhussaini 8
3. High-Level Language
■ The high-level languages are English-like and easy to learn and program
■ Since the computer cannot understand high-level languages, however, a program
called interpreter or compiler is used to convert high-level language programs into
machine code
■ For example, the following is a high-level language statement (instruction) that
computes the area of a circle with radius 5:

area = 5 * 5 * 3.1415

H Alhussaini 9
Interpreting/Compiling Source Code
■ The instructions in a high-level programming language are called statements
■ A program written in a high-level language is called a source code
■ Because a computer cannot understand a source program, a source program must
be translated into machine code for execution
■ The translation can be done using another programming tool called an interpreter or
a compiler

H Alhussaini 10
What is Python?
General Purpose

■ Python is a general-purpose programming language


■ That means you can use Python to write code for any programming tasks
■ Python are now used in Google search engine and in mission critical projects in
NASA

H Alhussaini 11
What is Python?
Interpreted

■ Which means that python code is translated and executed one statement at a time
by an interpreter

■ In a compiled language, the entire source code is compiled and then executed
altogether.

H Alhussaini 12
What is Python?
Object-Oriented

■ Data in Python are objects created from classes


■ A class is essentially a type that defines the objects of the same kind with properties
and methods for manipulating objects
■ Object-oriented programming is a powerful tool for developing reusable software

H Alhussaini 13
Python’s History
■ Python is created by Guido van Rossum in Netherlands in 1990
■ Python is open source
– Open-source software is a type of computer software in which source code is
released under a license in which the copyright holder grants users the rights
to study, change, and distribute the software to anyone and for any purpose

■ Python 3 is a newer version, but it is not backward compatible with Python 2


■ That means if you write a program using Python 2, it may not work on Python 3

H Alhussaini 14
Python in Real-World Scenarios
■ Python is being used in almost every industry and scientific field that you imagine

– Data Science - Computer Vision and Processing


– Machine learning - Game Development
– Robotics - Medicine and Pharmacology
– Autonomous Vehicle - Biology and Bioinformatics
– Business - Neuroscience and Psychology
– Web Development - Computer Science education

H Alhussaini 15
Getting Started with Python
Install PyCharm

■ Go to: https://www.jetbrains.com/pycharm/download/

H Alhussaini 16
Modes of Python Interpreter -
Interactive Mode
■ Interactive mode provides us with a quick way of running blocks or a single line of
Python code
■ The code executes via the Python Shell (also known as Python Interactive Shell),
which comes with Python installation.

H Alhussaini 17
Modes of Python Interpreter -
Script Mode
■ This is the normal mode where a python code is written in a text file with a ‘.py’
extension, and Python interpreter executes the file
■ The result of the code will be displayed after the Python interpreter runs the file

H Alhussaini 18
Interactive vs Script Mode
■ The key differences between programming in interactive mode and programming in
script mode:
– In script mode, a file must be created and saved before executing the code to
get results. In interactive mode, the result is returned immediately after
pressing the key from the keyboard
– In script mode, you are provided with a direct way of editing your code. This is
not possible in interactive mode

H Alhussaini 19
A Simple Python Program - Welcome
with Two Messages Program
■ Write a program that displays Welcome to Python and Programming is fun. The
output should be as the following:

■ The Solution:

H Alhussaini 20
Welcome with Two Messages Program –
Step 1 – of 7
■ Open PyCharm program

H Alhussaini 21
Welcome with Two Messages Program –
Step 2 – of 7
■ Click on “Create New Project”.

H Alhussaini 22
Welcome with Two Messages Program –
Step 3 – of 7
■ Change the default name of the project, For example, name it as “My First Project” ,
and then click on “Create”.

H Alhussaini 23
Welcome with Two Messages Program –
Step 4 – of 7
■ Then, the new project is created and opened. After that, you have to create a new
Python file inside the project to write the code on it

H Alhussaini 24
Welcome with Two Messages Program –
Step 5 – of 7
■ Name the new Python file: Welcome

H Alhussaini 25
Welcome with Two Messages Program –
Step 6 – of 7
■ Now, the new file is created and opened. Write the code in it:

print("Welcome to Python")
print("Python is fun")

H Alhussaini 26
Welcome with Two Messages Program –
Step 7 – of 7
■ To run the file, right click on any area of the editor and click on (Run ‘Welcome’),
which is the name of the file

H Alhussaini 27
A Simple Python Program - Compute an
Expression
10.5 + 2 × 3
■ Write a program that evaluates and print its result
45 − 3.5

# Compute expression
print ((10.5 + 2 * 3) / (45 - 3.5))

H Alhussaini 28
Excersice #1
■ Identify and fix the errors in the following code:

1. # Display two messages


2. print("Welcome to Python")
3. print("Python is fun").

H Alhussaini 29
Excersice #1 - Solution
■ The errors are the incorrect indentation in line 2 and the punctuation (.) in line 3

1. # Display two messages


2. print("Welcome to Python")
3. print("Python is fun").

H Alhussaini 30
Excersice #2
■ Show the output of the following code:

print("3.5 * 4 / 2 - 2.5 is")


print(3.5 * 4 / 2 - 2.5)

H Alhussaini 31
Anatomy of a Python Program
1. Statement
2. Indentation
3. Comment
4. Special Symbols

H Alhussaini 32
1. Statement
■ A statement represents an action or a sequence of actions
■ The statement print("Welcome to Python") in the program is a statement to display
the greeting "Welcome to Python“.

H Alhussaini 33
2. Indentation
■ The indentation matters in Python

■ The following figure is a block structure


visualizing indentation

■ Note that the statements are entered from the first column in the new line. It would
cause an error if the program is typed as follows:

H Alhussaini 34
2. Indentation
■ Indentation
– Indent four spaces
■ Spacing
– A consistent spacing style makes programs clear and easy to read, debug, and
maintain
– Use blank line to separate segments of the code.

H Alhussaini 35
Be careful!
■ Don’t put any punctuation at the end of a statement
■ For example, the Python interpreter will report errors for the following code:

H Alhussaini 36
Be careful!
■ Python programs are case sensitive
■ It would be wrong, for example, to replace print in the program with Print

H Alhussaini 37
3. Comment
■ A comment is a programmer-readable explanation or annotation in the source code
of a computer program
■ In Listing 1.1, line 1 is a comment that documents what the program is and how it is
constructed.

H Alhussaini 38
3. Comment
■ Comments help programmers communicate and understand a program
■ They are not programming statements and thus are ignored by the interpreter
■ In Python, comments are preceded by a pound sign (#) on a line, called a line
comment, or enclosed between three consecutive single quotation marks (''') on one
or several lines, called a paragraph comment

H Alhussaini 39
3. Comment
■ When the Python interpreter sees #, it ignores all text after # on the same line
■ When it sees ''' , it scans for the next ''' and ignores any text between the triple
quotation marks
■ Here are examples of comments:

H Alhussaini 40
3. Comment
■ Comments explain various parts of the program and help others understand its
structure and function
■ Good programming style and proper documentation make a program easy to read
and prevents errors
■ Include a summary comment at the beginning of the program to explain what the
program does, its key features, and any unique techniques it uses

H Alhussaini 41
4. Special Symbols

H Alhussaini 42
Programming Errors
■ Programming errors can be categorized into three types:

1. Syntax Errors
– Error in code construction
2. Runtime Errors
– Causes the program to abort
3. Logic Errors
– Produces incorrect result.

H Alhussaini 43
1. Syntax Errors
■ Syntax errors result from errors in code construction, such as mistyping a statement,
incorrect indentation, omitting some necessary punctuation, or using an opening
parenthesis without a corresponding closing parenthesis
■ Python has its own syntax, and you need to write code that obeys the syntax rules. If
your program violates the rules Python will report syntax errors

H Alhussaini 44
2. Runtime Errors
■ Runtime errors are errors that cause a program to terminate abnormally
■ They occur while a program is running if the Python interpreter detects an operation
that is impossible to carry out
■ Input mistakes typically cause runtime errors.

H Alhussaini 45
2. Runtime Errors
■ An input error occurs when the user enters a value that the program cannot handle
■ For instance, if the program expects to read in a number, but instead the user enters
a string of text, this causes data-type errors to occur in the program

H Alhussaini 46
3. Logic Errors
■ Logic errors occur when a program does not perform the way it was intended to
■ Logic errors produce unintended, incorrect or undesired output or other behavior,
although it may not immediately be recognized as such
■ In fact, they do not cause the program to terminate abnormally

H Alhussaini 47
Programming Errors
■ In Python, syntax errors are actually treated like runtime errors because they are
detected by the interpreter when the program is executed
■ In general, syntax and runtime errors are easy to find and easy to correct, because
Python gives indications as to where the errors came from and why they are wrong
■ Finding logic errors, on the other hand, can be very challenging

H Alhussaini 48
Excersice #3
■ If you forget to put a closing quotation mark on a string, what kind of error will be
raised?
– Answer: Syntax Error
■ If your program needs to read data from a file, but the file does not exist, an error
would occur when running this program. What kind of error is this?
– Answer: Runtime Error
■ Suppose you write a program for computing the perimeter of a rectangle and you
mistakenly write your program so that it computes the area of a rectangle. What kind
of error is this?
– Answer: Logic Error

H Alhussaini 49
Q&A

H Alhussaini 50

You might also like