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

01 Chapter 1 - Python Programming Language_P1 2

The document is an introductory guide to Python programming, covering essential concepts such as coding, algorithms, data types, and the use of development environments like PyCharm. It emphasizes the importance of planning and debugging in programming, as well as the various stages involved in problem-solving using computers. Additionally, it provides examples of algorithms, flowcharts, and assignments to reinforce learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

01 Chapter 1 - Python Programming Language_P1 2

The document is an introductory guide to Python programming, covering essential concepts such as coding, algorithms, data types, and the use of development environments like PyCharm. It emphasizes the importance of planning and debugging in programming, as well as the various stages involved in problem-solving using computers. Additionally, it provides examples of algorithms, flowcharts, and assignments to reinforce learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

NATIONAL ECONOMICS UNIVERSITY

NEU COLLEGE OF TECHNOLOGY


FACULTY OF INFORMATION TECHNOLOGY

PYTHON PROGRAMMING COURSE

CHAPTER 1: INTRODUCTION TO PYTHON


LECTURER

MSC. PHAM THAO


Senior Lecture
0966 986 689
[email protected]
https://fit.neu.edu.vn/lecturer/th-s-pham-thao
CHAPTER 1: INTRODUCTION TO PYTHON

Software is a set of
A software is a set of If the instructions are
instructions on the
programs written to not correct, the
basis of which
solve a particular computer gives wrong
computer gives
problem result.
output/result.

Never Ever Forget


• Just writing code is not sufficient to solve a problem.
• Program must be planned before coding in any computer language available.
• There are many activities to be done before and after writing code.
CHAPTER 1: INTRODUCTION TO PYTHON
STAGES WHILE SOLVING A PROBLEM USING COMPUTER

Problem Algorithm Compilation Debugging and


Flowcharting Coding Documentation
Analysis Development and Execution Testing

4
CHAPTER 1: INTRODUCTION TO PYTHON
CODING

The process of transforming the program logic design into computer language format.

An act of transforming operations in each box of the flowchart in terms of the statement of the program.

The code written using programming language is also known as source code.

Coding isn’t the only task to be done to solve a problem using computer.

Anyone can code.


CHAPTER 1: INTRODUCTION TO PYTHON
WHY PYTHON Top Programming Languages 2024

https://spectrum.ieee.org/top-programming-languages-2024 6
CHAPTER 1: INTRODUCTION TO PYTHON
WHY PYTHON
 Unlike Java, C, C++, R, and PHP, Python is witnessing incredible
popularity and growth lately.
 Python is defined as a general-purpose high-level programming
language with versatility with the feature of building different
functions (Rossum, 2018).
 Python is a dynamic, open-source, and high-level programming
language that supports object-oriented and procedural
programming.
 The Python language is an interpreted language that does not
depend on compilers and is executed line by line.
 This also makes the language versatile and does not have any
operating system preferences.
CHAPTER 1: INTRODUCTION TO PYTHON
WHY PYTHON
 Python is widely used in various fields, including data
science, web development, and artificial intelligence.
 Some of the key elements of
CHAPTER 1: INTRODUCTION TO PYTHON
WHY PYTHON TYPES DESCRIPTION EXAMPLES
Represents numerical
 In Python, data types are Numeric 'int' , 'float' , 'complex'
values
categories of values that Represents a collection
determine how the Sequence of ordered and indexed 'list' , 'tuple' , 'range'
values behave and what values
operations can be Represents a sequence
performed on them. Text 'str'
of characters
 Here are some common
Represents a collection
data types and their Mapping 'dict'
of key-value pairs
examples:
Represents a collection
Set 'set' , 'frozenset'
of unique elements
Represents a binary
Boolean 'bool'
truth value
'bytes' , 'bytearray' ,
Binary Represents binary data
'memoryview'
CHAPTER 1: INTRODUCTION TO PYTHON
START WITH PYCHARM

https://www.jetbrains.com/pycharm/
CHAPTER 1: INTRODUCTION TO PYTHON
START WITH PYCHARM

 Download and Install PyCharm  2. Launch PyCharm and Set Up


• Go to the official PyCharm website. the IDE
• Choose between the Community • Open PyCharm after installation.
Edition (free) or Professional • On the welcome screen:
Edition (paid).
• Choose “New Project” to start fresh.
• Download the installer for your
operating system (Windows, macOS, or • Or select “Open” to work on an existing
Linux). project.

• Follow the installation instructions for


your OS.

https://www.jetbrains.com/pycharm/
CHAPTER 1: INTRODUCTION TO PYTHON
START WITH PYCHARM
 In Python, we’ll try to create a variable
A variable in Python is just a name called "age" and assign a value to it, like
that represents a value, and you can this:
use it to store and manipulate data
throughout your program.

 Now, whenever you need to use that age


Let’s understand the same with a value in your program, you can just use the
small example:
variable name "age".
The output will be:  For example:
CHAPTER 1: INTRODUCTION TO PYTHON
START WITH PYCHARM
CHAPTER 1: INTRODUCTION TO PYTHON
START WITH JUPITER NOTEBOOK

https://docs.anaconda.com/anaconda/install/
TYPES DESCRIPTION EXAMPLES

Chapter 1: Introduction Arithmetic Used to perform


'2 + 3' returns '5'
to Python Operators arithmetic operations

Comparison
Operators: Operators
Used to compare
values
'2 < 3' returns 'True'

Used to combine and


Logical 'True and False' returns
manipulate boolean
Operators 'False'
values

Bitwise Used to perform


'5 & 3' returns '1'
Operators bitwise operations

Assignment Used to assign values


'x=5'
Operators to variables

Used to compare the


Identity
memory location of 'x is y' returns 'True'
Operators
two objects
Used to check if a
Membership '2 in [1, 2, 3]' return
value is present in a
Operators 'True'
sequence
COMPILATION EXECUTION

Once the compilation is completed then the


Process of changing high level language into program is linked with other object programs
machine level language. needed for execution, there by resulting in a
binary program and then the program is
loaded in the memory for the purpose of
It is done by special software, execution and finally it is executed.
COMPILER

The compilation process tests the program


whether it contains syntax errors or not. The program may ask user for inputs and
generates outputs after processing the inputs.
If syntax errors are present, compiler can
not compile the code.
CHAPTER 1: INTRODUCTION TO PYTHON
ALGORITHM DEVELOPMENT

Algorithm to Find the Sum of Two


Numbers:
Start
Declare a variable sum = 0
Input or assume two numbers x and y
Add x and y and store the result in the variable sum
Print the value of sum
End
TYPES DESCRIPTION EXAMPLES

Executes a block 'x=5' <br>


If

CHAPTER 1: statement
of code if a condition
is true
'if x>3:'<br>
'print("x is greater than 3")'

INTRODUC Executes one block


'x=2' <br>

TION TO
'if x>3: '<br>
If else of code if a condition
'print("x is greater than 3")' <br>
statement is true and another

PYTHON block if it is false


'else:' <br>
'print("x is less than or equal to 3")'

Conditional 'x = 5' <br>


'if x > 7: ' <br>

Statements: 'print("x is

Executes a block of greater than 7")' <br>


elif
code if a condition is true, and if not, 'elif x > 3: '<br>
statement
checks another condition 'print("x is greater than 3 but less

than or equal to 7")' <br>


'else:' <br>
'print("x is less than or equal to 3")'

'x = 5' <br>


Executes a block of 'if x > 3:' <br>
CHAPTER 1: INTRODUCTION TO PYTHON
IF ELSE FLOWCHART

Chương 1: Tổng quan về Công nghệ thông tin


20
CHAPTER 1: INTRODUCTION TO PYTHON
IF ELSE FLOWCHART

Chương 1: Tổng quan về Công nghệ thông tin


21
CHAPTER 1: INTRODUCTION TO PYTHON
ALGORITHM DEVELOPMENT
An algorithm to find largest
of three different numbers:
CHAPTER 1: INTRODUCTION TO PYTHON
ALGORITHM DEVELOPMENT
Algorithm to Find the Sum of Two Numbers:
Start
Declare a variable sum = 0
Input or assume two numbers x and y
Add x and y, and store the result in the variable sum
Print the value of sum
Decision: If you want to try again with different numbers:
Go to Step 3
Else,
Proceed to Step 7
End
CHAPTER 1: INTRODUCTION TO PYTHON
ALGORITHM DEVELOPMENT
CHAPTER 1: INTRODUCTION TO PYTHON
THREE FEATURES OF ALGORITHM

• Each step in the algorithm in executed in specified order. If not


Sequence
algorithm will fail.

• We have to make decision to do something.


• If the outcome of the decision is true, one thing is done
otherwise other.
• If condition then process1
Decision
• OR
• If condition
• then process1
• Else process2
• For example
• Repeat
Repetition • Fill Water in the kettle
• Until Kettle is full
Loops: LO O P T Y P E DESCRIPTION

Loops are used to repeat a block of code multiple


Used to iterate over a sequence of elements,
times until a certain condition is met. There are two 'for' loop
such as a list or string

main types of loops in Python: for loops and while


Used to repeat a block of code while a certain
loops. 'while' loop
condition is true
Here's an example of a for loop that iterates over a
list of numbers and prints each number along with
the output. In this example, the loop will continue to
FOR
run as long as count is greater than 0. Each time the
loop runs, it prints the value of count, then subtracts
LOOP
1 from it. Eventually, count becomes 0, and the loop
stops running. INPUT OUTPUT
There are also other types of loops in Python, such as
nested loops, which allow you to put one loop inside
another, and break and continue statements, which
WHILE
allow you to control the flow of the loop. LOOP
CHAPTER 1: INTRODUCTION TO PYTHON
LOOP FLOWCHART

Chương 1: Tổng quan về Công nghệ thông tin


27
WHILE AND DO WHILE FLOW CHART

Chương 1: Tổng quan về Công nghệ thông tin


28
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS

Write an algorithm and draw flowchart to determine a number whether


it is positive or negative.

Write an algorithm and draw flowchart to test a number for even or


odd.
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 Write an algorithm and draw flowchart to determine a number whether it is
positive or negative.
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 EX1: Write an algorithm and draw flowchart to determine a number whether it is positive or negative.
 PyCharm → New Python File
 File name: Ex1_Positive_Negative.py
 Write code as follow
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 EX1: Write an algorithm and draw flowchart to determine a number whether it is positive or negative.
 PyCharm → New Python File
 File name: Ex1_Positive_Negative.py
 Write code as follow
CHAPTER 1: INTRODUCTION TO PYTHON
DEBUGGING AND TESTING

Testing ensures that program performs


Debugging is the discovery and correctly the required task.
correction of programming errors.
Verification ensures that program does what
the programmer intends to do.
Some errors may remain in the
program because the Validation ensures that the program produces
designer/programmer might have the correct results for a set of test data.
never thought about a particular case.
Test data are supplied to the program and
output is observed.
When error appears debugging is
necessary. Expected output = Error free
CHAPTER 1: INTRODUCTION TO PYTHON
DEBUGGING AND TESTING
 EX1: Write an algorithm and draw flowchart to
determine a number whether it is positive or negative.
 Rund and Debug by Click on Debug button
 or Menu Run → Debug or Alt+Shift+F9
 Watch windows, type expression,
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 EX1: Write an algorithm and draw flowchart to
determine a number whether it is positive or
negative.
 Then, check Even or Odd number
 Use Nested IF
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 EX1: Write an algorithm and draw
flowchart to determine a number
whether it is positive or negative.
 Then, check Even or Odd number
 Use If elif…
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 Write an algorithm and draw flowchart to find the largest number among three
numbers.
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS

Print all items from a list

Print all items from a list with their position

Write an algorithm and draw flowchart to find the largest number from a
list
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 Write an algorithm and draw flowchart of print all items from a list
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS
 Write an algorithm and draw flowchart to find the largest number from a list
CHAPTER 1: INTRODUCTION TO PYTHON
ASSIGNMENTS

01 02 03
Read N numbers For finding the sum For calculating the
from user and of the series factorial of a given
display sum of all 1+2+3+4+… up to number N.
entered numbers. N terms
PROGRAM DOCUMENTATION

Helps to those who use, maintain and extend the program in future.

A program may be difficult to understand even to programmer who


wrote the code after some days.

Properly documented program is necessary which will be useful and


efficient in debugging, testing, maintenance and redesign process.
TWO TYPES OF DOCUMENTATIONS

Programmer’s Documentation Maintain, redesign and upgrade


(Technical Documentation) Logic, DFD, E-R, algorithm and flowchart

Support to the user of the program


User Documentation (User Manual) Instructions for installation of the program
CHAPTER 1: INTRODUCTION TO PYTHON
EXAMPLES: FIND A MAX NUMBER
 Natural Language Description: Flowchart:
