Resource - Python Cheat Sheets - Python Programming With Sequences of Data - Y9
Resource - Python Cheat Sheets - Python Programming With Sequences of Data - Y9
Python programming
Introduction
Python is a text-based programming language because it requires the user to write codes into the
computer.
● Lists
● List methods
● List functions
● List operators
● Strings
● String functions
● String operators
● Iterating over sequences
Lists
Lists are a type of data structure that involve
individual items organised in a sequence.
Create a list
Syntax [comma-separated list of items]
Examples summer = ["June", "July", "August"] Lists are usually assigned when they are
created (so they can be referred to and
modified later on).
data = [8, True, "Hello", 3.14] Lists can feature items of different data
types.
Syntax list[index]
The items in a list can be accessed through an index, i.e. their current
position in the list, with numbering starting at zero.
Examples month = summer[0] Retrieve the value of the first item (zero-
based index 0) in a list.
List slices
Syntax list[start index:end index:step]
A slice of a list is a new list that includes list items from a start index
up to (but not including) an end index. Specifying a step skips over
items.
head = data[:100] You can omit the start index (start from the
first item) and the end index (stop at the
last item).
Example numbers.append(42)
Syntax list.insert(index,item)
Insert an item at a given (zero-based) index.
Syntax list.pop(index)
Remove the item at the given (zero-based) index in the list, and return
it. If no index is specified, remove and return the last item in the list.
Examples tasks.pop()
Syntax list.remove(item)
Remove the first item from the list with a particular value. Raises a
ValueError if there is no such item.
Example countries.remove("Japan")
List methods
You can think of list methods as special functions
that are applied to lists. To call a list method, you
need to use dot notation (as shown in the examples
that follow).
Syntax list.count(item)
Return the number of times an item appears in the list.
Example values.reverse()
Syntax list.sort()
Sort the items in the list in ascending order.
Example len(planets)
Other functions
Syntax sum(list)
min(list)
max(list)
Return the sum of the list elements, the lowest and greatest values in
the list, respectively.
List operators
List operators allow you to form expressions that
involve lists and can be evaluated.
not "London" in destinations There are two ways to check if a list does
"London" not in destinations not contain a specific value.
Create a string
Syntax "character sequence"
Syntax string[index]
String slices
Syntax list[start index:end index:step]
prefix = word[:3] You can omit the start index (start from the
first character) and the end index (stop at
the last character).
Example len(password)
String operators
String operators allow you to form expressions that
involve strings and can be evaluated.
Syntax string.split(separator)
separator.join(list)
"".join(letters)
Iterating over sequences
The for-loop is a special type of control structure that
can be used to iterate over the elements of a
sequence.