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



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.




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.



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?




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!




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.



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.




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.




Functions

Question 9.

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