1. Input: Start by reading a list of numbers from 1. Start
the user.
2. Input: Read the list of numbers
2. Initialize: Set the first number in the list as
the current maximum. 3. Initialize: Set the first number as max
3. Iterate through the list: Compare each 4. Loop: For each number in the list:
number in the list with the current maximum.
1. Compare the current number with max
1. If the current number is larger than the current
maximum, update the current maximum to this 2. If the current number is greater, set max to this
number. number
4. Output: After checking all numbers, the final 5. Output: Display the value of max
value of the current maximum is the largest
number in the list. 6. End
5. End.

Chương 1: Tổng quan về Công nghệ thông tin


44
CHAPTER 1: INTRODUCTION TO PYTHON
EXAMPLES: FIND A MAX NUMBER

 Example 1: Finding the largest Idea


value – Max of a sequence of N
integers  Initialize Max = a1
 Input: A positive integer N and N
integers a1,a2,…,aN
 Sequentially compare Max
 Output: The largest integer of the
with ai for i=2,3,…,N
sequence ak, where k is in the range  if ai>Max,
[1...N]
 update Max with the new value.

Chương 1: Tổng quan về Công nghệ thông tin


45
CHAPTER 1: INTRODUCTION TO PYTHON
SOME EXAMPLES: FIND A MAX NUMBER
 Example 1: Finding the largest value –
Max of a sequence of N integers

Chương 1: Tổng quan về Công nghệ thông tin


46
CHAPTER 1: INTRODUCTION TO PYTHON
EXAMPLES: FIND A MAX NUMBER

Chương 1: Tổng quan về Công nghệ thông tin


47
EXAMPLES: FIND THE POSITION OF MAX NUMBER

Chương 1: Tổng quan về Công nghệ thông tin


48
EXERCISE

 Presenting the algorithm using natural language and flowcharts to solve the
following problems:
 Exercise 1: Find the minimum value from a sequence of n numbers entered from the
keyboard.
 Exercise 2: Input a natural number nnn and calculate the sum: S=1+2+3+⋯+n
 Exercise 3: Input a natural number nnn and calculate the factorial GT=n!
 Exercise 4: Input n numbers from the keyboard and sort the sequence in ascending order.
 Exercise 5: Input n numbers from the keyboard and sort the sequence in descending order.
 Exercise 6: Solve a quadratic equation with three coefficients a, b, and c.

Chương 1: Tổng quan về Công nghệ thông tin


49
EXERCISE
 Enter n numbers from the keyboard and sort them in ascending order.

Chương 1: Tổng quan về Công nghệ thông tin


50
EXERCISE

 Enter n numbers from the keyboard and sort them in ascending order.

Chương 1: Tổng quan về Công nghệ thông tin


51
PRACTICE
 Exercise 1: Given a list of numbers, calculate the sum of all elements in the list.
 Exercise 2: Given a list of numbers, calculate the average, maximum, and minimum values of the elements in the list.
 Exercise 3: Given a list and a value entered via the keyboard, determine whether the value exists in the list.
 Exercise 4: Given a list and a value entered via the keyboard, determine whether the value exists in the list. If it does,
return the index of its first occurrence.
 Exercise 5: Given a list and a value entered via the keyboard, determine whether the value exists in the list. If it does,
return the index of its last occurrence.
 Exercise 6: Given a list and a value entered via the keyboard, count the number of times the value appears in the list.
 Exercise 7: Given a list and a value entered via the keyboard, if the value exists in the list, remove the element at its first
occurrence.
 Exercise 8: Given a list and a value entered via the keyboard, remove all instances of the value from the list.
 Exercise 9: Given a list, create and output a set containing only the unique (non-repeating) elements of the list.
 Exercise 10: Given a list, reverse the order of the elements and print the reversed list.
 Exercise 11: Given a list of numbers, sort the elements in ascending order.
 Exercise 12: Given a list of numbers, sort the elements in descending order.
 Exercise 13: Given two lists, A and B, find and output their intersection (common elements).
 Exercise 14: Given two lists, A and B, find and output their union (all unique elements combined).
 Exercise 15: Given a list, count and display the number of occurrences of each value in the list.
52
EXERCISE

 Enter n numbers from the keyboard


and sort them in ascending order.

Chương 1: Tổng quan về Công nghệ thông tin


53
EXERCISE

Chương 1: Tổng quan về Công nghệ thông tin


54
EXERCISE

 Exercise 4: Input n numbers from the keyboard and sort the sequence in ascending
order.

Chương 1: Tổng quan về Công nghệ thông tin


55

You might also like