Variables and Reserved Words in Python

Variables and Reserved Words in Python

Variables and reserved words are fundamental concepts in Python programming. Understanding how to name, use, and work with variables, as well as recognizing reserved words, is crucial for writing clear and effective Python code.

Variables in Python

In Python, a variable is a symbolic name that references or points to a value. This value can be of various data types, such as integers, strings, lists, or any other type. Variables allow you to store data that can be used and manipulated throughout your program.

Assigning Values to Variables

Assigning a value to a variable in Python is straightforward. You simply use the assignment operator (=).

# Assigning values to variables
x = 10
y = "Hello, World!"
z = [1, 2, 3]

In the example above, x is assigned the integer value 10, y is assigned a string, and z is assigned a list.

Rules for Naming Variables

When naming variables in Python, you need to follow certain rules:

  • Start with a Letter or Underscore: A variable name must begin with a letter (a–z, A–Z) or an underscore (_).
  • No Spaces: Variable names cannot contain spaces. Use underscores (_) to separate words if needed (e.g., my_variable).
  • Case Sensitivity: Variable names are case-sensitive, meaning myVar and myvar are different variables.
  • No Reserved Words: Variable names cannot be Python reserved words.

Examples of Valid and Invalid Variable Names

Examples of Variable Names
Valid Variable Names Invalid Variable Names
my_variable 2nd_variable (cannot start with a number)
myVar123 my-variable (hyphens are not allowed)
_privateVar class (reserved word)

Reserved Words in Python

Reserved words (or keywords) in Python are special words that have specific meanings and are part of the syntax. These words cannot be used as identifiers (e.g., variable names) because they are reserved for their intended purpose in the language.

List of Python Reserved Words

Here’s a list of some common reserved words in Python:

Python Reserved Words
Reserved Word Purpose
and Logical operator for boolean operations
class Defines a class
def Defines a function
if Conditional statement
for Looping statement
while Looping statement
return Returns a value from a function
try Begins a block of code that will test for exceptions
except Catches and handles exceptions
lambda Creates an anonymous function
đŸ’¡

Note on IDE Formatting

Integrated Development Environments (IDEs) such as PyCharm, VS Code, and others including Code Runner and Virtual Programming Lab (VPL) typically highlight keywords, identifiers, and variables using different colours and styles. This visual distinction helps in identifying reserved words, variables, and functions easily. It’s a good practice to get accustomed to the styling provided by your IDE as it can help you quickly identify issues in your code, such as using a reserved word as a variable name or missing a keyword in a statement.

Key Takeaway

Understanding variables and reserved words is fundamental to programming in Python. By following the rules for naming variables and recognising reserved words, you can write code that is not only syntactically correct but also easy to read and maintain.