Cs133p chapter 9

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How many times is the letter o printed by the following statements? s = "python rocks" idx = 1 while idx < len(s): print(s[idx]) idx = idx + 2 (A) 0 (B) 1 (C) 2

(A) 0 Yes, idx goes through the odd numbers starting at 1. o is at position 4 and 8.

Evaluate the following comparison: "Dog" < "Doghouse" (A) True (B) False

(A) True---Both match up to the g but Dog is shorter than Doghouse so it comes first in the dictionary.

What is printed by the following statements? s = "python rocks" print(s[-3]) A) c (B) k (C) s (D) Error, negative indices are illegal.

(A) c

What is printed by the following statements? s = "python" excl = "!" print(s+excl*3) (A) python!!! (B) python!python!python! (C) pythonpythonpython! (D) Error, you cannot perform concatenation and repetition at the same time.

(A) python!!! repetition has precedence over concatenation

What is printed by the following statements? s = "python rocks" print(s[7:11] * 3) (A) rockrockrock (B) rock rock rock (C) rocksrocksrocks (D) Error, you cannot use repetition with slicing.

(A) rockrockrock

What is printed by the following statements? s = "python rocks" print(len(s)) (A) 11 (B) 12

(B) 12

How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO") (A) 4 (B) 5 (C) 6 (D) Error, the for statement cannot use slice.

(B) 5

Evaluate the following comparison: "dog" < "Doghouse" (A) True (B) False

(B) False--- The length does not matter. Lower case d is greater than upper case D.

Evaluate the following comparison: "dog" < "Dog" (A) True (B) False (C) They are the same word

(B) False--- Yes, upper case is less than lower case according to the ordinal values of the characters.

What is printed by the following statements? s = "python rocks" print(s[len(s)-5]) (A) o (B) r (C) s (D) Error, len(s) is 12 and there is no index 12.

(B) r

How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s: print("HELLO") (A) 10 (B) 11 (C) 12 (D) Error, the for statement needs to use the range function.

(C) 12

strings-11-1: How many times is the letter o printed by the following statements? s = "python rocks" for idx in range(len(s)): if idx % 2 == 0: print(s[idx]) (A) 0 (B) 1 (C) 2 (D) Error, the for statement cannot have an if inside.

(C) 2

What is printed by the following statements: s = "Ball" s[0] = "C" print(s) (A) Ball (B) Call (C) Error

(C) Error

What is printed by the following statements: s = "ball" r = "" for item in s: r = item.upper() + r print(r) (A) Ball (B) BALL (C) LLAB

(C) LLAB Yes, the order is reversed due to the order of the concatenation.

What is printed by the following statements? s = "python rocks" print(s[3:8]) (A) python (B) rocks (C) hon r (D) Error, you cannot have two numbers inside the [ ].

(C) hon r

What is printed by the following statements? s = "python" t = "rocks" print(s + t) (A) python rocks (B) python (C) pythonrocks (D) Error, you cannot add two strings together.

(C) pythonrocks-the two strings are glued end to end.

The ___operator also works on strings. It performs repetition.

* we expect "Go"*3 to be the same as "Go"+"Go"+"Go"

slicing ([:])

A slice is a substring of a string. Example: 'bananas and cream'[3:6] evaluates to ana (so does 'bananas and cream'[1:4]).

indexing ([])

Access a single character in a string using its position (starting from 0). Example: 'This'[2] evaluates to 'i'.

length function (len)

Returns the number of characters in a string. Example: len('happy') evaluates to 5.

for loop traversal (for)

Traversing a string means accessing each character in the string, one at a time. For example, the following for loop: for ix in 'Example': ... executes the body of the loop 7 times with different values of ix each time.

Types that are comprised of smaller pieces are called

collection data types.

A string that contains no characters, often referred to as the

empty string

The expression in brackets is called an

index. An index specifies a member of an ordered collection.

The loop variable achar is automatically reassigned each character in the string "Go Spot Go". We will refer to this type of sequence iteration as

iteration by item

In the case of strings, they are made up of smaller strings each containing

one character

The way you can find out the so-called ordinal value for a given character is to use a character function called

ord

The len function, when applied to a string

returns the number of characters in a string.

The indexing operator

selects a single character from a string. The characters are accessed by their position or index value.

A substring of a string is called a

slice

A lot of computations involve processing a collection one item at a time. For strings this means that we would like to process one character at a time. Often we start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a

traversal.

Strings are immutable,

which means you cannot change an existing string.


Set pelajaran terkait

Chapter One (Term & Concepts Review)

View Set

Loan Origination Activities Terminology Quiz

View Set

DM Chapter 6: Multiple Linear Regression

View Set

LEED Synergistic Opportunities Practice Test

View Set