News Ticker

Menu

Loops and Lists





<Python>Bài 7: Loops and Lists trong Python



Tạo file: bai7.py

 the_count = [1, 2, 3, 4, 5]

 fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# cho biến number chạy trong list the_count
for number in the_count:
    print "This is count %d" % number

for fruit in fruits:
    print "A fruit of type: %s" % fruit


# Vì trong list change chứ các giá trị là chuỗi và số nên ta dùng %r
for i in change:
    print "I got %r" % i

# khởi tạo list
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append là 1 hàm chèn.
    elements.append(i)
    # chèn vào list
# in ra
for i in elements:
    print "Element was: %d" % i

Kết Quả:



Chúc các bạn thành công!

Share This:

Post Tags:

No Comment to " Loops and Lists "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM