Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists in Python Creating lists.

Similar presentations


Presentation on theme: "Lists in Python Creating lists."— Presentation transcript:

1 Lists in Python Creating lists

2 Simple ways to create a list
Hard code the values mylist = [3, 9, ‘a’, 7.2] Using the replication operator mylist = [0] * 100 (gives a list with 100 zeros) Use the split method on a string Use the readlines method on a file Put [] around a variable if my_num is 5, then [ my_num] is the list [5]

3 Append vs. Concatenate Both can be used to add to an existing list but their syntax is not the same The concatenate operator + uses two lists and creates a bigger one You cannot say mylist = mylist + “joe”, it must be mylist = mylist + [“joe”] Append is a method which adds an element to the right end of a list – any type of data mylist.append(“3”) makes mylist one element longer

4 List accumulation Either concatenation or append can be used with lists as accumulators Initialize the list variable as an empty list [] inside your loop either concatenate or append the new item onto your list variable


Download ppt "Lists in Python Creating lists."

Similar presentations


Ads by Google