1.2 Data Types and Variables
Most values in a computer program have a type. The type tells Python what the value represents and what operations make sense for it.
Common Python types include:
- Numeric:
int,float,complex - Text:
str - Boolean:
bool - Containers:
list,tuple,set,dict
Variables
A variable is a name used to store or refer to data. The value that a variable points to has a type.
Use type() to inspect a value’s type.
num = 10
print(type(num)) # <class 'int'>
salary = 8232.56
print(type(salary)) # <class 'float'>Here, num refers to an integer, so its type is int. salary refers to a decimal number, so its type is float.
Naming variables
Good variable names are clear and valid:
- Use letters, digits, and underscores, but do not start with a digit.
- Do not use Python reserved keywords.
- Prefer meaningful English words.
Names like age, student_name, and total_score are easier to read than a or x1.
Python keywords
Keywords are reserved by Python and cannot be used as ordinary variable names.
Common keywords include:
False,None,Trueand,or,notif,elif,elsefor,while,break,continuedef,return,classtry,except,finallyimport,from,as
Using a keyword as a variable name would make Python unable to tell whether you mean a language feature or an ordinary value.