SlideShare a Scribd company logo
1 of 12
Download to read offline
Bài 1
Basic Python programs, functions
http://pythonvietnam.info
2
Python!
• Được tạo ra vào năm1991 bởi Guido van Rossum.
 Tên ngôn ngữ được lấy cảm hứng từ Monty Python
• Sử dụng như một ngôn ngữ kịch bản:
 script: Một chương trình nhỏ sử dụng chạy 1 lần.
 Sử dụng cho các dự án vừa và nhỏ.
• Các nhà cung cấp sử dnjg:
 Google, Yahoo!, Youtube
 Many Linux distributions
 Games and apps (e.g. Eve Online)
3
Cài đặt Python
Windows:
• Tải python từ địa chỉ
http://www.python.org
• Cài đặt Python.
• Chạy Idle từ menu.
Mac OS X:
• Python đã được cài đặt.
• Bạn có thể chạy Python từ câu
lệnh hoặc từ menu.
Linux:
• Bạn có thể chạy tương tự như
Mac OSX.
• Nếu chưa có bạn có thể sử dụng
câu lệnh yum ( trên centos) hoặc
apt-get install ( trên debian).
Note: Bạn có thể xem các bài hướng
dẫn trên website.
4
Ngôn ngữ thông dịch
• interpreted
 Không biên dịch như Java
 Mã nguồn được viết và sau đó chạy bởi trình thông dịch
(interpreter)
 Bạn có thể gõ lệnh và thấy ngay kết quả.
Computer
Runtime
Environment
CompilerCodeJava:
ComputerInterpreterCodePython:
5
Trình thông dịch Python
• Cho phép bạn gõ câu lệnh và nhận được kết quả luôn rên
màn hình.
• Đây cũng là một cách để người dung quen với câu lệnh
Python.
 Để lặp lại câu lệnh trước các bạn dung tổ hợp phím: Alt+P
6
Chương trình Python
• Python không có hàm main như Java
 Mã nguồn của chương trình được lưu trực tiếp trong tập tin.
• Python không kết thúc bằng dấu chấm phẩy như các ngôn
ngữ khác.
hello.py
1 print("Hello, world!")
7
Hàm print
print("text")
print() (a blank line)
 Cách sử dung " cũng giống trong Java
 Chuỗi cũng có thể bắt đầu/ kết thúc với dấu '
swallows.py
1
2
3
4
print("Hello, Python Viet Nam!")
print()
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
8
Ghi chú
• Chú pháp:
# comment text (one line) dòng ghi chú
swallows2.py
1
2
3
4
5
6
# Khanh Nguyen. Email: khanhnn@pythonvietnam.info
# This program prints important messages.
print("Hello, world!")
print() # blank line
print("Suppose two swallows "carry" it together.")
print('African or "European" swallows?')
9
Hàm - Functions
• Function: tương đương với một method trong Java.
• Cú pháp:
def name():
statement
statement
...
statement
 Cần được khai báo tên hàm.
 Các phần của hàm phải được thụt đầu dòng
hello2.py
1
2
3
4
5
6
7
# Prints a helpful message.
def hello():
print("Hello, world!")
# main (calls hello twice)
hello()
hello()
10
Ý nghĩa của khoảng trắng
• Python sử dụng thụt đầu dòng để định nghĩa thay vì {}
 Điều này làm cho code dễ đọc và dễ nhìn.
 Trong Java, thụt đầu dòng là tùy chọchonjTrong Python, bạn
phải thực hiện.
hello3.py
1
2
3
4
5
6
7
8
# Prints a helpful message.
def hello():
print("Hello, world!")
print("How are you?")
# main (calls hello twice)
hello()
hello()
11
Bài tập
• Viết chương trình hiển thị các hình sau bằng Python:
______
/ 
/ 
 /
______/
 /
______/
+--------+
______
/ 
/ 
| STOP |
 /
______/
______
/ 
/ 
+--------+
12
Bài giải.
def egg():
top()
bottom()
print()
def cup():
bottom()
line()
print()
def stop():
top()
print("| STOP |")
bottom()
print()
def hat():
top()
line()
print()
def top():
print(" ______")
print(" / ")
print("/ ")
def bottom():
print(" /")
print(" ______/")
def line():
print("+--------+")
# main
egg()
cup()
stop()
hat()

