Lesson · 40 min · Free
Python Syntax
Python Syntax - The Complete Python Bootcamp 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: au
Python Syntax
Welcome to the "Python Syntax" lesson, a foundational step in your journey through "The Complete Python Bootcamp: Basics to Pharma & Data Science." Understanding Python's syntax is akin to learning the grammar of a new language. It defines the rules for how code must be written to be correctly interpreted by the Python interpreter. Unlike some other programming languages, Python is known for its readability and relatively straightforward syntax, which significantly reduces the learning curve and makes it an excellent choice for scientific and data-intensive applications. For pharmacy and biotech students, clarity and reproducibility in code are paramount. Python's clean syntax directly supports these goals, allowing you to focus more on the logic of your drug discovery algorithms, data analysis pipelines, or pharmacokinetic models, rather than wrestling with complex language constructs. This lesson will cover the core elements that define Python's structure, ensuring your code is both functional and easily understood by others (and your future self!).
Key Syntactic Elements
Python's syntax emphasizes readability through several key features:
Indentation
One of the most distinctive features of Python is its reliance on indentation to define code blocks. Unlike languages that use curly braces {} , Python uses whitespace (spaces or tabs) to indicate the scope of loops, conditional statements, functions, and classes. Consistent indentation is not just a style preference; it's a mandatory part of the syntax. Typically, four spaces are used for each level of indentation. Consider the following example demonstrating indentation within an if statement: temperature = 37.5 if temperature > 37.0: print("Patient has a fever.") print("Consider administering antipyretic medication.") else: print("Patient temperature is normal.") print("Monitoring patient vitals.") In this example, the two print statements under the if block are executed only if temperature > 37.0 is true, because they are indented. The final print statement is outside the if-else construct and will always execute.
Comments
Comments are crucial for documenting your code, explaining complex logic, and making your programs understandable. In Python, a single-line comment starts with a hash symbol ( # ). Anything after the # on that line is ignored by the interpreter. Multi-line comments are typically achieved using triple quotes ( ''' or """ ), though these are technically multi-line string literals that are not assigned to a variable and thus act as comments. # This is a single-line comment explaining the purpose of the variable drug_dosage_mg = 500 # Dosage in milligrams ''' This is a multi-line comment. It can span several lines to provide more detailed explanations, for instance, about a complex calculation or algorithm for drug interaction. ''' patient_weight_kg = 70 calculated_dose_ml = (drug_dosage_mg / patient_weight_kg) * 0.1 # Simple example calculation
Variables and Data Types
Python is dynamically typed, meaning you don't need to declare the type of a variable explicitly. The interpreter infers the type at runtime. Variables are created the moment you assign a value to them. Variable names must start with a letter or an underscore, and can contain alphanumeric characters and underscores. They are case-sensitive. Common data types you'll encounter include integers ( int ), floating-point numbers ( float ), strings ( str ), and booleans ( bool ).
Statements and Expressions
A statement is an instruction that the Python interpreter can execute, such as assigning a value to a variable (e.g., x = 10 ) or calling a function (e.g., print("Hello") ). An expression is a combination of values, variables, operators, and function calls that evaluates to a single value (e.g., 10 + 5 evaluates to 15 , or "Hello" + " World" evaluates to "Hello World" ).
Keywords
Python has a set of reserved words, known as keywords, that have special meanings and cannot be used as variable names, function names, or any other identifier. Examples include if , else , for , while , def , class , import , return , True , False , and None . You will learn about these as we introduce different Python constructs.
Line Continuation
While Python encourages short, readable lines, sometimes a statement can become very long. You can break a statement into multiple lines using a backslash ( \ ) at the end of the line. However, Python implicitly handles line continuation within parentheses () , brackets [] , and curly braces {} , which is often a cleaner approach. Indentation: Defines code blocks (e.g., if , for , def ). Typically 4 spaces. Comments: Use # for single-line comments; triple quotes '''...''' for multi-line documentation. Dynamic Typing: Python infers variable types; no explicit declaration needed. Keywords: Reserved words with special meaning (e.g., if , else , def ). Readability: Python's syntax is designed to be clear and concise, reducing cognitive load.
Practice Exercise
Write a short Python script that defines two variables: drug_name (a string) and concentration_mg_per_ml (a float). Then, using an if-else statement and proper indentation, check if the concentration_mg_per_ml is greater than 100.0. If it is, print a message indicating it's a "High concentration formulation" along with the drug name. Otherwise, print "Standard or low concentration formulation" and the drug name. Add comments to explain your variable definitions and the conditional logic.
Watch the full lesson — free
This topic is part of The Complete Python Bootcamp: Basics to Pharma & Data Science, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →