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

036 for loop tủ tài liệu training

5 32 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 5
Dung lượng 507,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

for Loopsfor loops is a type of loop that allows us walking on the lists, tuples, strings, and even dictionaries.. The syntax of this loop is: for item in objectit can be a list, a tupl

Trang 1

for Loops

for loops is a type of loop that allows us walking on the lists, tuples, strings, and even dictionaries The syntax of this loop is:

for item in object(it can be a list, a tuple, a dictionary etc.): statements

Here is a breakdown of the syntax:

for➜ the keyword that indicates that the for statement begins

item ➜ specifies the name of the variable that will hold a single element of a sequence

in ➜ indicates that the sequence comes next

object ➜ the sequence that will be stepped through

statements ➜ the statements that will be executed in each iteration

Walking on the lists:

In [1]: my_list = [1,2,3,4,5,6]

for element in my_list:

print("Item",element)

In [2]: # Adding elements using for loop

list1 = [1,2,3,4,5,6]

sum = 0

for item in list1:

sum = sum + item print("Sum =",sum)

Item 1

Item 2

Item 3

Item 4

Item 5

Item 6

Sum = 21

Trang 2

In [3]: # Printing even numbers

list2 = [1,2,3,4,5,6,7,8]

for item in list2:

if item % 2 == 0:

print(item)

In [4]: # Printing odd numbers

list2 = [1,2,3,4,5,6,7,8]

for item in list2:

if item % 2 == 1:

print(item)

Walking on the strings:

In [5]: s = "Hello world!"

for letter in s:

print(letter)

In [6]: # Replicate strings using loops

s = "Python"

for letter in s:

print(letter * 5)

2

4

6

8

1

3

5

7

H

e

l

l

o

w

o

r

l

d

!

PPPPP

yyyyy

ttttt

hhhhh

ooooo

nnnnn

Trang 3

In [7]: # Walking on the tuples

t = (1,2,3,4,5,6)

for item in t:

print(item)

We can walkthrough the multidimensional tuples like this:

In [8]: # Two-dimensional tuples

list3 = [(1,2),(3,4),(5,6),(7,8)]

for item in list3:

print(item)

We can see that each element is printed as tuple What if we want to reach that items?

In [9]: # Tuple unpacking

list3 = [(1,2),(3,4),(5,6),(7,8)]

for (i,j) in list3:

print("i:",i,"j:",j)

In [10]: #Unpacking three dimensional tuples

list4 = [(1,2,3),(4,5,6),(7,8,9),(10,11,12)]

for (i,j,k) in list4:

print(i * j * k)

Walk through the dictionaries:

We have learned 3 methods of the dictionaries in our dictionaries lesson

1) keys() method

2) values() method

3) items() method

1

2

3

4

5

6

(1, 2)

(3, 4)

(5, 6)

(7, 8)

i: 1 j: 2

i: 3 j: 4

i: 5 j: 6

i: 7 j: 8

6

120

504

1320

Walking on the tuples:

Trang 4

In [11]: my_dict = {"one":1,"two":2,"three":3,"four":4}

my_dict.keys()

In [12]: my_dict.values()

In [13]: my_dict.items()

We can walk through the dictionaries with using these methods

If we don't write any dict methods, we get keys of the dictionary as default

Let's see this:

In [14]: dict1 = {"one":1,"two":2,"three":3,"four":4}

for i in dict1:

print(i)

In [15]: #using keys method

dict1 = {"one":1,"two":2,"three":3,"four":4}

for i in dict1.keys():

print(i)

In [16]: #using values method

dict1 = {"one":1,"two":2,"three":3,"four":4}

for i in dict1.values():

print(i)

In [17]: #using items method

dict1 = {"one":1,"two":2,"three":3,"four":4}

for i in dict1.items():

print(i)

Out[11]: dict_keys(['one', 'two', 'three', 'four'])

Out[12]: dict_values([1, 2, 3, 4])

Out[13]: dict_items([('one', 1), ('two', 2), ('three', 3), ('four', 4)])

one

two

three

four

one

two

three

four

1

2

3

4

('one', 1)

('two', 2)

('three', 3)

('four', 4)

Trang 5

In [18]: #getting the keys and values with items()

dict1 = {"one":1,"two":2,"three":3,"four":4}

for k,v in dict1.items():

print("Key:",k,"Value:",v)

This is the end of this lesson

See you in our next lessons

In [ ]:

Key: one Value: 1

Key: two Value: 2

Key: three Value: 3

Key: four Value: 4

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