Variables and data types
A variable is a name that refers to a value in memory. You create it with =, which means “store”, not “equals”.
PYTHON
name = "Sara" age = 21 gpa = 4.5 is_mis = True print(name, age, gpa, is_mis) print(type(age), type(gpa), type(name), type(is_mis))
| whole number | int → 21, -5, 0 |
| decimal number | float → 4.5, 0.15 |
| text | str → "Sara", 'IT' |
| true / false | bool → True, False |
Sometimes a value is text but you need a number, like data read from a file. Convert with int(), float() and str().
PYTHON
price = "4800" qty = 3 total = int(price) * qty print(total) print("Total: " + str(total))
Watch outVariable names use letters, digits and
_, and can't start with a digit. Python convention is lowercase with underscores, like total_price.