BP101T • Basics of Python Programming for Pharmaceutical Sciences

Python variables and data types

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"
Try it Yourself »

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
Try it Yourself »

Rules for names:

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.


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:
int("25")      # "25" → 25
float("3.5")   # "3.5" → 3.5
str(25)        # 25 → "25"
bool(0)        # 0 → False
Try it Yourself »

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().


Basic operators:

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

Input and output operations:

In Python, Input and Output (I/O) operations allow your programs to communicate with the user via the console terminal:

  • Output sends data from the program to the screen using the print() function.
  • Input receives data from the user through the keyboard using the input() function.

Output: The print() Function

The print() function displays text, numbers, or the results of expressions on your screen.

Example:
Name = "Rahul"
print(Name)
Output:
Rahul
Try it Yourself »

Input: The input() Function:

The input() function pauses the program, waits for the user to type something in the terminal, and presses Enter.

Note: input() always returns data as a string (str), even if the user types a number. If you need to do math with it, you must explicitly cast it (e.g., using int() or float()).

Example:
# Taking string input
username = input("Enter your name: ")

# Taking numeric input with explicit casting
user_age = int(input("Enter your age: "))
Try it Yourself »