STEM STUDY

Nghiên cứu & phát triển (R&D) về các lĩnh vực IoT, Robotics, Trí tuệ nhân tạo (AI), 3D Printing, ứng dụng website và các ứng dụng di động. Website: http://www.itr.com.vn

2. September 2018 12:27
by ĐÀO TƯỜNG SƠ
1 Comments

Bài 05 - Vòng lặp trong Python

2. September 2018 12:27 by ĐÀO TƯỜNG SƠ | 1 Comments

1. Vòng lặp while

- Cú pháp:

while expression:
statement(s) 
 

- Ví dụ:

count = 0
while (count < 9):
    print ('The count is:', count)
    count = count + 1
print ("Good bye!")

* Kết quả:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

2. Vòng lặp for

Cú pháp:

for iterating_var in sequence:
    statements(s)

- Ví dụ 1:

>>> for i in range(5):
print(i)


0
1
2
3
4

- Ví dụ 2:

fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
     print ('Current fruit :', fruits[index])
     print ("Good bye!")

* Kết quả: 

Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

3. Vòng lặp chồng nhau

- Cú pháp:

for iterating_var in sequence:
     for iterating_var in sequence:
          statements(s)
     statements(s)

 

while expression:
     while expression:
          statement(s)
     statement(s)

- Ví dụ:

import sys
for i in range(1,11):
     for j in range(1,11):
          k=i*j
          print (k, end=' ')
     print()
* Kết quả:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
4. Kiểm soát vòng lặp
 

4.1 Mệnh đề break

- Cú pháp:

break

- Ví dụ:

for letter in 'Python':
     if letter == 'h':
          break
     print ('Current Letter :', letter)
 
var = 10
while var > 0:

     print ('Current variable value :', var)
     var = var -1
     if var == 5:
         break
print ("Good bye!")
 
* Kết quả:
Current Letter : P
Current Letter : y
Current Letter : t
 
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
4.2 Mệnh đề continue
- Cú pháp:
continue
- Ví dụ:
for letter in 'Python':
     if letter == 'h':
          continue
     print ('Current Letter :', letter)
 
var = 10
while var > 0:

     print ('Current variable value :', var)
     var = var -1
     if var == 5:
         continue
     print ('Current variable value :', var)
print ("Good bye!")
* Kết quả:
 
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!

4.3 Mệnh đề pass
- Cú pháp:
pass
- Ví dụ:
for letter in 'Python':
     if letter == 'h':
          pass
          print ('This is pass block')
     print ('Current Letter :', letter)
print ("Good bye!")
- Kết quả:
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!

Comments (1) -

Hey there I am so happy I found your weblog, I really found you by mistake, while I was searching on Digg for something else, Anyhow I am here now and would just like to say many thanks for a marvelous post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the awesome jo.

Awesome blog! Do you have any tips and hints for aspiring writers? I'm hoping to start my own site soon but I'm a little lost on everything. Would you suggest starting with a free platform like Wordpress or go for a paid option? There are so many choices out there that I'm completely overwhelmed .. Any suggestions? Thanks!

Add comment