Booleans, Numbers, and Operations#
Drill#
1# Create a variable named pi and assign 3.14159 to it.
2pi = 3.14159
3
4# Next, create a variable named radius and assign a value to it.
5radius = 5
6
7# Now, create a variable named area and assign the expression of pi * radius * radius to it.
8area = pi * radius * radius
9
10# Print area to console.
11print(area)
12
13# Check the type of area using type()
14print(type(area))
15
16# Compute the circumference of the circle from the variable radius. Create a variable and print that variable.
17circumference = 2 * pi * radius
18print(circumference)
78.53975
<class 'float'>
31.4159
1#Create var_one with a value of 10 and var_two with a value of 3
2var_one = 10
3var_two = 3
1# Compute:
2# var_one plus var_two and assign to var_plus. Print var_plus
3var_plus = var_one + var_two
4print(var_plus)
13
1# var_one minus var_two and assign to var_minus. Print var_minus
2var_minus = var_one - var_two
3print(var_minus)
7
1# var_one times var_two and assign to var_times. Print var_times
2var_times = var_one * var_two
3print(var_times)
30
1# var_one divided by var_two and assign to var_div. Print var_div. Print the type as well.
2var_div = var_one / var_two
3print(var_div)
4print(type(var_div))
3.3333333333333335
<class 'float'>
1# var_one modulo var_two and assign to var_mod. Print var_mod. Print the type as well.
2var_mod = var_one % var_two
3print(var_mod)
4print(type(var_mod))
1
<class 'int'>
Exercises#
Write python code to convert 100 degrees Celsius to Fahrenheit.
1celsius = 100
2fahrenheit = (celsius * 9/5) + 32
3print(fahrenheit)
212.0
An internet coffee shop sells coffee for 12.85 per pound. The shipping cost is 3.25 per pound plus a flat handling fee of 2.50 per order. Using a variable named coffee_lbs, write a python statement that will compute the order cost and assign the value to a variable named cost. Then print that variable to the console.
1coffee_lbs = 2.5
2cost = 12.85 * coffee_lbs + 3.25 * coffee_lbs + 2.50
3print(cost)
42.75
Find the formula for periodic compounding. Write a snippet of python code that you can use to set the variables to apply the compounding formula. The code will be similar to the Fahrenheit conversion above, with a few more variables. You should have the following variables with P, r, n, and t where
A is the final amount
P is the original principal sum
r is the nominal annual interest rate
n is the compounding frequency
t is the overall length of time the interest is applied (expressed using the same time units as r, usually years).
1P = 1000
2r = 0.05
3t = 5
4n = 12
5A = P * (1 + r/n)**(n*t)
6print(A)
1283.3586785035118
How would you find the additional information for decimal class(type) within Python?
1help(type)
Help on class type in module builtins:
class type(object)
| type(object) -> the object's type
| type(name, bases, dict, **kwds) -> a new type
|
| Methods defined here:
|
| __call__(self, /, *args, **kwargs)
| Call self as a function.
|
| __delattr__(self, name, /)
| Implement delattr(self, name).
|
| __dir__(self, /)
| Specialized __dir__ implementation for types.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __instancecheck__(self, instance, /)
| Check if an object is an instance.
|
| __or__(self, value, /)
| Return self|value.
|
| __repr__(self, /)
| Return repr(self).
|
| __ror__(self, value, /)
| Return value|self.
|
| __setattr__(self, name, value, /)
| Implement setattr(self, name, value).
|
| __sizeof__(self, /)
| Return memory consumption of the type object.
|
| __subclasscheck__(self, subclass, /)
| Check if a class is a subclass.
|
| __subclasses__(self, /)
| Return a list of immediate subclasses.
|
| mro(self, /)
| Return a type's method resolution order.
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __prepare__(name, bases, /, **kwds)
| Create the namespace for the class statement
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs)
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __abstractmethods__
|
| __annotate__
|
| __annotations__
|
| __dict__
|
| __text_signature__
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __base__ = <class 'object'>
| The base class of the class hierarchy.
|
| When called, it accepts no arguments and returns a new featureless
| instance that has no instance attributes and cannot be given any.
|
|
| __bases__ = (<class 'object'>,)
|
| __basicsize__ = 936
|
| __dictoffset__ = 264
|
| __flags__ = 2155896066
|
| __itemsize__ = 40
|
| __mro__ = (<class 'type'>, <class 'object'>)
|
| __type_params__ = ()
|
| __weakrefoffset__ = 368
1?type
How would you find the additional information for decimal class(type) on the web?
Search for “python type docs”
LLM Query: “Explain the type class in Python”