DFP40203 CH1 Introduction To Python
DFP40203 CH1 Introduction To Python
INTRODUCTION
TO PYTHON
PROGRAMMING
DFP 40203 PYTHON PROGRAMMING
SESSION I : 2023/2024
Display general description about
Python
COURSE
LEARNING Display Basic Principles of Python
OUTCOME
Display the features of Python
2. Interpreted language
o The source code of a Python is converted into bytecode that is then executed
by the Python virtual machine. There is no explicit compilation step in Python.
https://www.javatpoint.com/python-features
HOW TO RUN PYTHON PROGRAM ?
There are several ways in which you can run a Python program, including
• Interactively using the Python interpreter
• Stored in a file and run using the Python command
• Run as a script file specifying the Python interpreter to use within the script file
• From within a Python IDE (Integrated Development Environment) such as
PyCharm.
INTERACTIVELY USING THE PYTHON INTERPRETER
• This method uses the Python REPL (Read Evaluate Print Loop style of operation).
• Using the REPL, Python statements and expressions can be typed into the Python
prompt and will then be executed directly.
• The values of variables will be remembered and may be used later in the session.
• To run the Python REPL, Python must have been installed onto the computer system
you are using.
• Once installed you can open a Command Prompt window (Windows) or a Terminal
window (Mac) and type python into the prompt.
INTERACTIVELY USING THE PYTHON INTERPRETER
In the above example, we interactively typed in several Python commands and the Python interpreter
'Read' what we have typed in, 'Evaluated' it (worked out what it should do), 'Printed' the result and
then 'Looped' back ready for further input.
RUNNING A PYTHON FILE
• We can of course store the Python commands into a file.
• This creates a program file that can then be run as an argument to the python
command.
• For example, given a file containing the following file (called hello.py) with the 4
commands in it:
RUNNING A PYTHON FILE
• To run the hello.py program on a PC using Windows we can use the python command
followed by the name of the file:
EXECUTING A PYTHON SCRIPT
• A script is a stand-alone file that can be run directly without the need to (explicitly)
use the python command.
• This is done by adding a special line to the start of the Python file that indicates the
Python command (or interpreter) to use with the rest of the file.
• This line must start with '#!' and must come at the start of the file.
EXECUTING A PYTHON SCRIPT
• Different systems will store Python in different locations and thus might need different
first lines, for example on a Linux we might write:
#!/usr/local/bin/python3
print('Hello, world')
print(5 + 4)
name = 'John'
print(name)
EXECUTING A PYTHON SCRIPT
1. An example of the hello.py script for a Windows or Linux machine is given below
using Notepad++ on a Windows box.
USING PYTHON IN AN IDE
1. We can also use an IDE such as PyCharm to writing and execute our Python program.
The same program is shown using PyCharm below: