0% found this document useful (0 votes)
39 views

Lec 01 - Python Basic

Uploaded by

Abc
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)
39 views

Lec 01 - Python Basic

Uploaded by

Abc
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/ 20

Chapter 1

Python Basic

Wear A Mask Properly


You are encouraged to wear a mask during the lecture class
and lab session.

1
Learning Outcomes
 Explain the fundamental concept of python
programming and security knowledge
 Perform different programming structures and
executions for python program
 Propose solution to security field using python
programming through teamwork

Mark Distribution
Coursework (100%)
1. Continuous Assessment (70%)
 Quizzes 20%
 Test 30%
 Assignment 20%
2. Final Assessment (30%)
 Project 30%

2
Introduction
 Python is an interpreted, high-level and general-purpose
programming language.
 Python is case sensitive.
 It is free and open-source.
 Python can be used for things like:
 Desktop app and software development
 Processing big data and performing mathematical computations
 Back-end (or server-side) web and mobile app development
 Writing system scripts (creating instructions that tell a computer
system to “do” something)
5

History

1980 1991 1994 2000 2008

• Python was • Python code • Python 1.0 was • Python 2.0 was • Python 3.0 was
conceived by ver 0.9.0 was released introduced released
Guido van published
Rossum

3
Compiler vs Interpreter
 Compiler and interpreter transform source code (written in
high-level programming language) into the machine code.
 Compiler:
 It takes entire program as input
 The compiler takes a full program at a time and compiles them.
 This compilation is done before execution.
 Interpreter:
 It takes single instruction as input
 It executes the program line by line at a time.

Compiler vs Interpreter

Machine
How Compiler works: Source
Compiler code Output
code

How Interpreter works: Source


Interpreter Output
code

4
Working with Python
 The python program is first compiled and then interpreted.
 Python Interpreter:
 Source code is compiled into byte code
 Byte code is executed line by line on the Python Virtual Machine (PVM)
 Program will run and display the output.
Library
Modules
Python Interpreter
Source Byte
code Compiler code PVM Output
(x.py) (x.pyc)

Installation
 Miniconda
 Download Python Executable Installer (Python 3.8, 3.9, 3.10)
 Run Executable Installer
 Jupyter Notebook
 Install Jupyter Notebook:
 Anaconda Prompt>> conda install -c conda-forge notebook
 Open Jupyter Notebook:
 Anaconda Prompt >> jupyter notebook

10

5
Miniconda
 The python interpreter have two mode:
 Interactive mode and Script mode.
 Interactive mode:
 It run the Python code in block or a single line
 In the Anaconda Prompt, type “python” to go into the Python shell

 The >>> is named as Python Prompt. It indicates that the Python shell is
ready to execute and send the commands to the Python interpreter.

11

Miniconda
 Script mode:
 In the Anaconda Prompt, type "idle"
to open the Python shell.
 In the Python shell, click "File" then
choose "New" to open a blank script.
 Write the Python code into the blank
script and save the file.
 Run the script by clicking "Run" then
"Run Module".
 Result will be displayed on the
Python shell.

12

6
Python IDE
 Python IDE is a development environment for Python.
 It is a graphical editor in which the programmer can type code, work
with multiple files, testing, debugging and etc.
 It helps programmer to manage a large codebase and achieve quick
deployment.
 IDLE is a decent IDE for learning as it is lightweight and simple to
use. However, it is only for small projects.

Jupyter Notebook
 It is web based front-end to IPython (Interactive Python) kernel.
 IPython is an enhanced interactive environment for Python with
many functionalities compared to the standard Python shell.

7
Feature of Python
 Easy to learn and use
 Python is easy to learn as compared to other programming languages.
 Program statement does not end with semicolons.
 Indentation is used to define a code block, instead of curly braces.
 Expressive Language
 Python can perform complex tasks using a few lines of code.
 Ex, the hello world program – just simply type print("Hello World").
 Free and Open-Source
 Python is freely available, it can be download from the Python Website.
 Python is open-source and its source code is available to the public.
 Everyone can download it, change it, use it, and distribute it
 Interpreted Language
 Python is an interpreted language that the program is executed one line at a time.

Feature of Python
 Cross-platform Language
 Python can run equally on different platforms such as Windows, Linux, UNIX, and
Macintosh, etc.
 It enables programmers to develop the software for several competing platforms
