CH 02
CH 02
Computer Programming 1
Program: Intermediate Diploma in Programming and Computer Science
Amira Alotaibi
• Script Mode: Save Python code in a file (.py) and run it.
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
• The >>> indicates that the Python shell is ready to execute and send your
commands to the Python interpreter
• The result is immediately displayed on the Python shell as soon as the
Python interpreter interprets the command
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
Interactive vs Script Mode
The key differences between programming in interactive mode and
programming in script mode:
1- 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 <enter>key from the keyboard.
2- In script mode, you are provided with a direct way of editing your code.
This is not possible in interactive mode.
First Python Program
Example Program:
Write a program that displays Welcome to Python, Programming is fun, and Problem Driven. The
output should be as the following:
Welcome to Python
Python is fun
Problem Driven
➢ The Solution:
➢ The Solution:
# Compute expression
print((10.5 + 2 * 3) / (45 -3.5))
➢ The output:
0.39759036144578314
Check Point #1
➢Solution: The errors are the incorrect indentation in line 2, and the punctuation (.) in line 3.
Solution:
3.5 * 4 / 2 -2.5 is
4.5
Anatomy of a Python Program
❑ Statement
❑ Indentation
❑ Comment
❑ Special Symbols
Statement
• A statement represents an action or a sequence of actions
Note that the statements are entered from the first column in Block2,Continuation
the new line It would cause an error if the program is typed as Block1, Continuation
Follows:
Bad code
Caution
• Don’t put any punctuation at the end of a statement.
For example, the Python interpreter will report errors for the following code:
Bad code
Note
• Python programs are case sensitive.
• It would be wrong, for example, to replace print in the program with Print.