Lesson · 40 min · Free
Python Intro & Basics
Python Intro & 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-famil
Python Intro & Basics
Welcome to the "Python for Data Science" course! In this introductory lesson, we will lay the foundation for using Python in your pharmacy and biotech applications. Python has emerged as a powerhouse in data science due to its simplicity, extensive libraries, and strong community support. For those coming from a quantitative background, Python offers a flexible and powerful environment for data manipulation, statistical analysis, machine learning, and visualization. We'll start by understanding the fundamental concepts of Python programming. Unlike some compiled languages, Python is an interpreted language, meaning code can be executed line by line, which is excellent for rapid prototyping and interactive data exploration. Its syntax is designed to be highly readable, often resembling natural language, making it relatively easy to pick up, even for those new to programming. At its core, Python deals with various data types. The most common ones you'll encounter include integers (whole numbers), floats (decimal numbers), strings (text), and booleans (True/False values). Understanding how to declare variables and assign these data types to them is the first step in writing meaningful Python code. Variables act as named containers for storing data, allowing you to reference and manipulate information throughout your programs.
Variables and Basic Data Types
Let's dive into some practical examples. In Python, you don't need to explicitly declare the type of a variable; the interpreter infers it based on the value assigned. This dynamic typing simplifies coding but also requires careful attention to the types of data you are working with. # Assigning an integer patient_id = 1001 print(f"Patient ID: {patient_id}, Type: {type(patient_id)}") # Assigning a float (e.g., drug concentration) drug_concentration_mg_per_ml = 0.55 print(f"Drug Concentration: {drug_concentration_mg_per_ml} mg/mL, Type: {type(drug_concentration_mg_per_ml)}") # Assigning a string (e.g., drug name) drug_name = "Paracetamol" print(f"Drug Name: {drug_name}, Type: {type(drug_name)}") # Assigning a boolean (e.g., clinical trial status) is_clinical_trial_active = True print(f"Clinical Trial Active: {is_clinical_trial_active}, Type: {type(is_clinical_trial_active)}") # Performing a simple arithmetic operation dosage_mg = 500 number_of_doses = 3 total_daily_intake_mg = dosage_mg * number_of_doses print(f"Total Daily Intake: {total_daily_intake_mg} mg") Beyond individual variables, Python offers powerful data structures to organize collections of data. Lists and dictionaries are two fundamental structures that will be invaluable in data science. A list is an ordered, mutable collection of items, meaning you can change its contents after creation. A dictionary , on the other hand, is an unordered collection of key-value pairs, perfect for storing data where each piece of information has a unique identifier. # Creating a list of patient ages patient_ages = [24, 35, 62, 48, 71] print(f"Patient Ages: {patient_ages}, Type: {type(patient_ages)}") print(f"First patient age: {patient_ages[0]}") # Accessing elements by index (0-based) # Adding a new age to the list patient_ages.append(55) print(f"Updated Patient Ages: {patient_ages}") # Creating a dictionary for a patient's medical record patient_record = { "patient_id": "P007", "diagnosis": "Hypertension", "medications": ["Lisinopril", "Hydrochlorothiazide"], "age": 68, "allergies": ["Penicillin"] } print(f"Patient Record: {patient_record}, Type: {type(patient_record)}") print(f"Patient Diagnosis: {patient_record['diagnosis']}") # Updating a value in the dictionary patient_record['age'] = 69 print(f"Updated Patient Age: {patient_record['age']}") These basic building blocks – variables, data types, lists, and dictionaries – will form the foundation for all subsequent data manipulation and analysis you perform in Python. Mastering them is crucial for effectively handling biological and pharmaceutical datasets.
Key Takeaways
Python is an interpreted, high-level programming language known for its readability and extensive libraries. Variables are used to store data, and Python uses dynamic typing. Fundamental data types include integers ( int ), floating-point numbers ( float ), strings ( str ), and booleans ( bool ). Lists ( list ) are ordered, mutable collections of items, accessed by index. Dictionaries ( dict ) are unordered collections of key-value pairs, excellent for structured data.
Practice Exercise
Imagine you are tracking inventory for a new biotech lab. Create Python code that defines a variable for the total number of petri dishes (an integer), a variable for the average cost per petri dish (a float), and a list of the names of three different cell lines currently in culture (strings). Then, create a dictionary that stores the details of one specific cell line, including its name, passage number (integer), and growth medium type (string).
Watch the full lesson — free
This topic is part of Python for Data Science, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →