1 of 12

Binary Tree

Digital Literacy and Intro to Programming

2 of 12

Week 5

Logic, Lists, Functions, etc. (Python)

3 of 12

Using Lists in Python

Lists are one of Python’s built-in data types which can store multiple items

  • A list can be created through the use of square brackets, [ ].

  • You can create a list using the following syntax (output shown on the right):

4 of 12

Using Lists in Python

List items are ordered, mutable, and allow duplicate values. Also, lists are indexed, so the first item has index 0, the second item has index 1, and in general, the nth item has index n - 1.

  • Ordered: Items have a defined order, which generally does not change.
  • Mutable: We can change, add, and remove items in a list after it is created.
  • Allow Duplicates: Lists are indexed, so they can have items with the same value.

5 of 12

Using Lists in Python

Note: Since a list of n items has a maximum index of n - 1, we cannot access a list item at an index position greater than or equal to n. If we do so, we get a “list index out of range” error. It’s important to make sure you know how many elements are in your list. That way, you reduce the likelihood of buggy code such as in line 12.

6 of 12

Using Lists in Python

Another operation that you can do with list is printing multiple values of the list.

We start with some index to start at and an end index. Like before we can start by typing out a print statement like

Keep in mind that we start with 0. So typing 1:3 would make us start with our second term and we will end before the 3rd term.

7 of 12

Using Lists in Python

  • You can find the length of a list using the len() function:

  • From the perspective of Python, all lists have their own data type:

  • Finally, you can construct a list using the list() constructor:

8 of 12

Using Functions In Python

  • In Python, functions are blocks of code that can be reused and “called” at any time.
  • Functions are meant to carry out a specific task, and they are initialized using the “def” keyword, a name, parentheses, additional parameters, and a colon.
  • An example function is shown in the code snippet below:

  • This is the output of the program:

9 of 12

Using Parameters with Functions

  • Parameters are essentially like placeholders in a function.
  • They allow you to send information (which can be in different data types) into the function to make it work with different inputs
  • When a function is called, the “argument” for input is given inside the parentheses.
  • An example is shown to the right:

  • This is the output of the program:

10 of 12

Nested Functions

  • Similar to what you can do with for loops and if - else statements, functions have the ability to be nested as well. Here is an example of a nested function:

  • This is the output of the program:

11 of 12

Mini Activity

12 of 12

Using the following link, attempt to create a Caesar Cipher encoder and decoder: https://www.programiz.com/python-programming/online-compiler/

(Read more about Caesar Ciphers at this link): https://www.geeksforgeeks.org/caesar-cipher-in-cryptography/)

Try to use a few of the topics we have covered today, such as functions and lists.��

Mini Activity Directions: