CSCI-UA.2 - Introduction to Programming (Python) - Department of Computer Science, New York University


Review Problems - prepartion for the first midterm exam

1. Write a program that will generate 1000 numbers randomly.

2. Write a program that will ask the user to enter a four digit number. You program should print the sum of the digits:

Sample run:

Enter a four digit number: 4231

Output:

The sum of the digits is 10.

3. Evaluate the following expressions. Provide the result (one value) and also indicate the data type of the result.

  EXPRESSION Result Data type
  2 // 7 _____________ _____________
  3 / 2 _____________ _____________
  10 % 3 _____________ _____________
  1 * 3 - 3 % 4 // 3 + (5 % 3) _____________ _____________
  ((2 == 3) and (3 < 4)) or (39 >= 2) _____________ _____________
  not(2 == 2) _____________ _____________
  random.randint(30,300) _____________ _____________
  random.random() _____________ _____________
  x = 10 _____________ _____________
  x /= 2 _____________ _____________

4. Which of the following causes a number to be placed in variable x? (circle the correct answer(s))

a) x = "20"
b) x = int(input("Enter your number"))
c) x = str(300)
d) x = input("Type your number")
e) x = float("123")


5. How many times would each while loop be executed?

loops