a function that counts the number of characters in a string
len
A series of elements
Sequence(s)
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
Why would this result in an error?
name = "PASS"
name[3] = "T"
Because strings are immutable
T/F: "Banana" the computer reads this as having characters ranging from 1 - 6.
False! it ranges from 0 - 5
_____ is an immutable data type.
a String/Boolean/Integer
Describe the similarity between a while loop and a for loop
They both repeat the code inside/within the loop
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?
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)
When we go through the code line by line and read it as though the computer would read it
Code-Tracing
What do the numbers (1, 10, 2) represent, respectively?
for num in range(1, 10, 2):
print(num)
start, stop, step
What does this line of code do in simple terms?
myfile = open("file.txt", "r")
reads from a file
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
__________ a sequence means accessing its elements one by one.
Traversing a sequence means accessing its elements one by one.
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
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)
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)
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
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)
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])