Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

Λίστες


Οι λίστες είναι πολύ παρόμοιες με πίνακες. Μπορούν να περιέχουν οποιοδήποτε τύπο μεταβλητής και μπορούν να περιέχουν όσες μεταβλητές θέλετε. Οι λίστες μπορούν επίσης να διατρέχονται με πολύ απλό τρόπο. Εδώ είναι ένα παράδειγμα για το πώς να δημιουργήσετε μια λίστα.

mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3

# prints out 1,2,3
for x in mylist:
    print(x)

Η πρόσβαση σε έναν δείκτη που δεν υπάρχει δημιουργεί μια εξαίρεση (ένα σφάλμα).

mylist = [1,2,3]
print(mylist[10])

Άσκηση

Σε αυτήν την άσκηση, θα χρειαστεί να προσθέσετε αριθμούς και συμβολοσειρές στις σωστές λίστες χρησιμοποιώντας τη μέθοδο "append" της λίστας. Πρέπει να προσθέσετε τους αριθμούς 1, 2 και 3 στη λίστα "numbers", και τις λέξεις 'hello' και 'world' στη μεταβλητή strings.

Θα πρέπει επίσης να συμπληρώσετε τη μεταβλητή second_name με το δεύτερο όνομα στη λίστα names, χρησιμοποιώντας τον τελεστή αγκύλης []. Σημειώστε ότι ο δείκτης ξεκινά από το μηδέν, οπότε αν θέλετε να έχετε πρόσβαση στο δεύτερο στοιχείο της λίστας, ο δείκτης του θα είναι 1.

numbers = [] strings = [] names = ["John", "Eric", "Jessica"] # write your code here second_name = None # this code should write out the filled arrays and the second name in the names list (Eric). print(numbers) print(strings) print("The second name on the names list is %s" % second_name) numbers = [] strings = [] names = ["John", "Eric", "Jessica"] # write your code here numbers.append(1) numbers.append(2) numbers.append(3) strings.append("hello") strings.append("world") second_name = names[1] # this code should write out the filled arrays and the second name in the names list (Eric). print(numbers) print(strings) print("The second name on the names list is %s" % second_name) test_output_contains("[1,2,3]", no_output_msg= "Make sure that you have printed the `numbers` list.") test_output_contains("['hello', 'world']", no_output_msg= "Make sure that you have printed the `strings` list.") test_output_contains("The second name on the names list is Eric", no_output_msg= "Did you fill in the variable `second_name` with the second name in the names list?") success_msg("Great Job!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy