SlideShare a Scribd company logo
Bài 2
Expressions and variables; for loops
http://pythonvietnam.info
2
Biểu thức
• Các phép tính cũng giống như Java.
 Toán tử: + - * / % (và ** cho phép lũy thừa)
 Độ ưu tiên : () sau đó đến ** tiếp theo * / % cuối cùng
+ -
 Số nguyên và số thực
>>> 1 + 1
2
>>> 1 + 3 * 4 - 2
11
>>> 7 / 2
3
>>> 7.0 / 2
3.5
>>> 10 ** 6
1000000
3
Biến
• Declaring
 no type is written; same syntax as assignment
• Operators
 no ++ or -- operators (must manually adjust by 1)
Java Python
int x = 2;
x++;
System.out.println(x);
x = x * 8;
System.out.println(x);
double d = 3.2;
d = d / 2;
System.out.println(d);
x = 2
x = x + 1
print(x)
x = x * 8
print(x)
d = 3.2
d = d / 2
print(d)
4
Kiểu dữ liệu
• Python khá linh hoạt hơn Java về kiểu dữ liệu.
 Kiểu của biến không cần khai báo.
 Biến có thể đổi các kiểu dữ liệu khi chương trình đang chạy.
Value Java type Python type
42 int int
3.14 double float
"ni!" String str
5
Nhân chuỗi
• Python có thể nhân lên với số lượng là số nguyên.
 Kết quả chuỗi sẽ được nhân lên nối tiếp nhau.
>>> “python" * 3
" pythonpythonpython"
>>> print(10 * “py")
py py py py py py py py py py
>>> print(2 * 3 * "4")
444444
6
Mối liên hệ chuỗi
• Integers and strings cannot be concatenated in Python.
 Cách giải quyết:
str(value) - chuyển đổi giá trị vào trong chuỗi.
print(expr, expr) - In ra hai giá trị theo như ý muốn
>>> x = 29
>>> print(“Khanh co so tuoi la: " + x + ".")
TypeError: cannot concatenate 'str' and 'int' objects
>>> print(" Khanh co so tuoi la: " + str(x) + ".")
Khanh co so tuoi la: 29.
>>> print(x + 1, “Se la so tuoi cua Khanh vao nam sau.")
30 la so tuoi Khanh vao nam sau.
7
Vòng lặp for
for name in range(max):
statements
 Sẽ lặp đi lặp lại từ giá trị đầu tiên đến giá trị cuối cùng
>>> for i in range(5):
... print(i)
0
1
2
3
4
8
Các dạng vòng lăp for
for name in range(min, max):
statements
for name in range(min, max, step):
statements
 Can specify a minimum other than 0, and a step other than 1
>>> for i in range(2, 6):
... print(i)
2
3
4
5
>>> for i in range(15, 0, -5):
... print(i)
15
10
5
9
Nested Loops
• Nested loops are often replaced by string * and +
....1
...2
..3
.4
5
Java
1
2
3
4
5
6
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= (5 - line); j++) {
System.out.print(".");
}
System.out.println(line);
}
Python
1
2
for line in range(1, 6):
print((5 - line) * "." + str(line))
10
Bài tập
• Hãy viết chương trình in ra màn hình chuỗi sau:
#================#
| <><> |
| <>....<> |
| <>........<> |
|<>............<>|
|<>............<>|
| <>........<> |
| <>....<> |
| <><> |
#================#
11
Exercise Solution
def bar():
print "#" + 16 * "=" + "#"
def top():
for line in range(1, 5):
# split a long line by ending it with 
print "|" + (-2 * line + 8) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 8) * " " + "|"
def bottom():
for line in range(4, 0, -1):
print "|" + (-2 * line + 8) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 8) * " " + "|"
# main
bar()
top()
bottom()
bar()
12
Kết nối các khoảng
• Các khoảng dữ liệu có thể kết nối bằng dấu +
 Có thể sử dụng vòng vặp trên một phạm vi để nối các dãy số
>>> range(1, 5) + range(10, 15)
[1, 2, 3, 4, 10, 11, 12, 13, 14]
>>> for i in range(4) + range(10, 7, -1):
... print(i)
0
1
2
3
10
9
8
13
Exercise Solution 2
def bar():
print "#" + 16 * "=" + "#"
def mirror():
for line in range(1, 5) + range(4, 0, -1):
print "|" + (-2 * line + 8) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 8) * " " + "|"
# main
bar()
mirror()
bar()
14
Hằng số
• Python không thực sự có hằng số.
 Thay vào đó bạn khai báo một biến toàn cục.
 Tất cả hàm sẽ sử dụng giá trị này.
