The two variables, first_number and
second_number, are given as follows:
first_number = 4
second_number = 8Modify the following line by putting parenthesis around the two variables added:
average = first_number + second_number / 2After 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:
Answers:
average = (first_number + second_number) / 2
print("The average of", first_number, "and", second_number, "is", average)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)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] = FalseConstruct 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-conditionalsWrite a Python code that determines a person’s stage of life. Set a
value for the variable age, and then:
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-loopsAssign 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.
number is less than
guess_me.number equals guess_me, print ‘found
it!’ and then exit the loop.number is greater than guess_me, print
‘oops’ and then exit the loop.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 += 1for-loopsAssign 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 statementWrite a Python code that uses the input() function with
a message in the Console “Please enter a number:” to print out
if the number entered, NUMBER_ENTERED, in the Console is a number and
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.")Define a function called harrypotter() that returns the
following list: ['Harry', 'Ron', 'Hermione'].
Answers:
def harrypotter():
return ['Harry', 'Ron', 'Hermione']
harrypotter()