More Related Content

What's hot

Báo cáo tuần 4
Báo cáo tuần 4Báo cáo tuần 4
Báo cáo tuần 4Daewoo Han
 
Bao cao tuan 3
Bao cao tuan 3Bao cao tuan 3
Bao cao tuan 3Loc Loc
 
introdution to Python
introdution to Pythonintrodution to Python
introdution to PythonPTIT HCM
 
Hacking de4dot for fun - Bài dịch
Hacking de4dot for fun - Bài dịchHacking de4dot for fun - Bài dịch
Hacking de4dot for fun - Bài dịchLevis Nickaster
 
Phân tích ConfuserEX - Invalid Metadata (bài dịch)
Phân tích ConfuserEX - Invalid Metadata (bài dịch)Phân tích ConfuserEX - Invalid Metadata (bài dịch)
Phân tích ConfuserEX - Invalid Metadata (bài dịch)Levis Nickaster
 
Decrypt các MSIL methods một cách thử công - Bài dịch
Decrypt các MSIL methods một cách thử công - Bài dịchDecrypt các MSIL methods một cách thử công - Bài dịch
Decrypt các MSIL methods một cách thử công - Bài dịchLevis Nickaster
 
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịch
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịchPhân tích Confuser 1.9.0.0 - Constant Protection - Bản dịch
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịchLevis Nickaster
 
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)Levis Nickaster
 
Introduction to python 20110917
Introduction to python   20110917Introduction to python   20110917
Introduction to python 20110917AiTi Education
 
Reverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILReverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILLevis Nickaster
 
Kết nối linux đến linux bằng ssh
Kết nối linux đến linux bằng sshKết nối linux đến linux bằng ssh
Kết nối linux đến linux bằng sshphanleson
 
Báo cáo thực tập tuần 3
Báo cáo thực tập tuần 3Báo cáo thực tập tuần 3
Báo cáo thực tập tuần 3Hai Te
 
Lớp 8: Bai thuc hanh 1
Lớp 8: Bai thuc hanh 1Lớp 8: Bai thuc hanh 1
Lớp 8: Bai thuc hanh 1Heo_Con049
 

What's hot (20)

Tùy biến Confuser
Tùy biến ConfuserTùy biến Confuser
Tùy biến Confuser
 
Báo cáo tuần 4
Báo cáo tuần 4Báo cáo tuần 4
Báo cáo tuần 4
 
Bao cao tuan 3
Bao cao tuan 3Bao cao tuan 3
Bao cao tuan 3
 
introdution to Python
introdution to Pythonintrodution to Python
introdution to Python
 
Hacking de4dot for fun - Bài dịch
Hacking de4dot for fun - Bài dịchHacking de4dot for fun - Bài dịch
Hacking de4dot for fun - Bài dịch
 
Phân tích ConfuserEX - Invalid Metadata (bài dịch)
Phân tích ConfuserEX - Invalid Metadata (bài dịch)Phân tích ConfuserEX - Invalid Metadata (bài dịch)
Phân tích ConfuserEX - Invalid Metadata (bài dịch)
 
Decrypt các MSIL methods một cách thử công - Bài dịch
Decrypt các MSIL methods một cách thử công - Bài dịchDecrypt các MSIL methods một cách thử công - Bài dịch
Decrypt các MSIL methods một cách thử công - Bài dịch
 
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịch
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịchPhân tích Confuser 1.9.0.0 - Constant Protection - Bản dịch
Phân tích Confuser 1.9.0.0 - Constant Protection - Bản dịch
 
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)
Các công cụ cần thiết cho quá trình Reverse Engineering .NET (bản đầy đủ)
 
Introduction to python 20110917
Introduction to python   20110917Introduction to python   20110917
Introduction to python 20110917
 
Reverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with ILReverse Engineering .NET - Advanced Patching, Playing with IL
Reverse Engineering .NET - Advanced Patching, Playing with IL
 
Kết nối linux đến linux bằng ssh
Kết nối linux đến linux bằng sshKết nối linux đến linux bằng ssh
Kết nối linux đến linux bằng ssh
 
Python
PythonPython
Python
 
Báo cáo thực tập tuần 3
Báo cáo thực tập tuần 3Báo cáo thực tập tuần 3
Báo cáo thực tập tuần 3
 
Virus toan tap
Virus toan tapVirus toan tap
Virus toan tap
 
WinEoP Framework
WinEoP FrameworkWinEoP Framework
WinEoP Framework
 
Lớp 8: Bai thuc hanh 1
Lớp 8: Bai thuc hanh 1Lớp 8: Bai thuc hanh 1
Lớp 8: Bai thuc hanh 1
 
Bai 8
Bai 8Bai 8
Bai 8
 
Baigiang01 mo dau
Baigiang01 mo dauBaigiang01 mo dau
Baigiang01 mo dau
 
Lap lich openerp
Lap lich openerpLap lich openerp
Lap lich openerp
 

Viewers also liked

Slide Python Bai 2 pythonvietnam.info
Slide Python Bai 2   pythonvietnam.infoSlide Python Bai 2   pythonvietnam.info
Slide Python Bai 2 pythonvietnam.infoKhá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-flowKhánh Nguyễn
 
Git Using - pythonvietnam.info
Git Using - pythonvietnam.infoGit Using - pythonvietnam.info
Git Using - pythonvietnam.infoKhá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-iterationsKhá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-classKhá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-fileioKhá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-moduleKhánh Nguyễn
 
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-databaseKhánh Nguyễn
 
Bottle web framwork for python
Bottle web framwork for pythonBottle web framwork for python
Bottle web framwork for pythonKhá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-threadKhá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 NamImr Hung
 
Software Development Process Seminar at HUI
Software Development Process Seminar at HUISoftware Development Process Seminar at HUI
Software Development Process Seminar at HUIKMS Technology
 
An Introduction of Apache Hadoop
An Introduction of Apache HadoopAn Introduction of Apache Hadoop
An Introduction of Apache HadoopKMS Technology
 
Am hoc kien truc
Am hoc kien trucAm hoc kien truc
Am hoc kien trucDang Lam
 

Viewers also liked (20)

Slide Python Bai 2 pythonvietnam.info
Slide Python Bai 2   pythonvietnam.infoSlide Python Bai 2   pythonvietnam.info
Slide Python Bai 2 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
 
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
 
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
 
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
 
Introduction to WEB HTML, CSS
Introduction to WEB HTML, CSSIntroduction to WEB HTML, CSS
Introduction to WEB HTML, CSS
 
Training android
Training androidTraining android
Training android
 
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
 
Training javascript 2012 hcmut
Training javascript 2012 hcmutTraining javascript 2012 hcmut
Training javascript 2012 hcmut
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Training python (new Updated)
Training python (new Updated)Training python (new Updated)
Training python (new Updated)
 
Am hoc kien truc
Am hoc kien trucAm hoc kien truc
Am hoc kien truc
 

Similar to Bai 1 pythonvietnam.info

Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng Yên
Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng YênGiáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng Yên
Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng YênTrần Thiên Đại
 
Ebook lập trình Java hướng đối tượng (Tiếng việt)
Ebook lập trình Java hướng đối tượng (Tiếng việt)Ebook lập trình Java hướng đối tượng (Tiếng việt)
Ebook lập trình Java hướng đối tượng (Tiếng việt)Duy Lê Văn
 
Bat dau voi Raspberry Pi (demo)
Bat dau voi Raspberry Pi (demo)Bat dau voi Raspberry Pi (demo)
Bat dau voi Raspberry Pi (demo)Thien Ta
 
bt-mon-lap-trinh-huong-doi-tuong.pdf
bt-mon-lap-trinh-huong-doi-tuong.pdfbt-mon-lap-trinh-huong-doi-tuong.pdf
bt-mon-lap-trinh-huong-doi-tuong.pdfHiNht6
 
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptxngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptxssuser49db3c1
 
Bai 1 may tinh va chuong trinh may tinh
Bai 1 may tinh va chuong trinh may tinhBai 1 may tinh va chuong trinh may tinh
Bai 1 may tinh va chuong trinh may tinhHoa Phượng
 
Hsbd taint
Hsbd taintHsbd taint
Hsbd taintvb2tin09
 
Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)laonap166
 
Slide py-sys-devops-final-v1
Slide py-sys-devops-final-v1Slide py-sys-devops-final-v1
Slide py-sys-devops-final-v1Công TÔ
 
tài liệu Mã nguồn mở Lap trình tren linux
tài liệu Mã nguồn mở  Lap trình tren linuxtài liệu Mã nguồn mở  Lap trình tren linux
tài liệu Mã nguồn mở Lap trình tren linuxThuyet Nguyen
 
Tl open office_impress
Tl open office_impressTl open office_impress
Tl open office_impressChi Lê Yến
 
Cach su dung Ubuntu
Cach su dung UbuntuCach su dung Ubuntu
Cach su dung UbuntuKien Ma
 
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEP
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEP
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPducminh28092018
 

Similar to Bai 1 pythonvietnam.info (20)

Tin 8
Tin 8Tin 8
Tin 8
 
Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng Yên
Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng YênGiáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng Yên
Giáo trình java Đại Học Sư Phạm Kỹ Thuật Hưng Yên
 
Ebook lập trình Java hướng đối tượng (Tiếng việt)
Ebook lập trình Java hướng đối tượng (Tiếng việt)Ebook lập trình Java hướng đối tượng (Tiếng việt)
Ebook lập trình Java hướng đối tượng (Tiếng việt)
 
Bat dau voi Raspberry Pi (demo)
Bat dau voi Raspberry Pi (demo)Bat dau voi Raspberry Pi (demo)
Bat dau voi Raspberry Pi (demo)
 
bt-mon-lap-trinh-huong-doi-tuong.pdf
bt-mon-lap-trinh-huong-doi-tuong.pdfbt-mon-lap-trinh-huong-doi-tuong.pdf
bt-mon-lap-trinh-huong-doi-tuong.pdf
 
Gt java080102005
Gt java080102005Gt java080102005
Gt java080102005
 
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptxngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptx
ngon-ngu-lap-trinh-python_C1_cac_khai_niem_(PI).pptx
 
Bai 1 may tinh va chuong trinh may tinh
Bai 1 may tinh va chuong trinh may tinhBai 1 may tinh va chuong trinh may tinh
Bai 1 may tinh va chuong trinh may tinh
 
Hsbd taint
Hsbd taintHsbd taint
Hsbd taint
 
Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)Linux Network Administration (LPI-1,LPI-2)
Linux Network Administration (LPI-1,LPI-2)
 
Slide py-sys-devops-final-v1
Slide py-sys-devops-final-v1Slide py-sys-devops-final-v1
Slide py-sys-devops-final-v1
 
tài liệu Mã nguồn mở Lap trình tren linux
tài liệu Mã nguồn mở  Lap trình tren linuxtài liệu Mã nguồn mở  Lap trình tren linux
tài liệu Mã nguồn mở Lap trình tren linux
 
Tl open office_impress
Tl open office_impressTl open office_impress
Tl open office_impress
 
Cach su dung Ubuntu
Cach su dung UbuntuCach su dung Ubuntu
Cach su dung Ubuntu
 
K37.103.529 tim hieu ve open office
K37.103.529 tim hieu ve open officeK37.103.529 tim hieu ve open office
K37.103.529 tim hieu ve open office
 
Embedded Linux
Embedded LinuxEmbedded Linux
Embedded Linux
 
Hsbd taint
Hsbd taintHsbd taint
Hsbd taint
 
Hsbd taint
Hsbd taintHsbd taint
Hsbd taint
 
Quan tri he dieu hanh linux
Quan tri he dieu hanh linuxQuan tri he dieu hanh linux
Quan tri he dieu hanh linux
 
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEP
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEP
DO AN TOT NGHIEPDO AN TOT NGHIEPDO AN TOT NGHIEP
 

Bai 1 pythonvietnam.info

  • 1. Bài 1 Basic Python programs, functions http://pythonvietnam.info
  • 2. 2 Python! • Được tạo ra vào năm1991 bởi Guido van Rossum.  Tên ngôn ngữ được lấy cảm hứng từ Monty Python • Sử dụng như một ngôn ngữ kịch bản:  script: Một chương trình nhỏ sử dụng chạy 1 lần.  Sử dụng cho các dự án vừa và nhỏ. • Các nhà cung cấp sử dnjg:  Google, Yahoo!, Youtube  Many Linux distributions  Games and apps (e.g. Eve Online)
  • 3. 3 Cài đặt Python Windows: • Tải python từ địa chỉ http://www.python.org • Cài đặt Python. • Chạy Idle từ menu. Mac OS X: • Python đã được cài đặt. • Bạn có thể chạy Python từ câu lệnh hoặc từ menu. Linux: • Bạn có thể chạy tương tự như Mac OSX. • Nếu chưa có bạn có thể sử dụng câu lệnh yum ( trên centos) hoặc apt-get install ( trên debian). Note: Bạn có thể xem các bài hướng dẫn trên website.
  • 4. 4 Ngôn ngữ thông dịch • interpreted  Không biên dịch như Java  Mã nguồn được viết và sau đó chạy bởi trình thông dịch (interpreter)  Bạn có thể gõ lệnh và thấy ngay kết quả. Computer Runtime Environment CompilerCodeJava: ComputerInterpreterCodePython:
  • 5. 5 Trình thông dịch Python • Cho phép bạn gõ câu lệnh và nhận được kết quả luôn rên màn hình. • Đây cũng là một cách để người dung quen với câu lệnh Python.  Để lặp lại câu lệnh trước các bạn dung tổ hợp phím: Alt+P
  • 6. 6 Chương trình Python • Python không có hàm main như Java  Mã nguồn của chương trình được lưu trực tiếp trong tập tin. • Python không kết thúc bằng dấu chấm phẩy như các ngôn ngữ khác. hello.py 1 print("Hello, world!")
  • 7. 7 Hàm print print("text") print() (a blank line)  Cách sử dung " cũng giống trong Java  Chuỗi cũng có thể bắt đầu/ kết thúc với dấu ' swallows.py 1 2 3 4 print("Hello, Python Viet Nam!") print() print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 8. 8 Ghi chú • Chú pháp: # comment text (one line) dòng ghi chú swallows2.py 1 2 3 4 5 6 # Khanh Nguyen. Email: khanhnn@pythonvietnam.info # This program prints important messages. print("Hello, world!") print() # blank line print("Suppose two swallows "carry" it together.") print('African or "European" swallows?')
  • 9. 9 Hàm - Functions • Function: tương đương với một method trong Java. • Cú pháp: def name(): statement statement ... statement  Cần được khai báo tên hàm.  Các phần của hàm phải được thụt đầu dòng hello2.py 1 2 3 4 5 6 7 # Prints a helpful message. def hello(): print("Hello, world!") # main (calls hello twice) hello() hello()
  • 10. 10 Ý nghĩa của khoảng trắng • Python sử dụng thụt đầu dòng để định nghĩa thay vì {}  Điều này làm cho code dễ đọc và dễ nhìn.  Trong Java, thụt đầu dòng là tùy chọchonjTrong Python, bạn phải thực hiện. hello3.py 1 2 3 4 5 6 7 8 # Prints a helpful message. def hello(): print("Hello, world!") print("How are you?") # main (calls hello twice) hello() hello()
  • 11. 11 Bài tập • Viết chương trình hiển thị các hình sau bằng Python: ______ / / / ______/ / ______/ +--------+ ______ / / | STOP | / ______/ ______ / / +--------+
  • 12. 12 Bài giải. def egg(): top() bottom() print() def cup(): bottom() line() print() def stop(): top() print("| STOP |") bottom() print() def hat(): top() line() print() def top(): print(" ______") print(" / ") print("/ ") def bottom(): print(" /") print(" ______/") def line(): print("+--------+") # main egg() cup() stop() hat()