constant.py
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
MAX_VALUE = 3
def printTop():
for i in range(MAX_VALUE):
for j in range(i):
print(j)
print()
def printBottom():
for i in range(MAX_VALUE, 0, -1):
for j in range(i, 0, -1):
print(MAX_VALUE)
print()
15
Exercise Solution 3
SIZE = 4
def bar():
print "#" + 4 * SIZE * "=" + "#"
def mirror():
for line in range(1, SIZE + 1) + range(SIZE, 0, -1):
print "|" + (-2 * line + 2 * SIZE) * " " + 
"<>" + (4 * line - 4) * "." + "<>" + 
(-2 * line + 2 * SIZE) * " " + "|"
# main
bar()
mirror()
bar()

More Related Content

What's hot

cours algorithme
cours algorithmecours algorithme
cours algorithme
NAWEL_DERBEL
 
15 Ghép nối 8255
15 Ghép nối 825515 Ghép nối 8255
15 Ghép nối 8255
Mr Giap
 
Một số hợp chất của Crom
Một số hợp chất của CromMột số hợp chất của Crom
Một số hợp chất của Crom
NganNguyen530
 
ALGORITHMIQUE fonction et procedure.pptx
ALGORITHMIQUE fonction et procedure.pptxALGORITHMIQUE fonction et procedure.pptx
ALGORITHMIQUE fonction et procedure.pptx
Hathat10
 
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
Vinh Phan
 
Thuật toán berlekamp và đa thức chia đường tròn modulo p
Thuật toán berlekamp và đa thức chia đường tròn modulo pThuật toán berlekamp và đa thức chia đường tròn modulo p
Thuật toán berlekamp và đa thức chia đường tròn modulo p
Bui Loi
 
Mã hóa đường cong Elliptic
Mã hóa đường cong EllipticMã hóa đường cong Elliptic
Mã hóa đường cong Elliptic
LE Ngoc Luyen
 
Cours pointeurs
Cours pointeursCours pointeurs
Cours pointeurs
Brahim BESSAA
 
Fortran cơ sở
Fortran cơ sởFortran cơ sở
Fortran cơ sởHajunior9x
 
Travaux dirigés 1: algorithme & structures de données
Travaux dirigés 1: algorithme & structures de donnéesTravaux dirigés 1: algorithme & structures de données
Travaux dirigés 1: algorithme & structures de données
Ines Ouaz
 
Fiche1 ex-sous-programme
Fiche1 ex-sous-programmeFiche1 ex-sous-programme
Fiche1 ex-sous-programme
Baghdadi Wajih
 
Giao an trinh_pascal_bai_tap_co_dap_an_huong_dan
Giao an trinh_pascal_bai_tap_co_dap_an_huong_danGiao an trinh_pascal_bai_tap_co_dap_an_huong_dan
Giao an trinh_pascal_bai_tap_co_dap_an_huong_dan
Võ Tâm Long
 
Hệ mật mã Elgamal
Hệ mật mã ElgamalHệ mật mã Elgamal
Hệ mật mã Elgamal
Thành phố Đà Lạt
 
Đề Kiếm tra đánh giá năng lực môn toán 8 phần ii
Đề Kiếm tra đánh giá năng lực môn toán 8 phần iiĐề Kiếm tra đánh giá năng lực môn toán 8 phần ii
Đề Kiếm tra đánh giá năng lực môn toán 8 phần ii
Vũ Lâm
 
Tuyển tập hình học giải tích trong mặt phẳng
Tuyển tập hình học giải tích trong mặt phẳngTuyển tập hình học giải tích trong mặt phẳng
Tuyển tập hình học giải tích trong mặt phẳng
https://www.facebook.com/garmentspace
 
MATMA - Chuong3 l tso
MATMA - Chuong3 l tsoMATMA - Chuong3 l tso
MATMA - Chuong3 l tsoSai Lemovom
 
Introduction à pl/sql
Introduction à pl/sqlIntroduction à pl/sql
Introduction à pl/sql
Abdelouahed Abdou
 
Cours complet Base de donne Bac
Cours complet Base de donne Bac Cours complet Base de donne Bac
Cours complet Base de donne Bac
Amri Ossama
 

What's hot (20)

cours algorithme
cours algorithmecours algorithme
cours algorithme
 
15 Ghép nối 8255
15 Ghép nối 825515 Ghép nối 8255
15 Ghép nối 8255
 
Một số hợp chất của Crom
Một số hợp chất của CromMột số hợp chất của Crom
Một số hợp chất của Crom
 
Phương trình hàm đa thức
Phương trình hàm đa thứcPhương trình hàm đa thức
Phương trình hàm đa thức
 
ALGORITHMIQUE fonction et procedure.pptx
ALGORITHMIQUE fonction et procedure.pptxALGORITHMIQUE fonction et procedure.pptx
ALGORITHMIQUE fonction et procedure.pptx
 
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
175 thuc-hanh-matlab-[dh-khoa-hoc-tu-nhien-hcm]
 
Thuật toán berlekamp và đa thức chia đường tròn modulo p
Thuật toán berlekamp và đa thức chia đường tròn modulo pThuật toán berlekamp và đa thức chia đường tròn modulo p
Thuật toán berlekamp và đa thức chia đường tròn modulo p
 
Mã hóa đường cong Elliptic
Mã hóa đường cong EllipticMã hóa đường cong Elliptic
Mã hóa đường cong Elliptic
 
Cours pointeurs
Cours pointeursCours pointeurs
Cours pointeurs
 
Đệ quy và quay lui
Đệ quy và quay luiĐệ quy và quay lui
Đệ quy và quay lui
 
Fortran cơ sở
Fortran cơ sởFortran cơ sở
Fortran cơ sở
 
Travaux dirigés 1: algorithme & structures de données
Travaux dirigés 1: algorithme & structures de donnéesTravaux dirigés 1: algorithme & structures de données
Travaux dirigés 1: algorithme & structures de données
 
Fiche1 ex-sous-programme
Fiche1 ex-sous-programmeFiche1 ex-sous-programme
Fiche1 ex-sous-programme
 
Giao an trinh_pascal_bai_tap_co_dap_an_huong_dan
Giao an trinh_pascal_bai_tap_co_dap_an_huong_danGiao an trinh_pascal_bai_tap_co_dap_an_huong_dan
Giao an trinh_pascal_bai_tap_co_dap_an_huong_dan
 
Hệ mật mã Elgamal
Hệ mật mã ElgamalHệ mật mã Elgamal
Hệ mật mã Elgamal
 
Đề Kiếm tra đánh giá năng lực môn toán 8 phần ii
Đề Kiếm tra đánh giá năng lực môn toán 8 phần iiĐề Kiếm tra đánh giá năng lực môn toán 8 phần ii
Đề Kiếm tra đánh giá năng lực môn toán 8 phần ii
 
Tuyển tập hình học giải tích trong mặt phẳng
Tuyển tập hình học giải tích trong mặt phẳngTuyển tập hình học giải tích trong mặt phẳng
Tuyển tập hình học giải tích trong mặt phẳng
 
MATMA - Chuong3 l tso
MATMA - Chuong3 l tsoMATMA - Chuong3 l tso
MATMA - Chuong3 l tso
 
Introduction à pl/sql
Introduction à pl/sqlIntroduction à pl/sql
Introduction à pl/sql
 
Cours complet Base de donne Bac
Cours complet Base de donne Bac Cours complet Base de donne Bac
Cours complet Base de donne Bac
 

Viewers also liked

Chuyen de flask -- pythonvietnam.info
Chuyen de flask  -- pythonvietnam.info Chuyen de flask  -- pythonvietnam.info
Chuyen de flask -- pythonvietnam.info
Khánh Nguyễn
 
Bai 1 pythonvietnam.info
Bai 1   pythonvietnam.infoBai 1   pythonvietnam.info
Bai 1 pythonvietnam.info
Khánh Nguyễn
 
Python Beginner Class day-03-flow
Python Beginner Class day-03-flowPython Beginner Class day-03-flow
Python Beginner Class day-03-flow
Khánh Nguyễn
 
Git Using - pythonvietnam.info
Git Using - pythonvietnam.infoGit Using - pythonvietnam.info
Git Using - pythonvietnam.info
Khánh Nguyễn
 
Python Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterationsPython Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterations
Khánh Nguyễn
 
Python Beginner Class day-10-class
Python Beginner Class day-10-classPython Beginner Class day-10-class
Python Beginner Class day-10-class
Khánh Nguyễn
 
Python Beginner Class day-09-fileio
Python Beginner Class day-09-fileioPython Beginner Class day-09-fileio
Python Beginner Class day-09-fileio
Khánh Nguyễn
 
Pbc day-01-introduction
Pbc day-01-introductionPbc day-01-introduction
Pbc day-01-introduction
Khánh Nguyễn
 
Python Beginner Class day-07-08-module
Python Beginner Class day-07-08-modulePython Beginner Class day-07-08-module
Python Beginner Class day-07-08-module
Khánh Nguyễn
 
Lập trình Python GUI vs PySide
Lập trình Python GUI vs PySideLập trình Python GUI vs PySide
Lập trình Python GUI vs PySide
Chien Dang
 
Python Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-databasePython Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-database
Khánh Nguyễn
 
Python Beginner Class day-15-networking
Python Beginner Class day-15-networkingPython Beginner Class day-15-networking
Python Beginner Class day-15-networking
Khánh Nguyễn
 
Bottle web framwork for python
Bottle web framwork for pythonBottle web framwork for python
Bottle web framwork for python
Khánh Nguyễn
 
Python Beginner Class day-14-thread
Python Beginner Class day-14-threadPython Beginner Class day-14-thread
Python Beginner Class day-14-thread
Khánh Nguyễn
 
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt NamMột góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
Imr Hung
 
Training android
Training androidTraining android
Training android
University of Technology
 
Introduction to WEB HTML, CSS
Introduction to WEB HTML, CSSIntroduction to WEB HTML, CSS
Introduction to WEB HTML, CSS
University of Technology
 
Software Development Process Seminar at HUI
Software Development Process Seminar at HUISoftware Development Process Seminar at HUI
Software Development Process Seminar at HUIKMS Technology
 
Training Google Drive and Hangouts.pptx
Training Google Drive and Hangouts.pptxTraining Google Drive and Hangouts.pptx
Training Google Drive and Hangouts.pptx
University of Technology
 
An Introduction of Apache Hadoop
An Introduction of Apache HadoopAn Introduction of Apache Hadoop
An Introduction of Apache Hadoop
KMS Technology
 

Viewers also liked (20)

Chuyen de flask -- pythonvietnam.info
Chuyen de flask  -- pythonvietnam.info Chuyen de flask  -- pythonvietnam.info
Chuyen de flask -- pythonvietnam.info
 
Bai 1 pythonvietnam.info
Bai 1   pythonvietnam.infoBai 1   pythonvietnam.info
Bai 1 pythonvietnam.info
 
Python Beginner Class day-03-flow
Python Beginner Class day-03-flowPython Beginner Class day-03-flow
Python Beginner Class day-03-flow
 
Git Using - pythonvietnam.info
Git Using - pythonvietnam.infoGit Using - pythonvietnam.info
Git Using - pythonvietnam.info
 
Python Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterationsPython Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterations
 
Python Beginner Class day-10-class
Python Beginner Class day-10-classPython Beginner Class day-10-class
Python Beginner Class day-10-class
 
Python Beginner Class day-09-fileio
Python Beginner Class day-09-fileioPython Beginner Class day-09-fileio
Python Beginner Class day-09-fileio
 
Pbc day-01-introduction
Pbc day-01-introductionPbc day-01-introduction
Pbc day-01-introduction
 
Python Beginner Class day-07-08-module
Python Beginner Class day-07-08-modulePython Beginner Class day-07-08-module
Python Beginner Class day-07-08-module
 
Lập trình Python GUI vs PySide
Lập trình Python GUI vs PySideLập trình Python GUI vs PySide
Lập trình Python GUI vs PySide
 
Python Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-databasePython Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-database
 
Python Beginner Class day-15-networking
Python Beginner Class day-15-networkingPython Beginner Class day-15-networking
Python Beginner Class day-15-networking
 
Bottle web framwork for python
Bottle web framwork for pythonBottle web framwork for python
Bottle web framwork for python
 
Python Beginner Class day-14-thread
Python Beginner Class day-14-threadPython Beginner Class day-14-thread
Python Beginner Class day-14-thread
 
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt NamMột góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
Một góc nhìn về chuyện khởi nghiệp của Sinh Viên Việt Nam
 
Training android
Training androidTraining android
Training android
 
Introduction to WEB HTML, CSS
Introduction to WEB HTML, CSSIntroduction to WEB HTML, CSS
Introduction to WEB HTML, CSS
 
Software Development Process Seminar at HUI
Software Development Process Seminar at HUISoftware Development Process Seminar at HUI
Software Development Process Seminar at HUI
 
Training Google Drive and Hangouts.pptx
Training Google Drive and Hangouts.pptxTraining Google Drive and Hangouts.pptx
Training Google Drive and Hangouts.pptx
 
An Introduction of Apache Hadoop
An Introduction of Apache HadoopAn Introduction of Apache Hadoop
An Introduction of Apache Hadoop
 

Similar to Slide Python Bai 2 pythonvietnam.info

Lap trinh matlab_co_ban_1731
Lap trinh matlab_co_ban_1731Lap trinh matlab_co_ban_1731
Lap trinh matlab_co_ban_1731Vu Tuan
 
matlab co ban
matlab co banmatlab co ban
matlab co ban
Diem Cong Hoang
 
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptxngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
ssuser49db3c1
 
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIITTu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
NIIT - ICT Hà Nội
 
Cac lenh trong matlab
Cac lenh trong matlabCac lenh trong matlab
Cac lenh trong matlab
AnhTuấn Nguyễn
 
Thuchanh Ktdk-matlab
Thuchanh Ktdk-matlabThuchanh Ktdk-matlab
Thuchanh Ktdk-matlab
mark
 
C đến C++ phần 1
C đến C++ phần 1C đến C++ phần 1
C đến C++ phần 1
TechMaster Vietnam
 
1. Python cơ bản.docx
1. Python cơ bản.docx1. Python cơ bản.docx
1. Python cơ bản.docx
SonViet3
 
Bai giangtrenlop
Bai giangtrenlopBai giangtrenlop
Bai giangtrenlopHồ Lợi
 
Học python
Học pythonHọc python
Học python
Tung Nguyen Xuan
 
Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06
Quach Long
 
Matlab intro
Matlab introMatlab intro
Matlab intro
hoang nguyen
 
Chuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdfChuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdf
HngTrn365275
 
Chuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdfChuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdf
nguyenkaka2
 
String c++
String c++String c++
String c++
ptquang160492
 

Similar to Slide Python Bai 2 pythonvietnam.info (20)

Lap trinh matlab_co_ban_1731
Lap trinh matlab_co_ban_1731Lap trinh matlab_co_ban_1731
Lap trinh matlab_co_ban_1731
 
matlab co ban
matlab co banmatlab co ban
matlab co ban
 
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptxngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PII).pptx
 
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIITTu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
Tu-Hoc-Python-Co-Ban-Trong-10-Phut-NIIT
 
Cac lenh trong matlab
Cac lenh trong matlabCac lenh trong matlab
Cac lenh trong matlab
 
Thuchanh Ktdk-matlab
Thuchanh Ktdk-matlabThuchanh Ktdk-matlab
Thuchanh Ktdk-matlab
 
C đến C++ phần 1
C đến C++ phần 1C đến C++ phần 1
C đến C++ phần 1
 
Chuong1 c
Chuong1 c Chuong1 c
Chuong1 c
 
1. Python cơ bản.docx
1. Python cơ bản.docx1. Python cơ bản.docx
1. Python cơ bản.docx
 
Bai giangtrenlop
Bai giangtrenlopBai giangtrenlop
Bai giangtrenlop
 
Học python
Học pythonHọc python
Học python
 
Multienumerate help
Multienumerate helpMultienumerate help
Multienumerate help
 
temp.pdf
temp.pdftemp.pdf
temp.pdf
 
Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
344444
344444344444
344444
 
Chuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdfChuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdf
 
Chuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdfChuong 1 Matlab co ban.pdf
Chuong 1 Matlab co ban.pdf
 
String c++
String c++String c++
String c++
 
Chuong1
Chuong1Chuong1
Chuong1
 

