Python Interactive Guide - Step 1: Basic Syntax and Data Types (2) - int, float
Series - Python Interactive Guide
Contents
Info
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
This is a continuation of “Step 1: Basic Syntax and Data Types”.
1.3. Basic Data Types
1.3.1. What is a Type?
- Python allows you to work with various kinds of data, such as strings and numbers. These different kinds of data are called types.
- You can use the
type()function to check the type of data.- Functions are, simply put, mechanisms that take input data, process it, and return output results. We’ll cover them in more detail later.
- From the execution result, we can see that the string
"Hello!"is of typestrand the number100is of typeint.
1.3.2. Numbers (int Type, float Type)
- In Python, there are two basic numeric types: integers (
int) and floating-point numbers (float).
Basic Numeric Operations
- You can perform addition (
+), subtraction (-), multiplication (*), and division (/) operations with numbers. - You can also use the
printfunction to display numeric values on the screen.
- You can also calculate the quotient (
//) and remainder (%) in division.
- You can also perform exponentiation (
**) calculations.
📚 Exercise
BMI (Body Mass Index) is calculated by dividing weight (kg) by the square of height (m). Calculate and display the BMI for a person weighing 65kg with a height of 170cm.
Style Guide
- For large numbers, you can use underscores (
_) as digit separators
- Put spaces before and after binary operators
Type Conversion
- You can use the
intfunction to convert a decimal to an integer (removing the decimal portion). - You can use the
floatfunction to convert an integer to a decimal.
Maximum and Minimum Values for int Type
- The
inttype has no upper or lower limit.- You can work with numbers of any size as long as memory allows.
Scientific Notation
- With
floattype, you can use scientific notation.- For example, $3.14 \times 10^{5}$ can be written as
3.14e5.
- For example, $3.14 \times 10^{5}$ can be written as
Maximum and Minimum Values for float Type
- The
floattype has upper and lower limits.
- In
floattype, values exceeding the upper (lower) limit becomeinf(-inf).
- In some operations, an
OverflowErroroccurs if the calculation result exceeds the upper (lower) limit.
Float Type Precision
- The
floattype also has limitations in precision.
- Special care is needed for
floatcalculations.
This type of error is called a “rounding error” and occurs because decimal numbers are represented internally in binary. For calculations requiring high precision, Python provides the Decimal data type.
📚 Exercise
The area of a circle is calculated by multiplying the square of the radius by pi ($\pi$).
Calculate the area of a circle with a diameter of 10cm, and display the result as an integer by truncating the decimal part.
(Use 3.14 for pi)