Variables and Assignment
Following our exploration of Your First Python Script: "Hello, World!" (Part 2), this article introduces a fundamental concept in any programming language: variables. We'll learn how to create and use variables in Python, and the best practices for naming them.
📚 Prerequisites
A basic understanding of Python syntax and how to run a Python script.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ What variables are and why they are useful.
- ✅ How to create variables and assign values to them.
- ✅ Python's dynamic typing.
- ✅ Best practices for naming variables (PEP 8).
🧠 Section 1: What are Variables?
Think of a variable as a labeled box where you can store information. You can put different things in the box, and you can change the contents of the box whenever you want. In programming, variables are used to store data that can be used and manipulated throughout your program.
💻 Section 2: Creating Variables and Assigning Values
In Python, you create a variable by assigning a value to it using the assignment operator (=).
# Creating variables
name = "Alice"
age = 30
is_student = True
In this example, we've created three variables: name, age, and is_student. We've assigned the string "Alice" to name, the integer 30 to age, and the boolean True to is_student.
You can then use the print() function to see the values of these variables:
print(name)
print(age)
print(is_student)
🛠️ Section 3: Dynamic Typing
Python is a dynamically typed language. This means you don't have to explicitly declare the data type of a variable. Python automatically infers the type based on the value you assign. You can even change the type of a variable by assigning a new value of a different type.
x = 10
print(type(x)) # Output: <class 'int'>
x = "Hello"
print(type(x)) # Output: <class 'str'>
🔬 Section 4: Naming Conventions (PEP 8)
To make your code readable and consistent, it's important to follow Python's naming conventions, as outlined in PEP 8 (Python Enhancement Proposal 8).
- Variable names should be lowercase, with words separated by underscores (snake_case).
user_name(good)userName(bad)
- Variable names must start with a letter or an underscore.
- Variable names can only contain letters, numbers, and underscores.
- Variable names are case-sensitive. (
nameandNameare different variables). - Avoid using Python keywords (e.g.,
if,else,for) as variable names.
💡 Conclusion & Key Takeaways
You've now learned how to create and use variables in Python, a crucial skill for any programmer.
Let's summarize the key takeaways:
- Variables: Labeled containers for storing data.
- Assignment Operator (
=): Used to assign values to variables. - Dynamic Typing: Python automatically infers the data type of a variable.
- PEP 8: The official style guide for Python code, which includes naming conventions.
Challenge Yourself: Create a script that uses variables to store your name, age, and favorite color, and then prints them out in a sentence.
➡️ Next Steps
In the next article, we'll dive deeper into Python's fundamental data types, starting with "Fundamental Data Types: Numbers (Integers, Floating-Point)".
Happy coding!
Glossary (Python Terms)
- Variable: A named storage location in a computer's memory.
- Assignment Operator: The
=symbol, used to assign a value to a variable. - Dynamic Typing: A type system where the type of a variable is determined at runtime.
- PEP 8: Python Enhancement Proposal 8, the official style guide for Python code.