COSC 1351
Introduction to
Computer Programming
Dr. Wail Mardini
PhD Computer Science
Course Agenda
What have we covered so far?
• Conditional Statements (if – else)
• Loops (for loops, while loops)
• Function Calls
• Mathematical and Relational Operations
• Creating Programmer-defined Functions
• Recursion
What’s next?
• Introduction to Data Structures (today .. )
• Searching and Sorting Algorithms (upcoming weeks)
2
Data Structures - Definitions
• Data – pieces of information that can be represented, stored,
or manipulated using a computer.
• Inputs being processed
• Outputs generated
3
Data Structures - Definitions
Data structure – a way of logically organizing data in a computer so that it
can be used efficiently.
What examples can you think of?
4
Data Structures = Organization
5
Arrays
Array (a.k.a list) – collection of similar pieces of data
stored in contiguous memory locations (next to one
another).
6
Arrays
item1 you are
located at index 0
Index =
4
7
Arrays/Lists
A value refers to a piece of data stored in an array, and its index is the
position in the array where that value is stored.
This means the index represents where an element is, whereas the value
represents what the element is.
Index = position
Value where stored
8
Pythonic way of defining
Lists/Arrays
# This assigns an empty list to the variable named my_list
my_list = []
# Defines lists with items
#list holding different types
my_list = [10, 100.0, 22, ‘Hi’]
print(my_list)
#list of strings
new_list = [‘Yes’, ‘No’]
#list of integers
another_list = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
9
Generating lists using range
Try this:
my_list = [range(0, 10)]
print(my_list)
Try this:
my_list = [*range(0, 10)]
print(my_list)
10
Pythonic way of defining
Lists/Arrays
# Declare a list of a certain size and initialize it to a certain value
my_list = [0] * 10
print(my_list)
# Access any item in the list by the index number
my_list[0] = 100
print(my_list[0])
my_list[1] = 200
print(my_list[1])
11
Pythonic way of defining
Lists/Arrays
# To declare your list variable and fill it up all at one (i.e. in a single line of code)
my_list = [99,44,55,66,77]
# To display the elements in your list
print (my_list)
# To print a single value from your list you need to know the index
print (my_list[3])
12
Index Error !!!
An IndexError exception will be raised if you use an invalid
index with a list. For example, look at the following code:
# This code will cause an IndexError exception.
my_list = [10, 20, 30, 40]
index = 0
while index < 5:
print(my_list[index])
index += 1
Pythonic way of defining
Lists/Arrays
my_list = [99,44,55,66,77]
# Displaying range of values from the list
print(my_list[2:5]) This is called List slicing
# accesses the last element in the list
print (my_list[-1])
# accesses the next to last element in the list
print (my_list[-2])
# access the third element from the beginning of the list
print (my_list[2])
14
Pythonic way of defining
Lists/Arrays
my_list = [99,44,55,66,77]
#Slicing with Step Values
Try this: print(my_list[0:5:2]) [start:end:step]
What about this? print(my_list[::2])
And this? print(my_list[2::2])
15
Concatenating Lists
• Concatenate: join two things together
• The + operator can be used to concatenate two lists
Try this: list1 = [10, 20, 30]
list2 = [50, 80]
list3 = list1 + list2
print(list3)
Try concatenating lists holding different data types
16
More fun things to do with Lists
17
For Loops and Lists are best
Friends!
How can a for loop be used to print out a list of strings?
# First we create our list
mylist = [“Joe”, “Sue”, “Mary”, “Felix”]
# for loop to print current items in a list
for index in range(len(names)):
print(names[index]
18
For Loops and Lists are best
Friends!
How can a for each loop be used to print out a list of strings?
# First we create our list
mylist = [“Joe”, “Sue”, “Mary”, “Felix”]
# for loop to print current items in a list
for name in mylist:
print (“Current name in the list is” + name)
# You can check if a value exists in a list using an if condition
if “Joe” in mylist:
print (name + “is enrolled in my list”)
19
in operator
#Create a list of product numbers
prod_nums = ['V475', 'F987', 'Q143', 'R688’]
#Get a product number to search for
search = input('Enter a product number: ‘)
#Determine whether the product number is in the list.
if search in prod_nums:
print(f'{search} was found in the list.’)
else:
print(f'{search} was not found in the list.')
What do you need to change in the code if you were to use the “not in” operator?
20
Useful functions
•del statement: removes an element from a specific index in a list
• General format: del list[i]
•sum function: returns the sum of the values in a numeric sequence
• Example: total = sum(my_list)
•min and max functions: built-in functions that returns the item
that has the lowest or highest value in a sequence
• The sequence is passed as an argument
21
Randomly Selecting List Elements
Try this:
import random
names = ['Jenny', 'Kelly', 'Chloe', 'Aubrey’]
winner = random.choice(names)
print(winner)
Try this:
import random
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
selected = random.choices(numbers, k = 3)
print(selected)
22
Exercise 1
• Write a program that continues to take student names as input from the
user and adds the names to a list until the user is done.
• Here is a sample output:
Enter a name: Kathryn [Enter]
Do you want to add another name?
y = yes, anything else = no: y [Enter]
Enter a name: Chris [Enter]
Do you want to add another name?
y = yes, anything else = no: n [Enter]
Here is the list of names you entered:
Kathryn
Chris
23
Exercise 2
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements
that are common between the lists (without duplicates). Make sure
your program works on two lists of different sizes.
Extras:
Randomly generate two lists to test this
24