py-103 Variables in python

py-103 Variables in python
Variables in Python

Programming is all about writing & giving instructions to a computer. But before you write those instructions, you need a way to store and manipulate data. This is where variables, data types, and operators come in.


In Python, variables act as named storage containers for data. They hold information the computer can process during your program's execution. Each variable has a specific data type, which defines the kind of data it can store. To use a variable, we must first declare it by giving it a name and assigning a value to it.

Programming fundamentals

Declare a variable

Declare a variable
Declare a variable

In the above example, we have declared a variable named "age" and assigned it a value of 25. We can then use this variable in our program to perform operations or make decisions based on its value.

To see the value of the variable in the console use the print() function.


If you are searching for a classroom or certification course here are some recommendations

  1. Python for Everybody [Getting started with python]
  2. Datacamp's Interactive Python course.
  3. Codecademy Interactive Python course.
  4. Google-certified Python Course Crash Course in Python.
  5. Python for Data Science, AI & Development by IBM.

Redeclare a variable

You can change the value of a variable anytime after it is declared once.

age="What is your age"
Redeclare a variable

Delete a variable

You can delete a variable by using the “del” keyword. After the variable is deleted the program throws a error “age is not defined” because we have deleted the viarable age.

del age
Delete a variable

Assignment Operators in Python

In Python, assignment operators perform two key tasks:

  1. Assign a value: They assign a value (often obtained from an expression) to a variable.
  2. Combine assignment and operation: They can combine an operation with the assignment, updating the value of the variable in the process.

Examples of assignment operators:

Simple Assignment:

  • =

Combined Assignment Operators:

  • += (Addition)
  • = (Subtraction)
  • = (Multiplication)
  • /= (Division)
  • //= (Floor Division)
  • %= (Modulo)
  • *= (Exponentiation)
  • <<= (Left Shift)
  • >>= (Right Shift)
  • &= (Bitwise AND)
  • |= (Bitwise OR)
  • ^= (Bitwise XOR)

Naming a variable in Python

In Python, choosing names for variables, functions, and other identifiers has one important rule: you cannot use words that are already reserved by the language itself. These reserved words have specific meanings and cannot be used for your custom elements.

There are a total of 35 reserved keywords in Python 3.12.

You can find the names of these keywords by using the help function

Keywords in python 3.12

Variable Naming conventions in python

While Python doesn't strictly enforce naming conventions, following these guidelines improves code readability, maintainability, and collaboration:

1. Lowercase with underscores (snake_case):

  • This is the preferred convention for naming variables, functions, and other identifiers in Python.
  • Examples: my_variable, user_name, function_name.

2. Use meaningful names:

  • Choose names that clearly describe the purpose or content of the variable, making the code self-explanatory.
  • Avoid using single-letter names like a, b, except for temporary variables in loops or short calculations.

3. Avoid reserved keywords:

  • Python has a set of reserved keywords with specific meanings in the language. These cannot be used as variable names.
  • You can find a list of reserved keywords using the keyword module:

4. Case sensitivity:

  • Python is case-sensitive, so age and Age are considered different variables.

5. Avoid special characters:

  • Stick to using letters, numbers, and underscores. Avoid using special characters like @, $, %, etc., as they can make code less readable and might have unexpected interpretations in certain contexts.

6. Class names:

  • Use PascalCase (uppercase first letter for each word) for class names.
  • Example: MyClassName.

7. Constant names:

  • Use all uppercase letters with underscores for constant names.
  • Examples: PI, MAX_VALUE, FILE_PATH.

8. Consistency is key:

  • Maintain consistency in your naming style throughout your codebase. This makes it easier for you and others to understand and modify the code.

Following these conventions fosters a clean and professional coding style, making your Python projects more readable and maintainable.