Variables Exercises#

Question-0: Warm Up#

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

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:

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

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.

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

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.