Lesson · 40 min · Free
Python Operators - Basics
Python Operators - Basics body { font-family: sans-serif; line-height: 1.6; margin: 20px; } pre { background-color: #eee; padding: 10px; border-radius: 5px; overflow-x: auto; } code { font-family: monospace; } h1, h2 { c
Python Operators - Basics
Welcome to the "Python Operators - Basics" lesson, a foundational component of your "Python Programming - Basics" course. For pharmacy and biotech students, understanding operators is crucial as they form the backbone of any computation, data manipulation, and logical decision-making you'll perform in your scientific work. Whether you're calculating drug dosages, analyzing gene sequences, or processing experimental data, operators will be your primary tools. In essence, operators are special symbols or keywords that perform operations on one or more values (operands). These operations can range from simple arithmetic like addition and subtraction to more complex comparisons and logical evaluations. Python, being a highly versatile language, provides a rich set of operators that allow you to express complex calculations and conditions concisely.
Types of Operators in Python
Python categorizes operators into several types, each serving a distinct purpose. For this introductory lesson, we will focus on the most commonly used categories that are directly applicable to scientific programming: Arithmetic Operators: Used for mathematical calculations. Comparison (Relational) Operators: Used to compare two values and return a Boolean (True/False) result. Logical Operators: Used to combine conditional statements. Assignment Operators: Used to assign values to variables.
Arithmetic Operators
These operators are fundamental for any quantitative analysis. They allow you to perform basic mathematical operations on numerical data. Consider their application in calculating concentrations, reaction rates, or statistical parameters. + (Addition): Adds two operands. - (Subtraction): Subtracts the right operand from the left. * (Multiplication): Multiplies two operands. / (Division): Divides the left operand by the right (results in a float). % (Modulus): Returns the remainder of the division. Useful for checking divisibility or cyclic operations. ** (Exponentiation): Raises the left operand to the power of the right. // (Floor Division): Divides and returns the integer part of the quotient. # Example of Arithmetic Operators in a biotech context initial_concentration = 100 # mg/mL dilution_factor = 0.5 volume_original = 50 # mL volume_added = 25 # mL # Calculate final concentration after dilution final_concentration = initial_concentration * dilution_factor print(f"Final concentration after dilution: {final_concentration} mg/mL") # Output: 50.0 mg/mL # Calculate total volume after adding more solvent total_volume = volume_original + volume_added print(f"Total volume: {total_volume} mL") # Output: 75 mL # Calculate the square of a measurement measurement = 3.5 measurement_squared = measurement ** 2 print(f"Measurement squared: {measurement_squared}") # Output: 12.25 # Floor division for integer-based calculations, e.g., number of full batches total_samples = 17 samples_per_batch = 5 num_batches = total_samples // samples_per_batch print(f"Number of full batches: {num_batches}") # Output: 3
Comparison Operators
Comparison operators are vital for creating conditional logic in your programs. They allow you to compare two values and determine their relationship, returning either True or False . This is essential for tasks like checking if a patient's vital sign is within a normal range, or if a reaction has reached a certain threshold. == (Equal to): True if both operands are equal. != (Not equal to): True if operands are not equal. > (Greater than): True if the left operand is greater than the right. < (Less than): True if the left operand is less than the right. >= (Greater than or equal to): True if the left operand is greater than or equal to the right. <= (Less than or equal to): True if the left operand is less than or equal to the right. # Example of Comparison Operators in a pharmacy context patient_temperature = 38.2 # Celsius normal_temp_max = 37.5 normal_temp_min = 36.5 # Check if patient has a fever has_fever = patient_temperature > normal_temp_max print(f"Does the patient have a fever? {has_fever}") # Output: True # Check if temperature is within normal range is_normal_temp = (patient_temperature >= normal_temp_min) and (patient_temperature
Logical Operators
Logical operators combine conditional statements, allowing you to build more complex decision-making structures. They are particularly useful when you need to evaluate multiple criteria simultaneously, such as checking for multiple symptoms in a diagnosis or ensuring several experimental conditions are met. and : Returns True if both statements are true. or : Returns True if at least one of the statements is true. not : Reverses the result; returns False if the result is true.
Assignment Operators
Assignment operators are used to assign values to variables. The most basic is = , but Python offers shorthand operators that combine an arithmetic operation with an assignment, making your code more concise and often more efficient. = : Assigns value from right operand to left operand. += (Add and assign): x += y is equivalent to x = x + y -= (Subtract and assign): x -= y is equivalent to x = x - y *= (Multiply and assign): x *= y is equivalent to x = x * y /= (Divide and assign): x /= y is equivalent to x = x / y %= (Modulus and assign): x %= y is equivalent to x = x % y **= (Exponent and assign): x **= y is equivalent to x = x ** y //= (Floor Division and assign): x //= y is equivalent to x = x // y
Key Takeaways
Operators are special symbols or keywords that perform operations on values. Arithmetic operators facilitate mathematical calculations (e.g., + , - , * , / , ** , % , // ). Comparison operators evaluate relationships between values, returning True or False (e.g., == , != , > , < , >= , <= ). Logical operators combine conditional statements ( and , or , not ). Assignment operators simplify variable assignment, often combining an operation with the assignment (e.g., = , += , -= ). Understanding operators is foundational for writing effective code for data analysis, simulations, and decision-making in pharmacy and biotech.
Practice Exercise
A new drug is being tested, and its efficacy is measured by the percentage reduction in a biomarker. The initial biomarker level is 150 units . After administering the drug, the level drops to 120 units . The drug is considered effective if the reduction is at least 15% AND the final biomarker level is below 130 units . Calculate the percentage reduction, and then use comparison and logical operators to determine if the drug is effective according to these criteria. Print the percentage reduction and the boolean result for drug effectiveness. Hint: Percentage reduction = ((initial_level - final_level) / initial_level) * 100
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →