BP101T • Basics of Python Programming for Pharmaceutical Sciences

Python variables and data types

TOPIC 01

Python Variables Overview

Simple Definition:

A Python Variable is defined as a named storage container in computer memory used to store data values that can be manipulated and changed during program execution.

  • • It is also known as: A memory label or identifier for data.
  • • It is used for: Storing patient details, drug dosages, laboratory test values, and experimental calculations in healthcare applications.
  • • Data Types are defined as: The classification of data values that tell Python what type of operations can be performed on them (eg. int, float, str, bool, list, dict).

Python Variables are containers used to store data values. You can create a variable using the assignment operator '='.

Example:
age = 25
name = "Sara"
print(age)
print(name)

Read the symbol '=' as "is set to".
age = 25 means "age is set to 25".

Once created, use the name anywhere in the code.

For example:
print(age)# which gives output 25
print(name)# gives output Sara
Output:
25
Sara
✓
Key Takeaway• Quick Recap

Variables act as dynamic memory labels for values created using the '=' assignment operator without explicit type declaration.

TOPIC 02

Rules for Variable Names & Identifiers

In Python, variable names must follow strict syntax rules (or they will cause errors) and standard naming conventions:

✓
Letters, numbers, underscores only:Valid examples: my_age, age2
✕
Can't start with a number:2age ❌ (Syntax Error)
✕
No spaces:my age ❌ (Use my_age instead)
!
Case matters:Python is case-sensitive: Age and age are completely different variables.

💡 You don't declare the type. Python figures it out from the value you give it.

✓
Key Takeaway• Quick Recap

Variable names must start with a letter or underscore, cannot begin with a number, and cannot use reserved Python keywords.

TOPIC 03

Python Data Types & Type Casting

Type casting means converting a value from one type to another. In type casting the type name is used as a function.

The type casting is simply used by putting the type name e.g. int() in front of the value.

Example:
print(int("25")) # 25
print(float("3.5")) # 3.5
print(str(25)) # "25"
print(bool(0)) # False

Common Type Casting Functions

FunctionWhat it doesExampleOutput/Result
int()Convert to whole numberint(3.9)3
float()Convert to decimalfloat(5)5.0
str()Convert to textstr(42)"42"
bool()Convert to True/Falsebool(1)True

Types of type casting:

Python has two types of type casting:

Type 1

Implicit Type Casting

Python converts it automatically without any user intervention.

Type 2

Explicit Type Casting

You need to convert it manually using functions like int(), float(), str().

✓
Key Takeaway• Quick Recap

Core data types include int, float, str, and bool. Explicit casting is done using built-in constructors like int(), float(), and str().

TOPIC 04

Basic Python Operators Reference

Python operators are special symbols used to perform operations on variables and values.

1. Arithmetic Operators

Used to perform standard mathematical calculations.

OperatorNameExampleResult/Output
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
//Floor Division5 // 22 (removes decimal points)
%Modulus (Remainder)5 % 21
**Exponentiation2 ** 38 (23)

2. Comparison Operators

Used to compare two values. They always return a Boolean (True or False).

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal to5 >= 5True
<=Less than equal to3 <= 5True

3. Logical Operators

Used to combine conditional statements.

OperatorDescriptionExampleResult
andReturns True if both statements are true(5 > 3) and (10 > 8)True
orReturns True if at least one statement is true(5 > 3) or (2 > 8)True
notReverses the result (turns True to False, etc.)not (5 > 3)False

4. Assignment Operators

Used to assign and update values stored inside Python variables.

OperatorNameExampleEquivalent To
=Assigns valuex = 5x = 5
+=Adds & assignsx += 3x = x + 3
-=Subtracts & assignsx -= 2x = x - 2
*=Multiplies & assignsx *= 2x = x * 2
/=Divides & assignsx /= 2x = x / 2
Python Basic Operators Visual Cheat Sheet - Arithmetic, Comparison, Logical and Assignment Operators
Figure 1.1: Complete reference map for Python arithmetic, comparison, logical, and assignment operators.
✓
Key Takeaway• Quick Recap

Arithmetic operators perform math calculations, comparison operators evaluate conditions to booleans, and assignment operators modify variables.

TOPIC 05

Input and Output Operations

It is the method to take input from the user and can be used in variables, it doesn’t matter what kind of input it is, the input method allows us to take any type of value.

The input is always taken as string by python even if user provided integer value it will be treated as string data type in python unless we use typing casting feature to specifically define type of the data we are going to provide.

Python Input and Output Operations Visual Diagram
Figure 1.2: Python input and output operations cheat sheet.
for ex:
name = input("Enter your name: ")
print("Welcome,", name)
output:
Enter your name: Rahul
Welcome, Rahul
Type Casting User Input Example:
age = int(input("Enter your age: "))
next_year = age + 1
print("Next year, you will be", next_year)
output:
Enter your age: 19
Next year, you will be 20
✓
Key Takeaway• Quick Recap

input() always captures data as string text and requires explicit casting (e.g. int(input())) to perform numerical operations.

TOPIC 06

The Python print() Output Operation

The print() function in Python is the standard output method used to display text, numbers, variables, or calculation results on the screen.

Ex:
print("Hello, World!")
name = "Rahul"
print(name)
output:
Hello, World!
Rahul
✓
Key Takeaway• Quick Recap

print() is the standard output mechanism, displaying variables, text, and computed results to the screen with custom separators and line endings.

✓

Unit 1 Quick Exam Recap & Key Takeaways

Variables & Identifiers

  • Variables are dynamically typed references to memory objects.
  • Identifier names must start with a letter (a-z, A-Z) or underscore (_); never a digit.
  • Reserved keywords (like if, def, class) cannot be used as variable names.

Data Types & Operators

  • Core primitive types: int, float, str, bool.
  • Floor division (//) rounds down; Modulo (%) returns remainder; Exponent (**) calculates power.
  • Comparison operators always return boolean True or False.

Input Operation Rules

  • input() always captures data as string text.
  • Perform explicit casting with int() for whole numbers and float() for decimals.
  • Always validate or strip user input in production software.

Output Best Practices

  • Prefer f-strings (f"{var}") for clear, fast text interpolation.
  • Use sep to define custom delimiters between comma-separated arguments.
  • Use end="" to prevent automatic newline breaks when streaming terminal output.