Data Types

Question 1.

The two variables, first_number and second_number, are given as follows:

first_number = 4
second_number = 8

Modify the following line by putting parenthesis around the two variables added:

average = first_number + second_number / 2

After modifying the above line, write a Python code that uses (1) the print() function and (2) the variables, first_number, second_number, and average to print out the following in the Spyder Console:

The average of 4 and 8 is 6.0


Answers:

average = (first_number + second_number) / 2
print("The average of", first_number, "and", second_number, "is", average)



Question 2.

The variables, string and second_number, are given as follows:

string_length = "length of string"

Write a Python code to count the length of string_length.


Answers:

len(string_length)




Data Structures

Question 3.

The variable list_variable is given:

list_variable = [7, True, 3, "Hello", 1.5]

Write a Python code to change the value True to value False in list_variable.


Answers:

list_variable[1] = False



Question 4.

  • Construct a dictionary with the keyword-value pairs: alpha and 1.0, beta and 3.141592, gamma and -100.

  • How can the value of alpha be retrieved from the dictionary?


Answers:

abc = dict(alpha = 1.0, beta = 3.141592, gamma = -100)
abc['alpha']




if-conditionals

Question 5.

Write a Python code that determines a person’s stage of life. Set a value for the variable age, and then:

  • If the person is less than 2 years old, print a message that
    The person is a baby!
  • If the person is at least 2 years old but less than 4, print a message that
    The person is a toddler!
  • If the person is at least 4 years old but less than 13, print a message that
    The person is an kid!
  • If the person is at least 13 years old but less than 20, print a message that
    The person is a teenager!
  • If the person is at least 20 years old but less than 65, print a message that
    The person is an adult!
  • If the person is age 65 or older, print a message that
    The person is an elder!


Answers:

age = 36

if age < 2:
    print("The person is a baby!")
elif age < 4:
    print("The person is a toddler!")
elif age < 13:
    print("The person is a kid!")
elif age < 20:
    print("The person is a teenager!")
elif age < 65:
    print("The person is an adult!")
else:
    print("The person is an elder!")




while-loops

Question 6.

  • Assign the value 7 to the variable guess_me, and the value 1 to the variable number.

  • Write a while-loop that compares number with guess_me.

    • Print ‘too low’ if number is less than guess_me.
    • If number equals guess_me, print ‘found it!’ and then exit the loop.
    • If number is greater than guess_me, print ‘oops’ and then exit the loop.
    • Increment number at the end of the loop.


Answers:

guess_me = 7
number = 1

while True:
    if number < guess_me:
        print('too low')
    elif number == guess_me:
        print('found it!')
        break
    elif number > guess_me:
        print('oops')
        break
    number += 1



for-loops

Question 7.

  • Assign a list of the integer numbers from 1 to 9 in numbers

  • After creating the list numbers, write a Python code that uses (1) for loop that processes each element in the list numbers and (2) if-elif-else statement inside the loop to print the proper ordinal ending for each number.

  • The output should read “1st 2nd 3rd 4th 5th 6th 7th 8th 9th”, and each result should be on a separate line.


Answers:

numbers = list(range(1,10))

for number in numbers:
    if number == 1:
        print(str(number) + "st")
    elif number == 2:
        print(str(number) + "nd")
    elif number == 3:
        print(str(number) + "rd")
    else:
        print(str(number) + "th")




try-except statement

Question 8.

Write a Python code that uses the input() function with a message in the Console “Please enter a number:” to print out

The number you entered is: NUMBER_ENTERED


if the number entered, NUMBER_ENTERED, in the Console is a number and


The value you entered is not a number.


if the number entered is not a number.


Answers:

try:
    value_entered = int(input("Please enter a number: "))
    print("The number you entered is:", str(value_entered))
except ValueError:
    print("The value you entered is not a number.")




Functions

Question 9.

Define a function called harrypotter() that returns the following list: ['Harry', 'Ron', 'Hermione'].


Answers:

def harrypotter():
    return ['Harry', 'Ron', 'Hermione']

harrypotter()