codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

5 / 68 topics
1Python Introduction & Features2Python Get Started & VirtualEnv3Python Syntax & First Program4Python Comments & Keywords5Python Variables & Constants6Python Basic Input and Output
Tutorials/Python Programming/Python Variables & Constants
🐍Python Programming

Python Variables & Constants

Updated 2026-05-15
15 min read

Python Variables & Constants

In this tutorial, you'll learn about variables and constants in Python. Understanding how to declare and use variables is fundamental to programming with Python. Variables allow you to store data that can be manipulated and used throughout your program. Constants, on the other hand, are values that remain unchanged once set.

Variables in Python are dynamically typed, meaning you don't need to explicitly declare their type when you create them. This flexibility makes Python easy to use for beginners but also requires careful attention to ensure your code behaves as expected.

Creating Variables

In Python, you can create a variable by simply assigning a value to it using the = operator. Here's a basic example:

create_variable.py
1x = 10
2print(x)

When you run this code, the output will be:

Output

In this example, the values 1, 2, and 3 are assigned to the variables a, b, and c, respectively.

You can also use multiple assignment to swap the values of two variables without needing a temporary variable:

swap_variables.py
1x = 5
2y = 10
3
4# Swap values using multiple assignment
5x, y = y, x
6
7print(x)
8print(y)

The output will be:

Output
10
5

This is a common technique in Python to swap the values of two variables.

Constants

Constants in Python are not a built-in feature like in some other programming languages. However, by convention, constants are represented using uppercase letters with underscores separating words (similar to snake_case). This convention helps indicate that the value should not be changed once it's set. For example:

constants.py
1PI = 3.14159
2GRAVITY = 9.81

While Python does not enforce immutability for constants, following this convention is a good practice to prevent accidental modification of these values.

Practical Example

Let's create a simple program that calculates the area and circumference of a circle using variables and constants:

circle_calculations.py
1# Constants
2PI = 3.14159
3
4# Variables
5radius = float(input("Enter the radius of the circle: "))
6
7# Calculations
8area = PI * radius ** 2
9circumference = 2 * PI * radius
10
11# Output results
12print(f"The area of the circle is {area:.2f}")
13print(f"The circumference of the circle is {circumference:.2f}")

When you run this program, it will prompt you to enter the radius of a circle and then calculate and display the area and circumference.

Summary

  • Variables in Python are created by assigning values using the = operator.
  • Use snake_case for variable names to improve readability.
  • Multiple assignment allows you to assign multiple variables in one line.
  • Constants are represented using uppercase letters with underscores, but they are not enforced as immutable.

What's Next?

In the next tutorial, you'll learn about basic input and output operations in Python. Understanding how to interact with users is essential for creating functional programs. Make sure to practice what you've learned about variables and constants by experimenting with different values and calculations.


PreviousPython Comments & KeywordsNext Python Basic Input and Output

Recommended Gear

Python Comments & KeywordsPython Basic Input and Output