Understanding Variables
- Variables are containers for storing data.
- They have a name and a value.
- Variables allow you to access data easily throughout your code.
Declaring and Initializing Variables
- To declare a variable, simply assign it a value:
my_variable = "Hello"
- This assigns the string "Hello" to the variable
my_variable
.
Python Data Types
- Variables can store different types of data:
- Strings (text):
my_string = "Example"
- Integers (whole numbers):
my_integer = 10
- Floats (decimal numbers):
my_float = 3.14
- Booleans (True/False values):
my_boolean = True
Variable Naming Rules
- Variable names must start with a letter or underscore.
- They can only contain letters, numbers, and underscores.
- They cannot be reserved keywords (such as
if
, else
, for
).
Simple Python Example
# Declare a string variable
my_name = "John Doe"
# Declare an integer variable
my_age = 30
# Print the values of the variables
print("My name is:", my_name)
print("My age is:", my_age)
Output:
My name is: John Doe
My age is: 30