1/45
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
what is a string
a sequence of characters
some characters = special such as /n = new line
what are characters
upper and lowercase letters
numbers
symbols
spaces
what matters in strings…
order!
escaping characters
either use ““ and inside use ‘
OR
before apostrophe use \
what does \t do
tabs = useful for formatting
what to use for long strings
can split using \
“I’m sorry to have kept you, “\
“but I have been rather busy”
OR use triple quotations “““
len()
string length
counts all characters, including spaces, new lines and tabs to get length
EG
ni = “Ni!”
print(“Length of string is: “, len(ni))
Length of string is: 3
Style
PEP-8
readability counts
print(witches[9]) = d
for assigned string “witches", the character for the index of 9 (technically the 10th character because we start at 0) is d
for index in range(12, len(witches)):
indent print(witches[index])
vertically down it will print the string of witches from the character of index 12 to the end of the string/string length
for index in range(12, len(witches)-1, -1, -1):
indent print(witches[index])
flips the way that the code is written so that it is readable bottom to top
for index in range(0, len(witches), 2):
indent print(witches[index])
prints every second character (1, 3, 5…)
for index in range(len(witches)-1, -1, -3):
indent print(witches[index])
prints every third character in reverse order from the end to the beginning.
negative indexing
every string also has corresponding negative numbers, the last character of the string would be referred to as -1
what would the output of ‘John’ + ‘Cleese’ be?
JohnCleese
print(John, Terry, Graham, sep=’*’)
John Paul*Terry Cruise*Graham Maine
print(John, Terry, Graham, sep=’,’)
John Paul,Terry Cruise,Graham Maine
ends
if we don’t want a character printed at the end of each line we use…
end=
example of end=
pa = “pineapples are great”
for index in range(len(pa))
indent print(pa[index], end=’ ‘)
OUTPUT: p i n e a p p l e s a r e g r e a t
what does concatenation refer to
using + to combine strings
min(s)
max(s)
s.count(x)
smallest item of s
biggest item of s
total number of occurrences of x in s
ASKII table gives….
numbered order of all characters
print(menuitem2.index(‘spam))
returns the index of the first occurrence of 'spam' in the list 'menuitem2'.
s.upper()
s.lower()
s.strip()
s.replace(‘_’, ‘_’)
converts characters to uppercase
converts characters to lowercase
returns a copy of the string with leading and trailing whitespace removed such as tabs, spaces, etc
replaces instance of first thing with second thing
when to use lists
when you need to keep a collection of data
what can lists contain
numbers, strings, other lists, combinations
difference between lists and strings
lists can hold combinations of data, string cannot
benefits of lists
flexible
dynamic
can change and delete anytime
can access using an index
python[2] = ‘__’
updating index 2 in list called python to contain __
del python[5]
deletes index of 5 in list python
x = [1, 2, 3]
y = [4, 5, 6]
z = x + y
[1, 2, 3, 4, 5, 6]
if you have a series of lists you can…
print(menu[4])
print(menu[4][2])
prints list 4
prints element 2 of list 4
ingredients = menuitem2.split(',’)
print(ingredients)
splits all of menuitem2 with commas into a list of ingredients.
slicing
you can slice strings and lists to access their parts
name = “John Cleese”
name[5:10]
returns 'Clee', a substring from the original string. REMEMBER the 10th index is not counted it is UP TO BUT NOT INCLUDING 10
name = “John Cleese”
name[:4]
returns 'John', the substring from the beginning of the string up to but not including index 4.
name = “John Cleese”
name[4:]
returns ' Cleese', the substring from index 4 to the end of the string.
name = “John Cleese”
name[:-2]
returns 'John Clee' = doesn’t include -2 which would be s
import random
for i in range(5):
print(random.random(), end=” “)
print()
prints 5 random numbers which are separated by spaces which are between 0 and 1
print(random.randrange(0,101,5)
returns values between 0 and 100 which are divisible by 5
WHAT IS HAPPENING?:
sides = [“heads”, “tails”]
outcomes = [0, 0]
for i in range (10000):
toss = random.choice(sides)
if toss == “heads”:
outcomes[0] += 1
else:
outcomes[1] += 1
list called sides contains a string called heads and a string called tails
the outcomes are defined as both being 0
for 10 000 tries
make a random choice of sides, in which there are 2 elements, 0 which is heads and 1 which is tails
if the choice is “heads”
the element 0, heads, has its total increase by 1
if this is not the case
element 1, tails, has its total increase by 1
NOTE: this continues to run for 10 000 tries
Pseudo random number generator (PRNG)
long peruod
uniformly distributed
reproducible
quick and easy to compute
Linear Congruential Generator (LCG)
short period
not uniformly distributed
reproducible
poor quality
Monte Carlo techniques
model systems by simulating via random sampling
powerful, flexible, direct
sometimes the only feasible way to solve something
how to add an element into a list
list.append()
how would you make a string uppercase and then duplicate it?
mystring = “abcd”
mystring.upper()*2
OUTPUT = ABCDABCD