Variables in Python

Introduction to Python

Variables and Data Types:

In Python, variables are used to store data. They can hold different types of data such as integers, floats, strings, lists, etc.

Example:

age = 25
name = "Alice"

Declaration and Assignment:

In Python, you can declare a variable and assign it a value using the equal sign =.

x = 10
pi = 3.14
name = "Alice"

Variable types are automatically detected by Python from the assigned value.

Data Types:

  • Int (integers): x = 10
  • Float (floats): pi = 3.14
  • Str (strings): name = "Alice"
  • Bool (booleans): is_student = True
  • List (lists): grades = [10, 20, 15]

Quiz: Test Your Knowledge on Variables

1. What is the correct syntax to declare an integer variable in Python?

2. How do you declare a string variable in Python?

3. Which data type is used to store true or false values?

4. What is the difference between declaring a variable and assigning it a value?

5. What is the result of the following operation in Python: `age = 10 + 5 * 2`?

6. How do you declare a list variable in Python?

7. What is the correct syntax to check if two variables are equal in Python?