Slide Python Bai 2 pythonvietnam.info

  • 1. Bài 2 Expressions and variables; for loops http://pythonvietnam.info
  • 2. 2 Biểu thức • Các phép tính cũng giống như Java.  Toán tử: + - * / % (và ** cho phép lũy thừa)  Độ ưu tiên : () sau đó đến ** tiếp theo * / % cuối cùng + -  Số nguyên và số thực >>> 1 + 1 2 >>> 1 + 3 * 4 - 2 11 >>> 7 / 2 3 >>> 7.0 / 2 3.5 >>> 10 ** 6 1000000
  • 3. 3 Biến • Declaring  no type is written; same syntax as assignment • Operators  no ++ or -- operators (must manually adjust by 1) Java Python int x = 2; x++; System.out.println(x); x = x * 8; System.out.println(x); double d = 3.2; d = d / 2; System.out.println(d); x = 2 x = x + 1 print(x) x = x * 8 print(x) d = 3.2 d = d / 2 print(d)
  • 4. 4 Kiểu dữ liệu • Python khá linh hoạt hơn Java về kiểu dữ liệu.  Kiểu của biến không cần khai báo.  Biến có thể đổi các kiểu dữ liệu khi chương trình đang chạy. Value Java type Python type 42 int int 3.14 double float "ni!" String str
  • 5. 5 Nhân chuỗi • Python có thể nhân lên với số lượng là số nguyên.  Kết quả chuỗi sẽ được nhân lên nối tiếp nhau. >>> “python" * 3 " pythonpythonpython" >>> print(10 * “py") py py py py py py py py py py >>> print(2 * 3 * "4") 444444
  • 6. 6 Mối liên hệ chuỗi • Integers and strings cannot be concatenated in Python.  Cách giải quyết: str(value) - chuyển đổi giá trị vào trong chuỗi. print(expr, expr) - In ra hai giá trị theo như ý muốn >>> x = 29 >>> print(“Khanh co so tuoi la: " + x + ".") TypeError: cannot concatenate 'str' and 'int' objects >>> print(" Khanh co so tuoi la: " + str(x) + ".") Khanh co so tuoi la: 29. >>> print(x + 1, “Se la so tuoi cua Khanh vao nam sau.") 30 la so tuoi Khanh vao nam sau.
  • 7. 7 Vòng lặp for for name in range(max): statements  Sẽ lặp đi lặp lại từ giá trị đầu tiên đến giá trị cuối cùng >>> for i in range(5): ... print(i) 0 1 2 3 4
  • 8. 8 Các dạng vòng lăp for for name in range(min, max): statements for name in range(min, max, step): statements  Can specify a minimum other than 0, and a step other than 1 >>> for i in range(2, 6): ... print(i) 2 3 4 5 >>> for i in range(15, 0, -5): ... print(i) 15 10 5
  • 9. 9 Nested Loops • Nested loops are often replaced by string * and + ....1 ...2 ..3 .4 5 Java 1 2 3 4 5 6 for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (5 - line); j++) { System.out.print("."); } System.out.println(line); } Python 1 2 for line in range(1, 6): print((5 - line) * "." + str(line))
  • 10. 10 Bài tập • Hãy viết chương trình in ra màn hình chuỗi sau: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| |<>............<>| | <>........<> | | <>....<> | | <><> | #================#
  • 11. 11 Exercise Solution def bar(): print "#" + 16 * "=" + "#" def top(): for line in range(1, 5): # split a long line by ending it with print "|" + (-2 * line + 8) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 8) * " " + "|" def bottom(): for line in range(4, 0, -1): print "|" + (-2 * line + 8) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 8) * " " + "|" # main bar() top() bottom() bar()
  • 12. 12 Kết nối các khoảng • Các khoảng dữ liệu có thể kết nối bằng dấu +  Có thể sử dụng vòng vặp trên một phạm vi để nối các dãy số >>> range(1, 5) + range(10, 15) [1, 2, 3, 4, 10, 11, 12, 13, 14] >>> for i in range(4) + range(10, 7, -1): ... print(i) 0 1 2 3 10 9 8
  • 13. 13 Exercise Solution 2 def bar(): print "#" + 16 * "=" + "#" def mirror(): for line in range(1, 5) + range(4, 0, -1): print "|" + (-2 * line + 8) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 8) * " " + "|" # main bar() mirror() bar()
  • 14. 14 Hằng số • Python không thực sự có hằng số.  Thay vào đó bạn khai báo một biến toàn cục.  Tất cả hàm sẽ sử dụng giá trị này. constant.py 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 MAX_VALUE = 3 def printTop(): for i in range(MAX_VALUE): for j in range(i): print(j) print() def printBottom(): for i in range(MAX_VALUE, 0, -1): for j in range(i, 0, -1): print(MAX_VALUE) print()
  • 15. 15 Exercise Solution 3 SIZE = 4 def bar(): print "#" + 4 * SIZE * "=" + "#" def mirror(): for line in range(1, SIZE + 1) + range(SIZE, 0, -1): print "|" + (-2 * line + 2 * SIZE) * " " + "<>" + (4 * line - 4) * "." + "<>" + (-2 * line + 2 * SIZE) * " " + "|" # main bar() mirror() bar()