Here are our solutions for the day 1 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Print your age to the console.
For this exercise, we just need a single
2) Calculate and print the number of days, weeks, and months in 27 years.
Exercise 2 is a little more challenging as we have to do a little bit of maths. For this exercise, we don't have to worry about leap years, so we can just multiply the number of days, weeks, or months in a single year by
print(27 * 365) print(27 * 52) print(27 * 12)
That should give us 9855 days, 1404 weeks, and 324 months.
3) Calculate and print the area of a circle with a radius of 5 units.
For this exercise we can be pretty loose with the value of pi, so I'm going to use
If you don't remember how to calculate the area of a circle, the formula for the area is pi multiplied by the radius squared: commonly written as πr².
In code we can write this as follows:
print(3.141 * 5 * 5)
3.141 (pi) multiplied by
5 squared, which is just
The exercise also tells us to look for additional operators, and there's actually an operator for exponents (
**) which we can use to square a number.
print(3.141 * 5 ** 2)
We can also use the
pow function to do the same thing with slightly different syntax:
print(3.141 * pow(5, 2))
The first value we provide to
pow is the base number, and the second value is the exponent. You can read about
pow in the official documentation.
All of these approaches are totally fine, and they all produce the exact same result. Using my approximate value for pi, we get
78.525 as the area of the circle.