by writing a program only once.
 Object-Oriented Language
 Python has all features of an object-oriented language such as inheritance,
polymorphism, objects, etc.
 It also supports the implementation of multiple inheritances, unlike java.
 Extensible
 Python code can be written in other languages such as Java, C++ and others.
 Python source code can be embedded in another programming language as well.
 Ex, if programmer wish not to expose any piece of code or algorithm, he/she can
write them in C or C++ and use the same from Python program.

8
Feature of Python
 Large Standard Library
 It provides a vast range of libraries for the various fields such as machine learning,
web developer, and also for the scripting.
 GUI Programming Support
 Graphical User Interface is used for the developing Desktop application.
 Libraries such as PyQT5, Tkinter, Kivy are used to develop the web application.
 Embeddable
 The code of other programming language can use in the Python source code.
 Ex, programmer can embed Python with C or C++ programs.
 Dynamic Memory Allocation
 Python does no need to specify the data-type of the variable.
 When value is assigned to the variable, it automatically allocates the memory to
the variable at run time.

Variable
 Variables are containers for storing data values.
 A variable is created the moment a value is assigned to it.
 Example:
 _name = "walk"
 na23me = 'I'
 x = y = z = 25.6 // assign single value to multiple variables
 a, b, c = 5, 'B', 6 // assign multiple values to multiple variables

print() - to display value of the variable. type() - to get the type of the variable.
print(_name) type(a) <class 'int'>
print(x, y, z, a, b, c, na23me) type(b) <class 'string'>

9
Variable Naming Rule
 Variable names must begin with an alphabet or underscore
 Variable names are case-sensitive
 Variable names must not contain any white-space, or special
