- Write numeric expressions in Python for these value:
-
4 + 3 * 2n-2
------------
10
- the sum of the cubes of integers x and
y
- the number of seconds in 4 hours, 14 minutes, and 32 seconds
(There are sixty seconds in a minute, and sixty minutes
in an hour.)
- Write boolean expressions in Python for these conditions:
- x is a factor of y (that is, x
divides evenly into y)
- age is at least 18 and state
equals 'Iowa'
- the string name contains a 'z'
- Consider this code:
if x % 2 == 1:
if x**3 != 27:
x = x + 4
else:
x = x / 1.5
else:
if x <= 10:
x = x * 2
else:
x = x - 2
print(x)
What does this code print if x == 8?
- The user enters the birthdays of, a and b,
two people who were born on different days in the same year.
Write Python code that prints a is younger
if a's birthday comes before b's, and
b is younger if b's birthday comes
before a's.
a_month = int( input('Enter the month: ') )
a_day = int( input('Enter the day : ') )
b_month = int( input('Enter the month: ') )
b_day = int( input('Enter the day : ') )
# fill in the blank
- Answer briefly each of these questions about algorithms and
programs:
- In what ways is an algorithm similar to a program?
- In what ways is an algorithm different from a program?
- Consider this code:
x = input('Enter a string: ')
y = 0
for i in x:
print(y, i)
y += 1
What does this code print if the user enters 'Felix'?
- Write a Python loop to print the sum of all the odd numbers
between 1 to n, for a given value of n.
For example, the sum of the odds between 1 and 7 is
1 + 3 + 5 + 7 = 16.
- Consider this code:
x = input('Enter a string: ')
y = 0
for i in x:
if i == 'a':
y += 1
print(y)
Write a while loop that does exactly the
same thing as the for loop.
- Answer briefly each of these questions about loops:
- What does the break statement do in a
loop?
- What is a loop and a half?
- What must the body of a while loop do
that the body of a for loop does not
need to do?
- Suppose we want to censor a string in this way:
Enter the source : eugene
Enter char to replace: e
Enter char to insert : *
Result: *ug*n*
Write Python code that prints out the characters in
string str, only with character old_ch
replaced with character new_ch.
str = input('Enter the source : ')
old_ch = input('Enter char to replace: ')
new_ch = input('Enter char to insert : ')
# fill in the blank
print('\nResult:', result)