1. Trang chủ
  2. » Giáo án - Bài giảng

038 while loop tủ tài liệu training

2 23 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 2
Dung lượng 394,83 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

while loopWhile loops, like the For loops, are used for repeating specific block of codes.. The "while" loop is capable of anything that the "for" loop can do.. But unlike a for loop, th

Trang 1

while loop

While loops, like the For loops, are used for repeating specific block of codes The "while" loop is capable of anything that the "for" loop can do But unlike a for loop, the while loop will not run n times, but until a defined condition is no longer met

If the condition is initially false, the loop body will not be executed at all

The syntax of this loop is:

while (condition):

statement_1

statement_2

statement_n

Let's make our examples;

In [1]: count = 0

while (count < 10):

print ('The count is:',count) count += 1

print("It's done!")

In [2]: a = 1

while (a < 10):

print (a) a+= 2

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

The count is: 9

It's done!

1

3

5

7

9

The Complete Python 3 Programming Course: (Beginner to Advanced)

Trang 2

In [3]: n = 10

sum = 0

i = 1

while i <= n:

sum = sum + i

i = i+1 # update counter

# print the sum

print("The sum is", sum)

In [5]: i=0

text="Python is great!"

while i<len(text):

print(text[i],text) i+=1

Infinite Loop

Since we don't increase i variable, it will become an infinite loop Please do not run this code!

In [ ]: i=0

while (i<15):

print(i)

This is the end of this lesson

See you in our next lessons

In [ ]:

The sum is 55

P Python is great!

y Python is great!

t Python is great!

h Python is great!

o Python is great!

n Python is great!

Python is great!

i Python is great!

s Python is great!

Python is great!

g Python is great!

r Python is great!

e Python is great!

a Python is great!

t Python is great!

! Python is great!

The Complete Python 3 Programming Course: (Beginner to Advanced)

Ngày đăng: 17/11/2019, 07:33

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN