eBOOKPython5 3 2023
eBOOKPython5 3 2023
FATIMAH RAHMAT | MOHAMAD IQBAL HAKIM CHE OMAR | NURUL SHAKIRAH MOHD ZAWAWI
FATIMAH RAHMAT
MOHAMAD IQBAL HAKIM CHE OMAR
NURUL SHAKIRAH MOHD ZAWAWI
Politeknik Mersing
Jalan Nitar,
86800 Mersing
Johor Darul Ta’zim
Telephone : 07-7980001
Fax : 07-7980002
Website : https://pmj.mypolycc.edu.my/
Printed in Malaysia
First Printing, 2023
eISBN : 978-967-2904-57-1
Authors :
Fatimah Rahmat
Mohamad Iqbal Hakim Che Omar
Nurul Shakirah Mohd Zawawi
Python and MySQL for Beginner
The need to share knowledge and experience on how the Python programming
language could have a positive impact on the learning process led to the
creation of this book focuses on the installation process, use of MySQL for
database connections, and fundamental Python language concepts.
This book offers helpful resources, advice, and examples, especially for
students and anybody else interested in learning the Python programming
language.
I. INTRODUCTION
What is Python Programming? ................................... 2
Basic Principles of Python ............................................... 3
II. REQUIREMENTS
TABLE OF CONTENTS
III. INSTALLING
Installing Python and IDE Environment Setup .. 12
References ................................................................................ 33
1
Python and MySQL for Beginner
Interpreted
Python is processed at runtime by the interpreter. You do not need
to compile your program before executing it. This is similar to PERL
and PHP. Execution is implemented directly and freely from source
without need to be compiled into machine code or binary format.
Interactive
You can actually sit at a Python prompt and
interact with the interpreter directly to write your
programs.
Object-Oriented
Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Beginner’s Language
Python is a great language for the beginner- level
programmers and supports the development of a
wide range of applications from simple text
processing to WWW browsers to games.
Python is a high-level programming language that was first released in 1991 by Guido van Rossum.
The language was designed to be easy to read and write, with a focus on code readability. Python's
design philosophy emphasizes code readability, and its syntax allows programmers to express
concepts in fewer lines of code than languages like C++ or Java.
2
Python and MySQL for Beginner
Modules
Self-contained programs which define a variety of functions and data types that you can call in order to
do tasks beyond the scope of the basic core language by using the import command.
Exception handling
When you’re performing an operation that might result in an error, you can surround it with a try loop,
and provide an exception clause to tell python what to do when a particular error arises.
3
CHAPTER 2
REQUIREMENTS
4
Python and MySQL for Beginner
What is needed?
Choose ONE of every category
IDE
(Integrated Development Environment)
PyCharm
Web Server
IDLE
Laragon
XAMPP
5
Python and MySQL for Beginner
7
Python and MySQL for Beginner
To install and run Visual Studio Code (VS Code) for Python development, your system
should meet the following minimum requirements:
1. Operating System: Windows 7 or later, macOS 10.10 or later, or Linux (64-bit)
2. Processor: 1.6GHz or faster, 2-core
3. Memory: 2 GB or more
4. Hard Disk Space: At least 1 GB of free space
5. Display: 1024x768 resolution or higher
6. Internet connection: Required for installation and updates
7. Administrator rights: Installation of VS Code requires administrator rights on Windows
and macOS.
8. Python: To use VS Code for Python development, you will need to have Python
installed on your system. You can download the latest version of Python from the
official website.
9. Python Extension for Visual Studio Code: In order to use VS Code for Python
development, you will need to install the Python extension for VS Code. This extension
provides rich support for Python development, including IntelliSense, debugging, and
linting.
10. A Python environment: Python can be used with virtual environments like Anaconda,
venv, virtualenv and pipenv.
PyCharm is a popular and widely used Integrated Development Environment (IDE) for
Python development developed by JetBrains. To install and run PyCharm, your system
should meet the following minimum requirements:
1. Operating System: Windows, macOS, or Linux
2. Processor: 2 GHz or faster
3. Memory: 2 GB of RAM or more
4. Hard Disk Space: At least 1 GB of free space
5. Display: 1024x768 resolution or higher
6. Internet connection: Required for installation and updates
7. Administrator rights: Installation of PyCharm requires administrator rights on Windows
and macOS.
8. Python: PyCharm is a Python IDE, so you will need to have Python installed on your
system. You can download the latest version of Python from the official website.
9. Java: PyCharm requires Java to be installed on your system. The latest version of Java
can be downloaded from the official website.
10. A Python environment: PyCharm can be used with virtual environments like Anaconda,
venv, virtualenv and pipenv.
You may need additional tools or libraries depending on the specific Python development tasks you plan to do. Be
sure to check the documentation for any libraries or frameworks you plan to use to ensure that your system meets
their requirements.
8
Python and MySQL for Beginner
IDLE (Integrated Development and Learning Environment) is the default, built-in Python
development environment that comes with Python. The requirements to install and run
IDLE are as follows:
1. Operating System: Windows, macOS, or Linux
2. Python: IDLE is built into Python, so you will need to have Python installed on your
system. You can download the latest version of Python from the official website.
3. Memory: IDLE is a lightweight development environment, so it does not require a large
amount of memory to run.
4. Display: IDLE requires a minimum resolution of 1024x768 or higher.
5. Internet connection: Not required for IDLE installation as it comes with Python
installation itself.
6. Administrator rights: IDLE does not require administrator rights to install or run.
7. A Python environment: IDLE can be used with virtual environments like Anaconda,
venv, virtualenv and pipenv.
As IDLE is already included with the Python installation, there is no need to install it
separately. You just need to open the terminal or command prompt and type python -m
idlelib to open IDLE.
Please note that IDLE is a basic text editor and does not have some of the features of
advanced IDEs like PyCharm, visual studio code, etc. It's best suited for beginners or small
scale projects.
9
Python and MySQL for Beginner
Laragon
Portable, isolated, fast & powerful universal Built from the ground up for ease
development environment for PHP, Node. js, of use and perfect compatibility
Python, Java, Go, Ruby. It is fast, lightweight, with Laravel.
easy-to-use and easy-to-extend. Great for
building and managing modern web applications.
Before installing Laragon, here are a few things you should be aware of:
1. System Requirements: Laragon requires Windows 7 or later, and at least 1 GB of RAM.
2. Disk Space: Make sure you have enough disk space to install Laragon and any
projects you plan to work on.
3. Firewall: If you have a firewall enabled, you may need to configure it to allow Laragon
to access the internet and connect to other services.
4. Anti-virus: Some anti-virus software may interfere with the installation or operation of
Laragon. Make sure to temporarily disable your anti-virus software before installing
Laragon, and add an exception for it if needed.
5. Existing Installation: If you have an existing web server, PHP, or MySQL installation,
you may need to remove it or reconfigure it before installing Laragon.
6. Backup: It's always a good idea to make a backup of any important files or data before
making any changes to your system, in case something goes wrong during the
installation.
7. Familiarize yourself with the tools: To get the most out of Laragon, it's a good idea to
be familiar with the tools it includes, such as Apache, PHP, and MySQL, so that you can
configure and troubleshoot them as needed.
By following these steps, it should make the installation process of Laragon smoother and
avoid any potential issues.
10
CHAPTER 3
INSTALLING PYTHON
AND
IDE ENVIRONMENT SETUP
11
Python and MySQL for Beginner
01
Python installation. Go to Figure 1 (https://www.python.org/)
and download the latest version of Python. Follow the
instructions by scan the QR Code.
Figure 1: Python.org
12
Figure 2: Search IDLE
Python and MySQL for Beginner
02
IDE Installation for Visual Studio Code. Go to Figure 4
(https://code.visualstudio.com/Download) and download the suitable
version with your machine. Follow the instructions on How to install
Visual Studio Code on Windows 10/11 [2022 Update] Complete Guide
by scan the QR Code.
Source: YouTube
13
Python and MySQL for Beginner
03
IIDE Installation for PyCharm. Go to Figure 6
(https://www.jetbrains.com/pycharm/download/#section=window
s) and download the Community version. Follow the instructions
on Install Python 3.10 and PyCharm on Windows 10 by scan the
QR Code.
Figure 6: Download
PyCharm: Python IDE for
Professional Developers
by JetBrains
14
Python and MySQL for Beginner
04
Command prompt application needs.
05
Web server installation for Laragon. Go to Figure 9
(https://laragon.org/download/), download Laragon - Full (173 MB).
Follow the instructions for complete installation.
15
CHAPTER 4
FIRST PROJECT ACTIVITY
16
Python and MySQL for Beginner
01
Right click Start All > Click MySQL > Click MySQL (Figure 10)
17
Python and MySQL for Beginner
Session manager window will appear. Click New. Please make sure the port is
correct, user (usually root) with password (usually empty), then click open
01
(Figure 12)
The screen will appear as Figure 13. db1 (database name) and table1(table
name). If you want rename -> right-click the database or table name > rename.
Press F5 for refresh table or database. If user want to see the data of the
table, user need to click selected table > Click data.
18
Python and MySQL for Beginner
02
Click Icon Setting > select Setting or used shortcut key Ctrl+ Alt + S (Figure 14).
19
Python and MySQL for Beginner
02
20
Python and MySQL for Beginner
If installation is successful a green bar will appear with a message stating that
the installation is successful. Please try again if not successful. Close the
python interpreter pop up screen and head to the code editor interface.
(Figure 18).
File name main.py is the default fail that will be run by the PyCharm (Figure 19).
1. Open the command prompt application: Press the Windows key to open the
start screen. In the search box type "command". Click on the command
prompt application.
2. Execute command: type pip install mysql-connector-python and press enter.
3. The advantage of using VSCode, the user only needs to install it once and
the module can be used by any Python project.
21
Python and MySQL for Beginner
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query
Language (SQL) is the language used for handling the RDBMS using commands i.e Creating,
Inserting, Updating and Deleting the data from the databases. SQL commands are case
insensitive i.e CREATE and create signify the same command.
MySQL Connector/Python enables Python programs to access MySQL databases, using an API
that is compliant with the Python Database API Specification v2.0 (PEP 249). It is written in pure
Python and does not have any dependencies except for the Python Standard Library.
22
Python and MySQL for Beginner
In this activity:
You will build a program with Create Read Update Delete (CRUD) method.
Each menu has it owns specific task. User can choose any menu even
there is no database has been created.
You will build your own function from scratch to fill in the selected menu.
03 Write the code and observe the output for each menu.
23
Python and MySQL for Beginner
Create module
Create your own module, name it as moduleDB in your directory with four(4)
submodules (Figure 22):
deleteData.py
displayData.py
insertData.py
updateData.py
24
Python and MySQL for Beginner
1 Create insertData.py
import mysql.connector
cursor = db.cursor()
sql = "insert into " + table_name + "(id, fname, lname, age,
phone_num, department) values(%s, %s, %s, %s, %s, %s)"
print(sql)
except:
# .rollback method sends a ROLLBACK statement to the MySQL
server,
# undoing all data changes from the current transaction.
# By default, Connector/Python does not autocommit,
# so it is possible to cancel transactions when using
transactional storage engines such as InnoDB.
db.rollback()
25
Python and MySQL for Beginner
2 Create deleteData.py
import mysql.connector
26
Python and MySQL for Beginner
3 Create displayData.py
import mysql.connector
# display data
def displayTableInfromation(host, username, password, database,table_name):
27
Python and MySQL for Beginner
4 Create updateData.py
import mysql.connector
cursor = db.cursor()
try:
sqlFormula = "UPDATE "+table_name+" SET fname = %s WHERE id = %s"
cursor.execute(sqlFormula, (fname, id))
db.commit()
except:
db.rollback()
28
Python and MySQL for Beginner
29
Python and MySQL for Beginner
except Error as e:
print(format(e))
for x in cursor:
print(x)
except Error as e:
print(format(e))
30
Python and MySQL for Beginner
31
Python and MySQL for Beginner
if choice == '1':
# go create_db function
create_db(u, p)
32
Python and MySQL for Beginner
References
Akhter, M. (2022, January 11). How to install Visual Studio Code on Windows 10/11 [2022 Update] Complete
Guide [Video]. YouTube. https://www.youtube.com/watch?v=JPZsB_6yHVo
Computer Science. (2021, November 13). Install Python 3.10 and PyCharm on Windows 10 [Video].
YouTube. https://www.youtube.com/watch?v=WJynvGY-2wk
Downey, A. (n.d.). Introduction to GUI programming. Python Textbook. Retrieved from https://python-
textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html
Laragon. (n.d.). Laragon - portable, isolated, fast & powerful universal development environment for PHP,
Node.js, Python, Java, Go, Ruby. Retrieved from https://laragon.org/download/
Ngo, J. (2021, February 3). PyCharm vs. VS Code: Which is the better code editor? [Blog post]. LogRocket.
https://blog.logrocket.com/pycharm-vs-
vscode/#:~:text=PyCharm%20and%20VS%20Code%20are,to%20an%20IDE%20through%20extensions.
Santos, A. (2021, November 5). How to Install Python 3.10.2 on Windows 10 [Video]. YouTube.
https://www.youtube.com/watch?v=uKHVNKd3f20
Sid Martin Biotechnology Institute. (n.d.). What is Laragon used for? Retrieved from
https://www.sidmartinbio.org/what-is-laragon-used-for/
33
POLITEKNIK MERSING
Jalan Nitar,
86800 Mersing
Johor Darul Ta’zim
Telephone : 07-7980001
Fax : 07-7980002
Website : https://pmj.mypolycc.edu.my/