Variables in Python are used to store data values that can be used in a program. In Python, variables are created when you assign a value to them using the `=` operator. Here is an example:
python # Assign a value to a variable x = 5 # Print the value of the variable print(x)
In this example, the variable `x` is assigned the value `5`. The `print()` function is then used to output the value of `x` to the console.
Python supports several data types, including:
– Integers: Whole numbers, such as 5, -10, and 0.
– Floats: Decimal numbers, such as 3.14, -2.5, and 0.0.
– Strings: Textual data, such as “hello”, “world”, and “42”.
– Booleans: Logical values that can be either `True` or `False`.
Here is an example of using variables with different data types:
python # Assign values to variables of different data types x = 5 y = 3.14 z = "hello" w = True # Print the values of the variables print(x) print(y) print(z) print(w)
In this example, four variables are created, each with a different data type. The `print()` function is then used to output the values of the variables to the console.
Python also supports more complex data types such as lists, tuples, and dictionaries, which are used to store collections of values. These data types are useful for storing and manipulating data in more complex programs.