Python variables and data types
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 '='.
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.
print(age)# which gives output 25
print(name)# gives output SaraSara
Variables act as dynamic memory labels for values created using the '=' assignment operator without explicit type declaration.
Rules for Variable Names & Identifiers
In Python, variable names must follow strict syntax rules (or they will cause errors) and standard naming conventions:
my_age, age2Age and age are completely different variables.💡 You don't declare the type. Python figures it out from the value you give it.
Variable names must start with a letter or underscore, cannot begin with a number, and cannot use reserved Python keywords.
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.
print(int("25")) # 25
print(float("3.5")) # 3.5
print(str(25)) # "25"
print(bool(0)) # FalseCommon Type Casting Functions
| Function | What it does | Example | Output/Result |
|---|---|---|---|
| int() | Convert to whole number | int(3.9) | 3 |
| float() | Convert to decimal | float(5) | 5.0 |
| str() | Convert to text | str(42) | "42" |
| bool() | Convert to True/False | bool(1) | True |
Types of type casting:
Python has two types of type casting:
Implicit Type Casting
Python converts it automatically without any user intervention.
Explicit Type Casting
You need to convert it manually using functions like int(), float(), str().
Core data types include int, float, str, and bool. Explicit casting is done using built-in constructors like int(), float(), and str().
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.
| Operator | Name | Example | Result/Output |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 5 / 2 | 2.5 |
| // | Floor Division | 5 // 2 | 2 (removes decimal points) |
| % | Modulus (Remainder) | 5 % 2 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 (23) |
2. Comparison Operators
Used to compare two values. They always return a Boolean (True or False).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 5 > 3 | True |
| < | Less than | 5 < 3 | False |
| >= | Greater than or equal to | 5 >= 5 | True |
| <= | Less than equal to | 3 <= 5 | True |
3. Logical Operators
Used to combine conditional statements.
| Operator | Description | Example | Result |
|---|---|---|---|
| and | Returns True if both statements are true | (5 > 3) and (10 > 8) | True |
| or | Returns True if at least one statement is true | (5 > 3) or (2 > 8) | True |
| not | Reverses the result (turns True to False, etc.) | not (5 > 3) | False |
4. Assignment Operators
Used to assign and update values stored inside Python variables.
| Operator | Name | Example | Equivalent To |
|---|---|---|---|
| = | Assigns value | x = 5 | x = 5 |
| += | Adds & assigns | x += 3 | x = x + 3 |
| -= | Subtracts & assigns | x -= 2 | x = x - 2 |
| *= | Multiplies & assigns | x *= 2 | x = x * 2 |
| /= | Divides & assigns | x /= 2 | x = x / 2 |

Arithmetic operators perform math calculations, comparison operators evaluate conditions to booleans, and assignment operators modify variables.
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.

name = input("Enter your name: ")
print("Welcome,", name)Welcome, Rahul
age = int(input("Enter your age: "))
next_year = age + 1
print("Next year, you will be", next_year)Next year, you will be 20
input() always captures data as string text and requires explicit casting (e.g. int(input())) to perform numerical operations.
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.
print("Hello, World!")
name = "Rahul"
print(name)Rahul
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
TrueorFalse.
Input Operation Rules
input()always captures data as string text.- Perform explicit casting with
int()for whole numbers andfloat()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
septo define custom delimiters between comma-separated arguments. - Use
end=""to prevent automatic newline breaks when streaming terminal output.