Python Day 3: Lists & Branching

Python Day 3: Lists & Branching

IWKS 2300 Fall 2019 John K. Bennett

Lists Basics

A list is a built-in data structure for storing and accessing objects that belong in a specific sequence.

Two ways to create an empty list in python:

list_one = list()

list_two = []

Lists Basics (Continued ...)

A list can contain all sorts of objects, including: integers, strings, booleans, floats, and even other lists.

Python allows you to have multiple data types in the same list.

example = [112, "Apple", True, 1.75, [57, False]]

More on Lists

Add element to the list:

list_two.append(7)

List can be concatenated:

numbers = [1, 2, 3] letters = ['a', 'b', 'c'] print(numbers + letters)

[1, 2, 3, 'a', 'b', 'c']

4

More on Lists (Continued ...)

What gets printed when we change the order?

print(letters + numbers) ['a', 'b', 'c', 1, 2, 3]

Order is important!

What about now?

letters.append('d') print(letters + numbers)

['a', 'b', 'c', 'd', 1, 2, 3]

Console Output

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download