2020/python (codecademy)

Creating and Modifying a list in Python.

bNayC 2020. 4. 15. 02:27

What is a list?

A list is an ordered set of objects in Python.

Suppose we want to make a list of the heights of students in a class:

  • Jenny is 61 inches tall
  • Alexus is 70 inches tall
  • Sam is 67 inches tall
  • Grace is 64 inches tall

In Python, we can create a variable called heights to store these numbers:

 

heights = [61, 70, 67, 64]

 

답은 

Lists II

Lists can contain more than just numbers.

We can also combine multiple data types in one list. 

 

List of Lists

답. 그냥 ages는 이름뿐이었구낭

 

Zip

zip takes two (or more) lists as inputs and returns an object that contains a list of pairs. Each pair contains one element from each of the inputs. You won’t be able to see much about this object from just printing it:

 

print(list(names_and_heights))

 

 

답.... 흠

 

Empty Lists

A list doesn’t have to contain anything! You can create an empty list like this:

empty_list = []

 

Why would we create an empty list?

Usually, it’s because we’re planning on filling it later based on some other input. 

 

Growing a List: Append

We can add a single element to a list using .append()

이해 노우 ㅠ

It’s important to remember that .append() comes after the list.

This is different from functions like print, which come before.

 

 

What does it mean? 

 

Growing a List: Plus (+)

When we want to add multiple items to a list, we can use + to combine two lists.

 

If we want to add a single element using +, we have to put it into a list with brackets ([]):

 

my_list + [4]

 

 

와잇...

Range I

Typing out all of those numbers takes time and the more numbers we type, the more likely it is that we have a typo.

 

Python gives us an easy way of creating these lists using a function called range. The function range takes a single input, and generates numbers starting at 0 and ending at the number before the input. So, if we want the numbers from 0 through 9, we use range(10) because 10 is 1 greater than 9:

 

Range II

By default, range creates a list starting at 0.

However, if we call range with two arguments, we can create a list that starts at a different number.

For example, range(2, 9) would generate numbers starting at 2 and ending at 8 (just before 9):

 

아래 예시까지 보니까 이해 done
규칙은 이해갔지만, 실제 '서비스'에서는 이 기능이 왜 필요할까? 

 

questions