Lesson · 40 min · Free
Python Numbers: Basics
Python Numbers: Basics body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code { font-fami
Python Numbers: Basics
Welcome to the foundational lesson on Python numbers, a critical data type for any scientific computing, including pharmaceutical research. In this module, we'll explore the fundamental ways Python handles numerical data, which is essential for working with dosages, concentrations, experimental results, and statistical analyses. Python categorizes numbers into several built-in types. The two most commonly used are integers ( int ) and floating-point numbers ( float ). Understanding their differences and how to perform basic operations with them is paramount.
Integers and Floats: The Core Numerical Types
Integers are whole numbers, positive or negative, without a decimal point. Examples include 10 , -5 , 1000 . In pharmaceutical contexts, you might use integers to represent the number of patients in a study, the count of pills in a bottle, or discrete scores from a questionnaire. Floating-point numbers , or "floats," are numbers that have a decimal point. They are used to represent real numbers and are crucial for measurements that require precision, such as drug concentrations (e.g., 0.5 mg/mL ), pH values (e.g., 7.4 ), or temperature readings (e.g., 37.5 degrees Celsius). Floats can also be represented in scientific notation (e.g., 6.022e23 for Avogadro's number). Python automatically infers the type of a number based on how it's written. You can always check the type of a variable using the type() function. # Example 1: Integers and Floats # Integer variable patient_count = 150 print(f"Patient count: {patient_count}, Type: {type(patient_count)}") # Floating-point variable drug_concentration = 0.75 # mg/mL print(f"Drug concentration: {drug_concentration}, Type: {type(drug_concentration)}") # Another float, using scientific notation avogadro_number = 6.022e23 print(f"Avogadro's number: {avogadro_number}, Type: {type(avogadro_number)}") # A whole number written as a float temperature = 37.0 print(f"Temperature: {temperature}, Type: {type(temperature)}")
Basic Arithmetic Operations
Python supports standard arithmetic operations, which are straightforward and intuitive. These include addition ( + ), subtraction ( - ), multiplication ( * ), division ( / ), integer division ( // ), modulo ( % ), and exponentiation ( ** ). Understanding these operators is fundamental for calculating dosages, dilutions, reaction rates, and more. + : Addition - : Subtraction * : Multiplication / : Division (always returns a float) // : Integer Division (returns the quotient as an integer, discarding the remainder) % : Modulo (returns the remainder of the division) ** : Exponentiation (e.g., 2**3 is 2 to the power of 3) # Example 2: Arithmetic Operations # Drug dosage calculation initial_dose = 200 # mg doses_per_day = 3 total_daily_dose = initial_dose * doses_per_day print(f"Total daily dose: {total_daily_dose} mg") # Dilution factor stock_concentration = 100 # mM target_concentration = 25 # mM dilution_factor = stock_concentration / target_concentration print(f"Dilution factor needed: {dilution_factor}") # Remaining drug after administration (modulo) total_pills = 17 pills_per_dose = 3 remaining_pills = total_pills % pills_per_dose print(f"Pills remaining after full doses: {remaining_pills}") # Half-life calculation (simple example) initial_amount = 100 # units half_life_cycles = 2 amount_after_cycles = initial_amount / (2 ** half_life_cycles) print(f"Amount after {half_life_cycles} half-life cycles: {amount_after_cycles} units") It's important to note that division ( / ) in Python 3 always results in a float, even if the numbers divide evenly. If you need an integer result from division, use integer division ( // ).
Key Takeaways
Python distinguishes between integers (whole numbers, int ) and floating-point numbers (numbers with decimals, float ). Use type() to inspect the data type of a variable. Standard arithmetic operators ( + , - , * , / , // , % , ** ) are available for numerical calculations. Division ( / ) always yields a float; integer division ( // ) truncates the decimal part. Understanding these basic numerical types and operations is fundamental for quantitative analysis in pharmaceutical research.
Practice Exercise
A new drug formulation requires a precise concentration. You have a stock solution of 500 mg/mL. You need to prepare 200 mL of a working solution with a concentration of 15 mg/mL. Calculate the following using Python: The total amount of drug (in mg) needed for the working solution. The volume (in mL) of the stock solution required. If you have 100 mL of the stock solution available, how many full working solutions (200 mL each) can you prepare? Write Python code to solve these problems and print the results with appropriate labels.
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →