Lesson · 40 min · Free
Python Basics: Get Started
Python Basics: Get Started 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-
Python Basics: Get Started
Welcome to the first lesson of our "Python Programming - Basics" course! As future pharmacists and biotech professionals, understanding basic programming concepts, especially in a versatile language like Python, can significantly enhance your ability to analyze data, automate tasks, and even develop custom tools for research and clinical applications. Python's readability and extensive libraries make it an excellent choice for scientific computing. In this lesson, we'll cover the absolute fundamentals: how to write and execute your first Python code, understanding basic data types, and performing simple operations. Think of this as laying the groundwork for more advanced data manipulation and algorithmic thinking that will be crucial in your scientific endeavors.
Your First Python Program: "Hello, World!"
The traditional first program in any language is "Hello, World!". It demonstrates how to print output to the console. In Python, this is remarkably simple, reflecting the language's design philosophy of clarity and conciseness. # This is a comment in Python. It's ignored by the interpreter. # Comments are useful for explaining your code. print("Hello, World!") When you run this code, the output will simply be: Hello, World! . The print() function is a built-in Python function used to display messages or variable values. The text inside the parentheses, enclosed in double quotes, is a string – a sequence of characters. Let's look at another example that introduces variables and basic arithmetic. Variables are symbolic names that refer to values. They are essential for storing and manipulating data. # Assigning values to variables drug_dose_mg = 250 patient_weight_kg = 75 daily_doses = 2 # Calculating total daily intake total_daily_intake_mg = drug_dose_mg * daily_doses # Calculating dose per kg dose_per_kg = total_daily_intake_mg / patient_weight_kg # Printing the results with descriptive text print(f"Daily drug intake: {total_daily_intake_mg} mg") print(f"Dose per kg: {dose_per_kg:.2f} mg/kg") In this example, we've defined three variables: drug_dose_mg , patient_weight_kg , and daily_doses . We then perform multiplication ( * ) and division ( / ) to calculate total_daily_intake_mg and dose_per_kg . Notice the use of an f-string (formatted string literal) for printing. This allows us to embed expressions inside string literals by prefixing the string with f and enclosing the expressions in curly braces {} . The :.2f within the f-string for dose_per_kg formats the floating-point number to two decimal places, which is often crucial for presenting scientific data.
Key Takeaways
Python uses print() to display output to the console. Text enclosed in quotes (e.g., "Hello") is a string. Variables are used to store data and are assigned using the = operator. Python supports standard arithmetic operations: + (addition), - (subtraction), * (multiplication), / (division). Comments, starting with # , are used to explain code and are ignored by the interpreter. F-strings provide a convenient way to embed expressions inside string literals.
Practice Exercise
Imagine you are analyzing a new compound. You have determined its molecular weight to be 342.3 g/mol and you need to prepare a solution with a concentration of 0.5 M . If you want to prepare 100 mL of this solution, write a Python script that calculates the mass of the compound (in grams) required. Remember that Molarity (M) = moles / Liters, and moles = mass / molecular weight. Your output should clearly state the required mass, formatted to two decimal places. Hint: Convert mL to L first.
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 →