Variables, Assignment, and Number Types
A variable is a name that refers to a value so you can use it again later. You create one by giving it a value. Python is dynamically typed: a variable…
Variables
A variable is a name that refers to a value so you can use it again later. You create one by giving it a value.Python is dynamically typed: a variable name can point to a value of any type, and unlike Java or C there is no need to declare the variable first.
Naming variables
A variable name can use letters, digits, and the underscore symbol, but it cannot start with a digit. It is considered best practice to use meaningful names.The equals sign is not equality
In math, = states that two things are equal. In Python it is an assignment: evaluate the expression on the right side, then store that result in the variable on the left.That is why a line which looks impossible in math is perfectly ordinary in Python.
Basic built-in types
Every value in Python has a type. These are the three number-like types. Text has its own type, str, which is coming up next.| Type | Example | Description |
|---|---|---|
| int | x = 1 | Integers, that is whole numbers |
| float | x = 1.0 | Floating-point numbers, that is real numbers |
| bool | x = True | Boolean: True or False values |
Integers and floats
The most basic numerical type is the integer. Any number written without a decimal point is an integer. Python integers are variable-precision, so you can do computations that would overflow in other languages.The floating-point type stores fractional numbers. Writing a decimal point is what makes a value a float, even when the value is a whole number: 4.0 is a float, 4 is an int.
Booleans
The Boolean type is a simple type with two possible values: True and False. Boolean values are case-sensitive, so unlike some other languages, True and False must be capitalized.Comparison operators produce a Boolean value.
| Expression | Value |
|---|---|
4 < 5 | True |
3 >= 5 | False |
3 != 5 | True |
3 == 5 | False |