Sequences
Terms/Definitions
For Loops
Bonus Questions
100

a function that counts the number of characters in a string

len

100

A series of elements

Sequence(s)

100

how would you read the line of code below as the computer would read it:

for i in variable: 

for every element/character in variable

100

Why would this result in an error?

name = "PASS"

name[3] = "T"

Because strings are immutable

200

T/F: "Banana" the computer reads this as having characters ranging from 1 - 6.

False! it ranges from 0 - 5

200

_____ is an immutable data type.

a String/Boolean/Integer

200

Describe the similarity between a while loop and a for loop

They both repeat the code inside/within the loop

200

Write a code that would locate the position of 'n' in the following string:

long_str = "Coding is easy!"


long_str = "Coding is easy!"

locate = long_str.find("n")

print(locate)

What is printed?

300

To print all characters in 'letter' individually line-by-line what and where would the print statement be?

letter  = "xyz"

for var in letter:

__________________

letter  = "xyz"

for var in letter:

    print(var)

300

When we go through the code line by line and read it as though the computer would read it

Code-Tracing

300

What do the numbers (1, 10, 2) represent, respectively?

for num in range(1, 10, 2):

    print(num)

start, stop, step

300

What does this line of code do in simple terms?

myfile = open("file.txt", "r")

reads from a file

400

What is printed?

food = input("Enter a food item: ")

for n in food:

    print("The food is ", n, food[2])

Enter a food item: cake

The food is  c k

The food is  a k

The food is  k k

The food is  e k

400

__________ a sequence means accessing its elements one by one.

Traversing a sequence means accessing its elements one by one.

400

Describe the difference between a while loop and a for loop

In a while, we control how many times the loop goes around with a condition and/or break statement

In a for, we control how many times the loop goes around within the sequence

400

write a program that prints the positions of a string of letters:

string_letters = "Jeopardy!"

string_letters = "Jeopardy!"

for i in range(len(string_letters)):

    print(i)

500

Write a program that asks the user's first name and age then prints out the characters one per line

firstname = input("What is your first name? ")

age = input("What is your age? ")

nameage = firstname + age

for i in nameage:

    print(i)

500

For each iteration of the ____ loop, _____ returns the next number in the range and assigns it to the variable we called variable

For each iteration of the for loop, range returns the next number in the range and assigns it to the variable we called variable

500

Write an alternative program using a for loop that would produce the same output as the code below

counter = 0

name = "PASS"

while counter < len(name):

    print(name[counter])

    counter = counter + 1


name = "PASS"

for i in name:

    print(i)

500

Write a program that asks the user for their email and prints only the email domain

example: pass@gmail.com

prints --> gmail

email = input("Email: ")

address = email.find("@")

dot = email.find(".")

print("The email domain is ", email[address+1:dot])