character (!, @, #, %, ^, &, *).
 Examples of valid variable name: a123, _n, n_9, etc.
 Examples of invalid variable name: 1a, n%4, n 9, etc.

Variable
 Local variable
 It is created inside the function and have scope within the function.
 Global variable
 It is created outside the function and have scope in the entire program.
 To create a global variable inside a function, the global keyword is
used.
To change the value of a global
variable inside a function, refer to
the variable by using the global
keyword.

10
Variable

Data Type
 Numeric type
 int: number without a decimal point.
 e.g. -5, 5, 236, 235786.
 float: number that has a decimal point.
 e.g. 0.98, 3.983, 7.451189e2.
 complex: number with a real and imaginary component
(format: a + bi).
 e.g. 8.19j, 2.0 + 2.5j, 3.7 + 4.6j

 Boolean type
 It provides two built-in values, True (non-zero value or ‘T’) and
False (0 or ‘F’)

11
Data Type
 Sequence type
 String: the sequence of characters represented in quotation marks
 e.g. 'hello', "good day", "welcome"

 List: It contain collection of the items of different data types.


The items stored are enclosed in square brackets [].
 e.g. ['security', ‘data science', 2006, 2010], [1, 2, 3, 4, 5]

 Tuple: It contain collection of the items of different data types.


The items stored are enclosed in parentheses ().
 e.g. ('melaka', 'fist', 2020, 'cyberjaya', 'fci', 2021)

Data Type
 Set type
 It contains collection of the items of different data types.
 The set elements are unique.
 Sets can be used to perform mathematical set operations like
union, intersection, symmetric difference, etc.
 e.g. {'fist', 'fet', 'fob', 'fol'}, {1, 2, 3, 4, 5}

 Dictionary type
 It consists of a collection of key-value pairs.
 Each key-value pair maps the key to its associated value.
 e.g. {'Melaka': 98, 'Johor': 139, 'Pahang': 27}

12
String Literals
 String literals - text enclosed in the quotes.
 Python does not have a character data type, a single character is
just a string with a length of 1.
 Single and double quotes are used to create a string.
 Single-line String – String terminated within a single-line
 name1 = 'user' or name1 = "user"
 Multi-line String – A piece of text written in multiple lines.
name2 = 'hello\ name3 = '''welcome
user' to
FIST'''
single or double quotes

Comment
 Comment - hash(#) symbol is used
 It provides explanatory information about the source code.
 It helps a developer to explain logic of the code and improves
program readability.
 To help other developers to clearly understand the logic used in
the program.

# This is the print statement


print("Hello Python")

13
Expression
 Expression is a combination of values, variables,
operators, and calls to functions.
 In the Python prompt, the interpreter evaluates the expression
and displays the result, which is always a value.

operand operator operand

 A value all by itself is a simple expression,


and so is a variable.

Statement
 Statement is instruction that a Python interpreter can execute.
 Other types of statements: assignment statement, conditional
statement, looping statements etc.
 Ex. num = 2 is an assignment statement.

 Multi-line Statement:
 In Python, the end of a statement is
marked by a newline character.
 The statement can be extended over
multiple lines with line continuation
character \, parentheses (), square
brackets [], braces {}, and semicolon ;

14
Operators
 Arithmetic Operator
 It is used to perform arithmetic operations between two operands.
 Assuming that a=5 and b=3.
Operator Description Example

+ Addition a+b=8

- Subtraction a-b=2

/ Divide a/b = 1.67

* Multiplication a * b = 15

% Modulo a%b = 2

** Exponent a**b = 125

// Floor division a//b = 1

Operators
Operator Example
 Assignment Operator
= a=5
 It is used to assign values b=2
to variables. += a += b
a=a+b=7

-= a -= b
a=a-b=3

*= a *= b
a = a * b = 10

%= a %= b
a=a%b=1

**= a**=b
a = a**b = 25

//= a//=b
a = a // b = 2

15
Operators
 Comparison Operator
 It returns a Boolean value either True or False.
Operator Description Example

== True if the operands are equal x == y

!= True if the operands are not equal x != y

<= True if the left operand is lower than or equal to the right one x <= y

>= True if the left operand is higher than or equal to the right one x >= y

> True if the left operand is higher than the right one x>y

< True if the left operand is lower than right one x<y

Operators
 Logical Operator
 Two operands should have Boolean value True or False.
 Assuming that x=True and y=False.
Operator Description Example

and True if both are true x and y

or True if at least one is true x or y

not Returns True if an expression evalutes to false not x


and vice-versa

16
Operators
0000 0111
 Bitwise Operator 0000 0011
 It perform bit by bit operation on the values of the two operands.
 If x=7, y=3 => binary (x) = 0000 0111, binary (y) = 0000 0011
Operator Description Example

& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the result. x&y
Otherwise, 0 is copied. =0000 0011=3
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1. x|y
=0000 0111=7
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0. x^y
=0000 0100=4
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will ~x = -(7+1) = -8 OR
be 1 and vice versa. (Same as -(x+1)) ~x = -(0000 0111+1)
= -(0000 1000)
= -8
<< (left shift) The left operand value is moved left by the number of bits present in the right operand. (x<<2)
= 0001 1100=28
>> (right shift) The left operand is moved right by the number of bits present in the right operand. (x>>2)
=0000 0001=1

Operators
 Bitwise Left Shift Operator

Picture source: https://www.log2base2.com/programming-language/python3/bitwise/bitwise-left-shift-operator-in-python.html

17
Operators
 Bitwise Right Shift Operator

Picture source: https://www.log2base2.com/programming-language/python3/bitwise/bitwise-right-shift-operator-in-python.html

Operators
 Bitwise Complement Operator

Picture source: https://www.log2base2.com/programming-language/python3/bitwise/bitwise-ones-complement-operator-in-python.html

18
Operators
 Identity Operator
 It is used to check if two values (or variables) are located on the
same part of the memory.
Operator Description x = ["AI", "DCN", "ST"]
y = ["AI", "DCN", "ST"]
z=x
is True if the operands are identical
(refer to the same object) print(x is z)
# returns True because z is the same object as x
is not True if the operands are not identical
(refer to the different object) print(x is not y)
# returns True because x is not the same object as y,
even if they have the same content

print(x == y)
# to show the difference between "is" and "==": this
comparison returns True because x is equal to y

Operators
 Membership Operator
 It is used to test whether a value or variable is found in a
sequence (string, list, tuple, set and dictionary).
 In a dictionary the test is applied for the key, not the value.
Operator Description x = 'Hello world'

# Output: True
True if value/variable is found in the print('H' in x)
in
sequence
# Output: True
print('hello' not in x)
True if value/variable is not found in
not in
the sequence

19
Operators
 Operator Precedence

Picture source:
https://medium.com/
@thoashook/operatio
ns-in-python-
69bbbef781a4

20

You might also like