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 '='.
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
Rules for names:
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.
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.
int("25") # "25" → 25
float("3.5") # "3.5" → 3.5
str(25) # 25 → "25"
bool(0) # 0 → FalseTry it Yourself »Common 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().
Basic operators:
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 |
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.
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()).
# Taking string input
username = input("Enter your name: ")
# Taking numeric input with explicit casting
user_age = int(input("Enter your age: "))Try it Yourself »