TALENTS FOR DEVELOPMENT - Petrovietnam UniversityChapter 3: Fundamental conceptsLecturer: Tran Ngoc Hiep, MSc.Email: hieptn@pvu.edu.vnMobile: 0901 252468ContentsStructure of Python prog
Trang 1TALENTS FOR DEVELOPMENT - Petrovietnam University
Chapter 3: Fundamental concepts
Lecturer: Tran Ngoc Hiep, MSc.
Email: hieptn@pvu.edu.vn Mobile: 0901 252468
Contents
Structure of
Python program Basic concepts Variables
Python Basic Data Types Input – Output in Python 1
Trang 2Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
Sample program
3
3.1 Structure of Python program
3
Trang 3Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.2 Basic concept
In Python, each individual statement must be ended with “enter”
5
Semicolons :
3.2 Basic concept Block in Python:
Python uses indentation to indicate a block of code
All the statements inside the block is treated as
one unit Blocks are used as the body in constructs
like function, if-else and loop, which may contain
multiple statements but are treated as one unit.
5
Trang 4Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.2 Basic concept Comments:
Program comments are explanatory statements
that you can include in Python code and helps
anyone easier to read and understand.
Python support single-line and multi-line
comments
Single Line Comment Multi Line Comment
7
3.2 Basic concept Keyword :
7
Trang 5Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.3 Variables
A variable is a location in computer memory
where a value can be stored
Variables must be declared before they are used in
a program.
9
3.3 Variables
Variable
Name
Type
Size Value
9
Trang 6Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.3 Variables Variable naming:
We can naming the variables by using:
Upper case letter
Lower case letter
Digits (0 to 9)
Underscore
Keywords: can not be used as variable name.
Example:
Number, phoneNumber, _firstName…
11
3.3 Variables
1 • Always the first character of a variable must be a letter or an underscore.
2 • The variable name can not start with a digit.
3 • Other character should be either letters, digits, underscores.
4 • There is no limit for the length of variables.
5 • Declared keywords cannot be used as a variable name.
6 • Upper and lower case letter are distinct.
The rules of naming variables :
11
Trang 7Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.3 Variables Variable Declaration:
Take note:
In Python, you don’t need to declare the name of variable
before it can be used
Python is not a “ Strongly-type ” language.
13
3.3 Variables
Declaration and Initialization of Variable
Python has no command for declaring a variable.
A variable is created the moment you first assign a
value to it.
Example:
13
Trang 8Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.3 Variables
Declaration and Initialization of Variable:
Initialization means assigning value to an already
declared variable.
Initialization and declaration can be done in single step
also.
15
3.4 Python Basic Data Types 15
Trang 9Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.4 Python Basic Data Types
17
3.5 Input - Output in Python
To input and output data in Python we use input() and
print() methods.
Examples:
print - object providing a connection to the monitor
input - object providing a connection to the keyboard
17
Trang 10Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
3.5 Input - Output in Python
Example:
19
Summary
1 • Structure of Python program
2 • Basic concept: Identity, keyword, comment…
3 • Declaring and using variable
4 • Basic datatype in Python
5 • Input/output in Python
19
Trang 11Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University
Quiz
21
Discussion 21
Trang 12Tran Ngoc Hiep, MSc Foundations of Engineering II- PVU
TALENTS FOR DEVELOPMENT - Petrovietnam University 23
23