Python DSC551 (Auto-Saved) (Auto-Saved)
Python DSC551 (Auto-Saved) (Auto-Saved)
Started With
Python
Programming For Data
Science (DSC551)
Object-Oriented
Programming
Introduction To Python Programming Languages
• Python has found numerous applications in various industries,
including:
• Web Development
• Data Analysis and Visualization
• Machine Learning and Artificial Intelligence:
• Scripting and Automation
• Game Development
Introduction To Python Programming Languages
• Python is a versatile and powerful programming language .
• Popular language
• Code readability and simplicity
• Excellent choice for both beginners and experienced developers.
Introduction To Python Programming Languages
• List of the five most popular and best programming languages
that will be in demand in 2023:
1. Javascript
2. Python
3. Go
4. Java
5. Kotlin
Introduction To Python Programming Languages
• Examples of games developed using Python
Introduction To Python Programming Languages
• Examples of apps developed using Python
Introduction To Python Programming Languages
• Pyhton vs Java
Python
score = int(input("Enter the score: "))
Java
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the score: "); int
score = scanner.nextInt();
Introduction To Python Programming Languages
• Comment is python precedes by the hash symbol (#)
• Example
#This program receives two numbers as input
Object-Oriented
Programming
Variables and Data Types
n = 5 n = 5 5
n = 7
n
n 5 7
Arithmetic Operations
• Python has numerous operators that can be used to perform mathematical calculations
as shown in the following table:
Symbol Operation Description
/ Division Divides one number by another and gives the result as a floating-
point number
// Integer Division Divides one number by another and gives the result as an integer
• String literals in python are enclosed either by single quote ('String’) or double quotes
(“String”).
• String declaration:
0 1 2 3 4 5 6 7 8 9 10 11
‘D’ ‘a’ ‘n’ ‘a’ ‘u’ ‘ ‘ ‘K’ ‘a’ ‘o’ ‘l’ ‘i’ ‘n’
Variables and Data Types : String
• String functions
Methods Example Description
title 'ben huR'.title() Capitalizes the first letter of each word and lowercases the
rest
rstrip 'ab ‘.rstrip() Remove spaces from the right side of the string
Variables and Data Types : List
• Each element in the list has a unique index starting from 0 the length - 1 as per illustrated the
following figure:
Variables and Data Types : List
• Creating a list using range keyword:
series = list(range(1,5))
• Split method
message = "Welcome to Bukit Keluang Beach Resort"
word = message.split(" ")
print(word)
output:
['Welcome', 'to', 'Bukit', 'Keluang', 'Beach', 'Resort']
Variables and Data Types : List
• Join method
greet = " ".join(word)
print(greet)
output:
Correct Method
Variables and Data Types : List
o l 2
1 2 3
Variables and Data Types : List
Variables and Data Types : List
0 1 2 3 4
(remove)
- 68
Variables and Data Types : List
Variables and Data Types : List
column
r
o
w
Variables and Data Types : List
[row] (column] ??
show
43000
-
0
O
Mambay 20000
I Rantay 18000
Variables and Data Types : List 2 Remban 45000
0 1 2
O 1 2
- row
without position 1
o 1 2
0
2
Variables and Data Types : List
- position
0
2
0 l Z
Variables and Data Types : List
Variables and Data Types : Dictionary
• A dictionary is an object that stores a collection of data.
• Examples:
To store information about employee’s id and employee’s name or person’s name and person’s phone
number.
permanent data
Variables and Data Types : Tuples
Example 2:
Variables and Data Types : Dictionary
name Phone number
Abu Bakar 019-4533454
Umar 014-3234345
Uthman 012-334545
Ali 012-3345456
Examples:
Variables and Data Types : Dictionary
O
Using the in and not in Operators
• in and not in operators can be used to test the value in a dictionary.
dictionary[key] = value
• Examples:
Variables and Data Types : Dictionary
Deleting Elements
• Syntax
del dictionary[key]
• Examples:
Variables and Data Types : Dictionary
Getting the number of elements in a Dictionary
• Syntax
len(dictionary)
• Examples:
Variables and Data Types : Dictionary
List as a data Types in a Dictionary
• Example:
• Example:
Value Value Value
dictionary_name = {}
• Example:
students = {}
Variables and Data Types : Dictionary
Using loops to Iterate over a Dictionary
• You can use the for loop in the following general format to iterate over all the keys in a dictionary:
ikat data
for var in dictionary:
statement_1
statement_2
..
statement_n
Variables and Data Types : Dictionary
Using loops to Iterate over a Dictionary
• Example:
Variables and Data Types : Dictionary
Example of Using loops to Iterate over a Dictionary
This loop iterates once for each element of the phonebook This loop iterates once for each element of the phonebook
dictionary. Each time the loop iterates, the key is assigned to dictionary. Each time the loop iterates, the key is assigned
the variable. The print() function is called to print the value the key variable. The print() function is called to print the
of the key variable. value of the key variable and its associated value.
✓
Variables and Data Types : Dictionary
• There are several methods can be applied to a dictionary. The following list shows some useful
methods.
Method Description
get Gets the value associated with a specified key. If the key is not found, the method does not
raise an exception. Instead, it returns a default value.
items Returns all the keys in a dictionary and their associated values as a sequence of tuples
pop Returns all the values associated with specified key and removes that key-value pair from
the dictionary. If the key is not found, the method returns a default value.
popitem Returns a randomly selected key-value pair as a tuple from the dictionary and removes that
key-value pair from the dictionary.
Object-Oriented
Programming
Input and Output Operations
• Print function
• General Syntax:
print(output1, output2, … outputN)
• Examples:
print('The sum is : ', sum)
Input and Output Operations
• Input function
• Syntax:
variable = input('Prompt message : ')
• Examples
var = input('Enter an input : ')
amount = float(input('Enter an fee RM:'))
score = float(input('Enter the score :'))
Topics
Introduction to Python Variables and Data Types in
Programming Language Python
Object-Oriented
Programming
Decision Structure
if condition:
indented block of statements
else:
indented block of statements
Decision Structure : if-else statement
Decision Structure : Nested if statement
if condition:
if condition:
indented block of statements
else:
indented block of statements
else:
if condition:
indented block of statements
else:
indented block of statements
Decision Structure : Nested if statement
Decision Structure : elif clause
if condition1:
indented block of statements if condition1 is True
elif condition2:
indented block of statements if condition2 is True
elif condition3:
else:
indented block of statements if non of the above
Packages
14/4/2025- Monday
• Syntax:
for var in sequence:
indented block of statements
Repetition Structure : for statement
Repetition Structure : for statement
infile = open(“filename.txt”,’r’)
for line in infile:
indented block of statements
infile.close()
Repetition Structure : for statement
dataList = []
infile = open(“Data.txt”, ‘r’)
for line in infile:
datalist.append(line.strip())
infile.close()
Repetition Structure : while statement
While Statement
General format:
Conditio False
n
while condition:
statement
True
statement
statement Statement(s)
Repetition Structure : while statement
While Statement count 0
General format:
False
count < 5
count = 0
while count < 5: True
print(‘Hello World’) Print
count = count + 1 ‘Hello World’
Add 1 to count
Topics
Introduction to Python Variables and Data Types in
Programming Language Python
Packages
18/4/2025
What is a function?
• A group of statements that exists within a program for the purpose of performing a specific
task.
sequence of
statement Modularise each of which is performed by a
statement
statements statement
def function(): Separate function.
statement
statement
statement
statement
statement
statement
Statement
statement def function():
statement statement
statement statement
statement statement
statement
Defining and calling a function
• The code for a function is known as a function definition.
Example:
Defining and Calling a function
2
The interpreter jumps to 4
the message() function and
begins executing the When the message()
statements in its block. function ends, the
interpreter jumps back to
the part of the program
that called it and resumes
3 execution from that points
1
The interpreter jumps to
the main() function and When the main() function
begins executing the ends, the interpreter
statements in its block. jumps back to the part of
the program that called it.
If there is no statements,
the program will end
Local Variables
• A local variable is created inside a function and cannot be accessed by the statement that
are outside the function.
• Different function can have local variables with the same names because the function cannot
see each other’s local variables.
• A variable’s scope is the part of a program in which the variable may be accessed.
texas()
birds 5000
local (
var.
california()
birds 8000
Passing Arguments to Functions
• An argument is any piece of data that is passed into a function when the function is called.
number
5
result 10
show_double()
10
screen
The sum of 12 and 45 is 57
The sum of 12 and 45 is 57
12 45
num1 num2
12 45
result 57
show_sum()
screen
Keyword Arguments
• Python allows you to write an argument in the following format:
parameter_name = value
main()
34 50
num1 num2
34 50
result 84
show_sum()
• Write a function called computeSum() that receive two integers as parameter, and this
function will compute and display the sum of these numbers.
num2 = 80
The main function will call computeSum() function and sent num1 and num2 as arguments.
Global Variables
my_value
• A global variable is accessible to all function in a program file.
34
show_value()
print (my_value)
34
screen
• Python does not allow you to create true global constants, but you can simulate them with
global variable.
• Usually, global constant is declared using capital letter as follows:
GST_RATE = 0.05
Question
VALUE RETURNING FUNCTION
• A value returning function is a function that returns a value back to the part of the program
that called it.
VALUE RETURNING FUNCTION
Topics
Introduction to Python Variables and Data Types in
Programming Language Python
Object-Oriented
Programming
2/5/2025
JSON
• JSON (JavaScript Object Notation) is a lightweight data interchange
format that is easy for humans to read and write, and easy for
machines to parse and generate.
• Primarily used to transmit data between a server and a web
application as an alternative to XML.
• JSON structures data in key-value pairs, making it a popular choice for
APIs and configuration files.
JSON
• In Python, JSON is supported natively through the json module, which
provides functions to work with JSON data.
• This allows Python developers to easily convert Python objects (like
dictionaries and lists) to JSON format and vice versa.
• The process of converting a Python object into JSON is known
as serialization, while converting JSON back into a Python object is
called deserialization.
JSON
• Serialization: This is the process of converting a Python object
(typically a dictionary) into a JSON-formatted string.
• For example, a Python dictionary can be transformed into a JSON
string that can be easily stored or transmitted
• In Python, this can be done using the json.dumps() function,
which takes a Python object and returns a JSON string.
JSON
• Deserialization: This is the reverse process, where a JSON-
formatted string is converted back into a Python object.
• This can be accomplished using the json.loads() function, which
takes a JSON string and returns the corresponding Python object
JSON
• In summary, JSON serves as a bridge for data exchange between
different programming environments, and Python's built-in
capabilities for serialization and deserialization make it
straightforward to work with JSON data.
JSON (Example 1: Simple Object)
JSON (Example 2: Array of Objects)
JSON (Example 3: Nested Objects)
JSON (Example 1: Serializing a Simple Dictionary)
convert Python object into seen data
type (data)
JSON (Example 4: Deserializing JSON with Mixed Data Types)
JSON (Example 5: Deserializing JSON into Custom Objects)
Packages : matplotlib
• Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in
Python. Matplotlib makes easy things easy and hard things possible.