CIS QUIZ 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Sentinel Value example

1. Ask user to enter a number 2. Add input into sum 3. Ask user to enter number 4. Print sum

Loop Example

1. count = 0 2. while count < 100: #loop continuation condition 3. ----->print("Programming is fun!\n") 4. count = count + 1 #loop body -->Prints programming is fun 100 times Execution Order: 12342

Infinite Loop

1. count=0 2. while count < 2: 3. -----> print("Programming is fun")

Suppose that s1 and s2 are two strings. s1 = "programming 101" s2 = "programming is fun" What are the results of the following expressions? 1) s1[0] 2) s1[ :5] 3) s1[5:] 4) s1+s2 5) s1*2

1. p 2.progr 3. amming 101 4. programming 101programming is fun 5. programming 101programming 101

Which of the following statements is the precise meaning for the statement n = 3? (a) n is a variable that holds int value 3. (b) n is a variable that references an object that holds int value 3.

B. n is a variable that references an object that holds int value 3

Given a string s = "Programming is fun", what is s.startswith('m')? A. 0 B. 1 C. -1 D. True E. False

E. False *string starts with 'P' would be true

A single execution of a loop body is called ....?

ITERATION (repetition) of loop

Analyze the following code. Is count < 100 always True, always False, or sometimes True or sometimes False at Point A, Point B, and Point C? count = 0 while count < 100: # Point A --> print("Programming is fun!") --> count += 1 # Point B # Point C

Point A: Always True Point B: Sometimes True Point C: Always False

Suppose that the tuition for a university is $10,000 this year and increases 5% every year. List the tuitions in next one-ten years (using a while loop). 1) System Design (List steps from input to output in English) 2) Implementation Year Tuition 1 .. 2. .. ... 10. ..

STEP 1: set tuition as 10000 tuition=10000 STEP 2: set year as 0 year= 0 STEP 3: print header print('year\ttuition') while year<10: ----->STEP 4: increase tuition from last year by 5% tuition=tuition*1.05 ------>STEP 5: increase year by 1 year=year+1 ------>STEP 6: print year and tuition from this year print(str(year)+'\t' + str(tuition)) \t= tab unicode

Loop-Continuation Condition

a Boolean expression that controls the body's execution *after each iteration the loop continuation is reevaluated -if True the body is repeated -if False the loop terminates

Suppose that s1 and s2 are two strings, given as follows: s1 = "Welcome to Python" s2 = "123" What are the results of the following expressions? a. s1.isalpha() b. s1.isupper() c. s1.isdigit() d. s2.isdigit() e. s1.count("ao") f. s1.find(s4) g. s1.startswith("Welco") h. s1.endswith("on")

a. FALSE b. FALSE c. FALSE d. TRUE e. 0 f. Error g. TRUE h. TRUE

str[] function

access on character from string EX: str=Programming is fun *positions start with 0, and include spaces str[0]=P str[12]= ' ' (a space) str[17] = runtime error (out of index)

str[start:end]

acess one substring from a string str[0:1] = 'P' (picks 0) str[14: ] = '3!' (picks 14, 15)

Convert the following while loop into a for loop. i = 1 sum = 0 while sum < 10000: ---->sum = sum + i ---->i += 1

for sum in range(0,10000,i): i=i+1

Printing w/o newline

if you don't want a print functions to be on seperate lines ---> print(item, end = "anystring") Ex; print("The area is", radius * radius * math.pi, end= ' , ') print("and the diameter is", 2 * radius) Results: The area is 28.27433, and the diameter is 6

Object Methods: Testing Strings

isalnum()--> true if all characters are alphanumeric (at least one #) isalpha()--> returns True if all character in str ae alphabetic isdigit()--> True if str ONLY contains #

Suppose the input is 2 3 4 5 0 (one number per line). What is the output ofthe following code? number = int(input("Enter an integer: ")) max = number while number != 0: --> number = int(input("Enter an integer: ")) --> if number > max: ---> max = number print("max is", max) print("number", number)

max is 5 number is 0

What is the output of the following code? Please also show the execution order of lines. 1. count = 1 2. while count < 4: 3. ---> count += 1 4. ---> print(count, end = ' ')

output: 2 3 4 Execution Order: 1 2 3 4 2 3 4 2 3 4 2

s.replace ('e', 'E')

replace the letter in the string with new letter Welcome--> WElcomE

s.lower()

returns string in lowercase

s.upper()

returns string in uppercase

Suppose the input is 2 3 4 5 0 (one number per line). What is the output of the following code? number = 0 sum = 0 for count in range(3): ---->number = int(input("Enter an integer: ")) ----->sum += number print("sum is", sum) print("count is", count)

sum is 9 count is 2

Ending Loop with an sentinel value

*Use input value to signify the end of loop Use when... - Number of iterations is NOT predetermined -->stop reading input if user enters 0

Negative numbers as index

-Python allows negative #s as indexes to reference positions relative to end of string -backwards *if starting backwards--> the last letter is position -1 Ex: s="Welcome" s[-1]='e' s[-2] 'm' (same as s[-1+len(s)])

Object

-numbers, strings, and datum is an object -id() and type() function to get info about object *id() ---> assigns unique integer when program is executed(will not be changed during the execution of program, however id can change every time the program is executed) Ex: n=3 (n is an integer) type(n)--> class int f=3.0 (f is float) type(f)--> class float

Slice Operators

-returns slice of the string using syntax--> s[start:end] Ex: S="Welcome" s[1:4] 'elc'---> returns substrings from index 1-3 *starting or ending index may be omitted *default-->start is 0 s='Welcome' s[ :6]-->'Welcom' *index 6 is omitted same as-->[0:6] returns index 0-5 s=[4: ]-->'ome' *4th position to the end of the string returns index 4-6 s=[1: -1]--->'elcom' *last index is omitted index 1 - (-1)


संबंधित स्टडी सेट्स

Chapter 19: Seizing an American Empire, 1865-1913

View Set

Building Therapeutic Relationships

View Set

#2 Mechanisms of Genetics. Transcription/Translation

View Set

Chapter 18 - Environmental Science

View Set