CS Functions Guide
CS Functions Guide
#Function body
Function names MUST begin with a letter, spaces and special characters are not allowed in function names.
A function without a value returned is a void or none function and must be invoked as a statement.
for var in sequence:
# Loop Body
For v in range(3,9,2) includes lower bound (3) but not upper bound (9) and moved in steps of 2.
while loop-continuation-condition:
# Loop body
Statement(s)
Do not use floating points for equality checkers in a loop. Be careful of = and =<
max() - returns largest value in list/string
min() - returns smallest value in list/string
len() - returns length of the list/string (number of objects)
Random.shuffle can be used to shuffle objects in a list
in and not in are boolean operators that test membership in a list/string
You cannot perform mathematical operations on any type of string. However, you can use the * operator to
concatenate the same string multiple times.
You can use index operators on strings, first character is 0 from left to right (lists also use this index), and first
character is -1 from right to left. e.g( s= “Luther college” s[5] = r = s[-9]) Strings are not mutable and no new value
can be assigned through index. However, lists are mutable and new value can be assigned by MyList[2] = __
[start : end] is a slicing operator that extracts substring from string, start character is included but end character is
not. Strings can be compared using comparison operators (==, !=, >, >=, <=) letters are compared by their
respective ASCII code
.isalnum() - returns true if characters are alphanumeric
.isalpha() - returns true if characters are alphabets
.isdigit() = returns true if string contains only numbers
.isidentifier() - returns true if string is a python identifier
.islower() - returns true if all characters are lower case
.isupper() - returns true if all characters are uppercase
.isspace() - returns true if all characters are only white space
captalise() - returns copy of string with only first letter capital
lower() - string with all lowercase
upper() - string with all uppercase
title() = returns string with first letter of each word capitalised
swapcase() - upper case to lowercase lower case to uppercase
replace() - returns old string replaced by new string
Shifting lists
Def shift(list):
Temp = lst[0]
For i in range (1, len(lst)):
Lst[i - 1] = lst[i]
lst[len(lst) - 1] = temp
Return lst
To copy a list, you can use list2 = [x for x in list1] or list2 = [] + list1
.append(x) - adds an element x
.insert(index, x) - inserts element x at given index
.pop (x)- removes the element x
.remove (x) - removes first occurrence of x
.sort() - sorts elements in ascending order
.split() - splits strings into list
.join() - converts list to string
Removing duplicates from list
Sequence = [blah, blah, blah]
Unique = []
For i in sequence:
if i not in unique:
unique.append(i)
Print (unique)