Back to Browse

#9 Python Playground: List | Computer Programming & Coding for Kids & Beginners

6.2K views
Oct 27, 2020
3:42

Magic9 9-0 Python List Manipulation 9-1 Emma Hi, Liam! Liam Hi, Emma! Emma A variable can only contain one piece of data. How do I manipulate a lot of data? Liam In that case, you can use a list. Emma What is a list? Liam The list can contain and manipulate a lot of data. Each data is numbered so you can manipulate it by its number. Emma That's convenient. 9-2 Liam How to Write List in Python Liam When you're manipulating a lot of data, use lists, not variables. Emma What does a list look like? Liam A list is like a numbered shelf. You can manipulate a lot of data by specifying it by number. Each piece of data in the list is called an element and the number is called an index. Emma How do I number it? Liam Indexes are numbered from left to right, such as 0, 1, 2. Emma In Python, we count from zero to the left in order. Liam Exactly! Now, let's create a list of the lunch menu and display the third element. Emma Sounds interesting! Liam Open a new file in IDLE and write code like this. Save and run the program. Emma The third element, the hamburger, was displayed. Liam Using lists like this, we can manipulate a lot of data. Liam Fortune Cookie Program Liam Let's use the list to create a fortune cookie program. Emma Sounds like fun! Liam Here we use a random function to display the results randomly. Emma How do we use random functions? Liam We need to import the random module when using the random function. We can import the random module by writing import random on the first line. Emma So we can display the results randomly by using a random function. Liam Exactly. Now, let's create a fortune cookie program. Open a new file in IDLE and write code like this. Save and run the program. Emma Common Luck is now displayed. My fortune today seems normal. Liam Run this program again. Emma Now Best Luck was displayed. My fortune today seems to be the best. Liam Run this program again. Emma This time Bad Luck is displayed. I'm very disappointed. Liam The program randomly displays the elements in the list. Emma That was fun! Emma In this lesson, we learned how to use the list. Liam Well done!

Download

0 formats

No download links available.

#9 Python Playground: List | Computer Programming & Coding for Kids & Beginners | NatokHD