0% found this document useful (0 votes)
5 views

Lecture 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CITS2401 LECTURE 2

What is a List? Lists are good for keeping track of a sequence of items. Lists in Python are
created by placing the sequence inside square brackets [ ] separated by commas. Lists can
be assigned to a single variable. List items are indexed and you can access them by referring
to the index number. The first item has index 0. Negative indexing means start from the
end. List items can be any data type, contain different data types and can have items with
the same value.

len() function shows amount of variables in list.

Range of indexes (slicing): You can specify a range of indexes by specifying where to start
and where to end the range. When specifying a range, the return value will be a new list
with the specified items. list_name[start_index(included): end_index(NOT included)]
Leaving out first value of index will automatically start from first item and leaving out last
item will mean it finishes at the last index.

append() function: To add an item to the end of the list, use the append() method.
list_name.append(‘item_name’)

insert() function: To insert a list item at a specified index, use the insert() method.
list_name.insert(specific_index,‘item_name’)

remove() function: The remove() method removes the specified item.


list_name.remove(‘item_name’)

pop() function: The pop() method removes the specified item with index.
list_name.pop(specific_index) *NOTE: If you do not specify the index, the pop() method
removes the last item. Can’t use for tuples

To change value of specific item: list_name[specific_index]=‘value_to_update’


To change range of item values:
list_name[start(included):end(NOT_included)]=[‘value_to_update’,…]

Tuples: A tuple is like a list, but it is unchangeable (called immutable in Python). Tuples are
written with round brackets(). Since tuples are also indexed, tuples can have items with the
same value. (Allow Duplicate).
We can do the similar thing for accessing tuples like what we did with lists:
- Length using len().
- Access a tuple item using tuple_name[specific_index]
- Slicing using a range of indexes tuple_name[start_index(included):end_index(NOT
included)]

Technically you cannot update it, but there are some tricks! 1) Convert the tuple to a list
using list() 2) Do append() or remove() 3) Convert it back into a tuple using tuple()

Set: A Set is a collection which is unordered, unindexed, mutable, and does NOT allow
duplicate values. Sets are written with curly brackets. {}. No index at all. Note: Sets are
unordered, so you CANNOT be sure in which order the items will appear. However, we can
check whether the item is in a set using in ‘item_name’ in set_name. Will give true or false.
Once a set is created, you cannot change its items, but you can add new items:
To add one item to a set use the add()method: set_name.add(‘item_value’).
To remove an item in a set, use the remove() method. set_name.remove(‘item_value’). A
set with nothing inside is classified as dictionary.

Dictionary: A dictionary is like a list but is not indexed by positions, but by keys (which are
often strings). Dictionaries are written with curly brackets {} Dictionaries are used to store
data values in key:value pairs. It can be referred to by using the key name, and it CANNOT
have two items with the same key. Each line separated by comma. Eg:
my_comp = {
‘brand’: ‘Apple’,
‘model’: ‘iMac’,
‘year’: 2007
}
print(my_comp)

- You can access the items of a dictionary by referring to its key name, inside square
brackets. Print(dictionary_name[‘key_name’]) SQUARE BRACKETS
- You can change the value of a specific item by referring to its key name.
dictionary_name[‘key_name’]=new_value
- Adding an item to the dictionary is done by using a new index key and assigning a
value to it. dictionary_name[‘new_key_name’]=new_value
- The pop() method removes the item with the specified key name:
dictionary_name.pop(‘key_name’)

Control flow:
Selection statements: : if, elif, else statements.

An if statement is used to do some action(s) if the condition is true. IF statement starts with
keyword if. Colon symbol (:) denotes start of if block. Body of the if statement is indented
with 4 spaces or a tab

else statement to do something else if the condition is false No condition after the else
statement.
Test multiple conditions using elif(‘else if’) statement:

You might also like