Variables Exercises#

Section Title: Variables Exercises

Warm Up Question#

Define a variable called quantity and assign it the value 25. Display its ID number and type.

Warm Up Question#

Which of the following cannot be used as a variable name, and why?

  1. _name

  2. 1player

  3. player_name

  4. birth year

  5. True

Warm Up Question#

Determine the output of the code below and provide an explanation.

x = 5
print(x)
x = 7
print(x)

Warm Up Question#

Use the provided variables and print() function to output the statement: Harry is 23 years old.

name, age = 'Harry', 23

Warm Up Question#

Use the given variables and string concatenation to display 35.

x, y = 3, 5

Question-1: Name Tag#

Use the following information to create the variables name, league, age, and height with the given values.

  • Create the birthyear variable using the age variable.

  • Design a name tag as given below.

  • There is a space between each asterisk in the first and last lines.

Michael Jordan played basketball in the NBA. In 2024, he is 61 years old, and his height is 1.98 meters.

Sample Output:

 A rectangular frame made of asterisks that includes the following text:Name     : Michael JordanLeague   : NBAAge      : 61Birthyear: 1963Height   : 1.98 m

Question-2#

By using the variables \(x\) and \(y\) given below, print 353535 using 4 different pieces of code.

x, y = 3, 5

Question-3#

Print the statement I went to Italy in 2015. using the country and year variables below.

country = 'Italy'
year = 2015

Warning: There is no space before the period.

Question-4: Circle#

Create a variable named r and assign the value of \(5\) to it.

  • Print the area and perimeter of the circle with a radius equal to r.

  • Round them to the nearest hundredth.

  • Hint:

    • Perimeter = \(2\pi r\), Area = \(\pi r^2\)

    • Import \(\pi\) from a module.

  • Output:

    • Perimeter: 31.42

    • Area : 78.54

Question-5: U-Shape#

Print the letter U using the character + and the variable horizontal.

horizontal = 3
  • The vertical left and right parts of U have five + characters each.

  • The variable horizontal represents the number of + characters with one space between them in the bottom horizontal part.

Examples The U shape is formed using + characters. The first four rows each contain two + characters with a varying number of spaces between them, depending on the horizontal variable's value. The final row (fifth) contains a number of + characters equal to the horizontal variable's value, with one space separating each +.

Question-6: V-Shape#

Print the following using the character *, spaces and the variable n=5 to represent the number of * characters in each block.

In the first row, there are two blocks of n stars with 7n spaces between them.In the second row, there are two blocks of n stars with 5n spaces between them.In the third row, there are two blocks of n stars with 3n spaces between them.In the fourth row, there are two blocks of n stars with n spaces between them.In the fifth (last) row, there is a single block of n stars.

Question-7: H-Shape#

Print the following ‘H’ shapes using the characters ‘+’, ‘ ‘, ‘-’, and the variable number_dash.

number_dash = 5
  • The number of ‘-’ characters in the middle line is represented by the variable ‘number_dash’.

Examples In the first and third (last) rows, there are two + characters with a certain number of spaces between them, depending on the number_dash variable. In the second row, there are two + characters with a certain number of dashes between them, also determined by the number_dash variable. There is a spaceafter each dash.

Business Applications#

Question: Demand Function#

The demand, p = D(q), function shows how much of a product consumers want (q=quantity) and the price (p) they are willing to pay.

The demand function is given as the following linear relation: \(p = -2q+300\)

  • Determine the price if the number of items demanded is 75.

  • Use the variables \(q\) and \(p\).

Question: Demand Equation#

A store can sell 1200 ice creams at 3 dollars each. If the price is 5 dollars, they can sell 800 ice creams.
What will be the price if 500 ice creams are sold, assuming the demand is linear?

Hint:

  • Since the relation between p and q is linear, we have \(p=mq+b\)

  • The slope \(m\) is calculated using the formula \(\displaystyle m=\frac{p_2-p_1}{q_2-q_1}\) for given two points \((q_1,p_1)\) and \((q_2,p_2)\).

  • The y-intercept \(b\) can be found using one of the points \((q_1,p_1)\) or \((q_2,p_2)\) so \(b=p_1 - mq_1\) or \(b=p_2 - mq_2\)

  • Use the variables \(q_1, p_1, q_2, p_2\) for the given values in the question.

Question: Supply Function#

The supply function, \(p=S(q)\), shows how the price (p) affects the quantity (q) that producers want to supply.

The supply function is given by the linear relation \(p = 4q+100\).

  • Determine the price when 120 items are supplied.

  • Use the variables \(q\) and \(p\).

Question: Supply Equation#

A store can supply 2000 ice creams at 4 dollars each. If the price is 6 dollars, they can supply 6000 ice creams. How many ice creams can they supply at 5 dollars, assuming the supply is linear?

Hint:

  • Since the relation between \(p\) and \(q\) is linear, we have \(p=mq+b\)

  • The slope \(m\) is calculated using the formula \(\displaystyle m=\frac{p_2-p_1}{q_2-q_1}\) for given two points \((q_1,p_1)\) and \((q_2,p_2)\).

  • The y-intercept \(b\) can be found using one of the points \((q_1,p_1)\) or \((q_2,p_2)\) so \(b=p_1 - mq_1\) or \(b=p_2 - mq_2\)

  • Use the variables \(q_1, p_1, q_2, p_2\) for the given values in the question.

Question: Simple Interest#

Simple Interest Formulas: \(\displaystyle I=P r t\) and \(A = P + I\) where

  • A is the final amount

  • I is the interest amount

  • P is the initial (principal) amount

  • r is the annual interest rate (decimal form)

  • t is the number of years

You borrow 1,200 dollars for 8 months at a 24% annual simple interest rate.

  • How much interest will you pay?

  • What is the total amount you will repay after 8 months?

Hint:

  • Use the variables \(P, r, t\) for the given values.

  • Calculate the values of the variables \(I\) and \(A\).

  • t is 8/12 years.

Output:
a) I = 192.0
b) A = 1392.0

Question: Compound Interest#

Compund interest formula: \(\displaystyle A=P\left(1+{\frac {r}{n}}\right)^{nt}\) where

  • A is the final amount

  • P is the initial (principal) amount

  • r is the annual interest rate (decimal form)

  • n is the compounding frequency (anual: n=1, semi annual: r=2, quarter: r=4, monthly: r=12)

  • t is the number of years

If you deposit 800 dollars into a savings account with a 2.75 percent interest rate compounded monthly, what will be your balance after five years (Round your answer to the nearest hundredth)?

Hint:

  • Use the variables \(P, r, n, t\) for the given values.

  • Calculate the value of the variable \(A\).

Question: Continuous Compound Interest#

Continuous Compound Interest formula: \(\displaystyle A=Pe^{rt}\) where

  • A is the final amount

  • P is the initial (principal) amount

  • r is the annual interest rate (decimal form)

  • t is the number of years

If you deposit 500 dollars into a savings account with a 5.25 percent interest rate compounded continuously, what will be your balance after ten years (Round your answer to the nearest hundredth)?

Hint:

  • Use the variables \(P, r, t\) for the given values.

  • Calculate the value of the variable \(A\).

  • import the number e from the math module.