Variables and data types
Basic data types
This chapter will introduce you to the basics of Python variables and data types.
The following four data types are the most basic in Python:
Data type | Description | Example |
---|---|---|
| Integer |
|
| Floating-point number |
|
| String |
|
| Boolean |
|
Function print()
is a built-in function in Python that is used to print the specified message to the screen. The following examples show how to use the print()
function.
To show the data type of a variable, use the type()
function.
Cast a variable to a different data type using the following functions:
Variables
Variables are used to store data values. A variable is created when a value is assigned to it.
Operators
Arithmetic operators
Operators are used to perform operations on variables and values. The following operators are available in Python:
Operator | Description | Example |
---|---|---|
| Addition |
|
| Subtraction |
|
| multiplication |
|
| Division |
|
| Modulus |
|
| Exponentiation |
|
| Floor Division |
|
Assignment operators
Assignment operators are used to assign values to variables. The following assignment operators are available in Python:
Operator | Example | Equivalent |
---|---|---|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparison operators
Comparison operators are used to compare two values. The following comparison operators are available in Python:
Operator | Description | Example |
---|---|---|
| Equal |
|
| Not equal |
|
| Greater than |
|
| Less than |
|
| Greater than or equal to |
|
| Less than or equal to |
|
Logical operators
Logical operators are used to combine conditional statements. The following logical operators are available in Python:
Operator | Description | Example |
---|---|---|
| Returns |
|
| Returns |
|
| Reverse the result |
|
Comments
Comments are used to explain the code and make it more readable. Comments start with a #
and Python will ignore them.
A multi-line comment can be created using triple quotes.
Last updated