Shared Flashcard Set

Details

MIT 6.00.1x- Week1- Python and CS Basics
Problem Sets
9
Computer Science
Undergraduate 1
09/17/2016

Additional Computer Science Flashcards

 


 

Cards

Term
Write a piece of Python code that prints out the string hello world
Definition
x = "hello world"

print('hello world')
Term
Write a piece of Python code that prints out the string 'hello world' if the value of an integer variable, happy, is strictly greater than 2.
Definition
statement = 'hello world'

if happy > 2:
print(statement)
Term
Write a piece of Python code that prints out one of the following messages:

"string involved" if either varA or varB are strings

"bigger" if varA is larger than varB

"equal" if varA is equal to varB

"smaller" if varA is smaller than varB
Definition
if type(varA) == str or type(varB) == str:
print("string involved")

elif varA > varB:
print("bigger")

elif varA == varB:
print("equal")

else:
print("smaller")
Term
1. Convert the following into code that uses a while loop.

print 2
prints 4
prints 6
prints 8
prints 10
prints Goodbye!
Definition
x = 2

while(x <= 10):
print(x)
x=x+2
print("Goodbye!")
Term
2. Convert the following into code that uses a while loop.

prints Hello!
prints 10
prints 8
prints 6
prints 4
prints 2
Definition
x = 10
print("Hello!")
while(x>0):
print(x)
x-=2
Term
3. Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
Definition
current = end
temp = 0

while current > 0 :
temp+=current
current-=1

print(str(temp))
Term
1. Convert the following code into code that uses a for loop.

prints 2
prints 4
prints 6
prints 8
prints 10
prints "Goodbye!"
Definition
for x in range(2,12,2):
print(x)

print("Goodbye!")
Term
2. Convert the following code into code that uses a for loop.

prints "Hello!"
prints 10
prints 8
prints 6
prints 4
prints 2
Definition
print("Hello!")
for x in range(10,0,-2):
print(x)
Term
3. Write a for loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
Definition
start = 0
total = 0

for x in range(start,end):
total+=end
end-=1
print(total)
Supporting users have an ad free experience!