Python Variables šŸŽÆ

beginner
7 min

Python Variables šŸŽÆ

Welcome to our comprehensive guide on Python Variables! In this lesson, we'll dive deep into understanding variables in Python, their types, and how to use them effectively. Let's get started!

What are Variables? šŸ“

Variables are containers used to store data in a program. In Python, variables don't require explicit declarations like in other languages. You can create a variable and assign a value to it simply by giving it a name.

python
my_variable = "Hello, World!" print(my_variable)

āœ… Output: Hello, World!

Variable Naming Rules šŸ’”

  1. Variable names should not contain spaces.
  2. Variable names can only contain letters, numbers, and underscores.
  3. Variable names are case-sensitive (my_variable and my_variable are considered different variables).
  4. Python is auto-case sensitive, so it's a good practice to use lowercase letters and underscores (_) for readability.

Variable Types šŸ“

Python has three types of variables:

  1. Immutable Types (cannot be changed once assigned):

    • Integers (e.g., 5)
    • Floating-point numbers (e.g., 3.14)
    • Strings (e.g., "Python")
    • Tuples (e.g., (1, 2, 3))
  2. Mutable Types (can be changed once assigned):

    • Lists (e.g., [1, 2, 3])
    • Dictionaries (e.g., {"name": "John", "age": 25})

Assignment Operators šŸ’”

Python has several assignment operators that allow you to assign values to variables in a more efficient way:

  1. =: Simple assignment
  2. +=: Augmented assignment (addition)
  3. -=: Augmented assignment (subtraction)
  4. *=: Augmented assignment (multiplication)
  5. /=: Augmented assignment (division)
  6. //=: Augmented assignment (floor division)
  7. %=: Augmented assignment (modulo)
  8. **=: Augmented assignment (exponentiation)

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the output of the following code?


We've covered the basics of variables in Python, now let's practice using them in practical scenarios. In the next lesson, we'll dive into data structures like lists and dictionaries.

Stay tuned and happy coding! šŸš€