Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
Variables in Python
Primitive Types:
Representing a single vale
1. Integer
Integers represent positive or negative whole numbers, from negative infinity to infinity. Note that integers should not include decimal points. Integers have the type `int`.
```
Current_Year = 2020
type(Current_Year)
Output: int
```
2. Float
Floats (or floating-point numbers) are numbers with a decimal point. There are no limits on the value or the number of digits before or after the decimal point. Floating-point numbers have the type `float`.
3. Boolean
Booleans represent one of 2 values: True and False. Booleans have the type bool.
Booleans are automatically converted to ints when used in arithmetic operations. True is converted to 1 and False is converted to 0.
Ex: 3. + True = 4.0
Any value in Python can be converted to a Boolean using the bool function.
Only the following values evaluate to False (they are often called falsy values):
The value False itself
The integer 0
The float 0.0
The empty value None
The empty text ""
The empty list []
The empty tuple ()
The empty dictionary {}
The empty set set()
The empty range range(0)
Everything else evaluates to True (a value that evaluates to True is often called a truthy value).
bool(False) = False, bool(0) = False, bool(0.0) = False, bool(None) = False, bool("") = False, bool([]) = False,
bool(()) = False, bool({}) = False, bool(set()) = False, bool(range(0)) = False
bool(True), bool(1), bool(2.0), bool("hello"), bool([1,2]), bool((2,3)), bool(range(10))
(True, True, True, True, True, True, True)
4. String
5. None
Containers:
Holds multiple pieces of data together
- List
- Tuple
- Dictionary
# Integer Variable Example
Current_Year = 2020
type(Current_Year)
int