Strings and Lists

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/45

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

46 Terms

1
New cards

what is a string

a sequence of characters

  • some characters = special such as /n = new line

2
New cards

what are characters

  • upper and lowercase letters

  • numbers

  • symbols

  • spaces

3
New cards

what matters in strings…

order!

4
New cards

escaping characters

either use ““ and inside use ‘

OR

before apostrophe use \

5
New cards

what does \t do

tabs = useful for formatting

6
New cards

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 “““

7
New cards

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

8
New cards

Style

PEP-8

readability counts

9
New cards

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

10
New cards

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

11
New cards

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

12
New cards

for index in range(0, len(witches), 2):

indent print(witches[index])

prints every second character (1, 3, 5…)

13
New cards

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.

14
New cards

negative indexing

every string also has corresponding negative numbers, the last character of the string would be referred to as -1

15
New cards

what would the output of ‘John’ + ‘Cleese’ be?

JohnCleese

16
New cards

print(John, Terry, Graham, sep=’*’)

John Paul*Terry Cruise*Graham Maine

17
New cards

print(John, Terry, Graham, sep=’,’)

John Paul,Terry Cruise,Graham Maine

18
New cards

ends

if we don’t want a character printed at the end of each line we use…

end=

19
New cards

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

20
New cards

what does concatenation refer to

using + to combine strings

21
New cards

min(s)

max(s)

s.count(x)

smallest item of s

biggest item of s

total number of occurrences of x in s

22
New cards

ASKII table gives….

numbered order of all characters

23
New cards

print(menuitem2.index(‘spam))

returns the index of the first occurrence of 'spam' in the list 'menuitem2'.

24
New cards

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

25
New cards

when to use lists

when you need to keep a collection of data

26
New cards

what can lists contain

numbers, strings, other lists, combinations

27
New cards

difference between lists and strings

lists can hold combinations of data, string cannot

28
New cards

benefits of lists

  • flexible

  • dynamic

  • can change and delete anytime

  • can access using an index

29
New cards

python[2] = ‘__’

updating index 2 in list called python to contain __

30
New cards

del python[5]

deletes index of 5 in list python

31
New cards

x = [1, 2, 3]

y = [4, 5, 6]

z = x + y

[1, 2, 3, 4, 5, 6]

32
New cards

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

33
New cards

ingredients = menuitem2.split(',’)

print(ingredients)

splits all of menuitem2 with commas into a list of ingredients.

34
New cards

slicing

you can slice strings and lists to access their parts

35
New cards

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

36
New cards

name = “John Cleese”

name[:4]

returns 'John', the substring from the beginning of the string up to but not including index 4.

37
New cards

name = “John Cleese”

name[4:]

returns ' Cleese', the substring from index 4 to the end of the string.

38
New cards

name = “John Cleese”

name[:-2]

returns 'John Clee' = doesn’t include -2 which would be s

39
New cards

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

40
New cards

print(random.randrange(0,101,5)

returns values between 0 and 100 which are divisible by 5

41
New cards

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

42
New cards

Pseudo random number generator (PRNG)

long peruod

uniformly distributed

reproducible

quick and easy to compute

43
New cards

Linear Congruential Generator (LCG)

short period

not uniformly distributed

reproducible

poor quality

44
New cards

Monte Carlo techniques

  • model systems by simulating via random sampling

  • powerful, flexible, direct

  • sometimes the only feasible way to solve something

45
New cards

how to add an element into a list

list.append()

46
New cards

how would you make a string uppercase and then duplicate it?

mystring = “abcd”

mystring.upper()*2

OUTPUT = ABCDABCD