Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • Asp.net Core
    • C
    • C#
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Python
  4. Understanding Variables in Python: A Complete Guide with Examples

Understanding Variables in Python: A Complete Guide with Examples

Date- Dec 09,2023

Updated Mar 2026

3127

python variables

C# Variable

A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times. It is a way to represent memory location through symbol so that it can be easily identified. The basic variable type available in C# can be categorized as:

  • int - stores integers (whole numbers), without decimals, such as 123 or -123
  • double - stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • string - stores text, such as "Hello World". String values are surrounded by double quotes
  • bool - stores values with two states: true or false

Declaring (Creating) Variables

Syntax for variable definition in C# is -

<data_type> <variable_list>;

Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.

Some valid variable definitions are shown here -



int i, j, k;
char c, ch;
float f, marks;
double d;

You can initialize a variable at the time of definition as -



int i = 100;

Above, int is a data type, i is a variable name (identifier). The = operator is used to assign a value to a variable. The right side of the = operator is a value that will be assigned to left side variable. Above, 100 is assigned to a variable i.

Initializing Variables

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is -





variable_name = value;



Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as โˆ’


<data_type> <variable_name> = value;





A demonstration of how to declare variables of other types:





int myNum = 5;
double 
myDoubleNum = 5.99D;

char myLetter = 'D';

bool myBool = true;

string myText = "Hello";



Rules for defining variables

  • A variable can have alphabets, digits and underscore.
  • A variable name can start with alphabet and underscore only. It can't start with digit.
  • No white space is allowed within variable name.
  • A variable name must not be any reserved word or keyword e.g. char, float etc.

Example



using System;

public class MyApplication
{
	public static void Main()
	{
		int i = 200;
		int j = i + 20; 
		Console.WriteLine("j = {0}", j);
		
		i = 300;
		j = i + 20; 
		Console.WriteLine("j = {0}", j);
		
		i = 400;
		Console.WriteLine("j = {0}", j);  

	}
}



OUTPUT

j = 220
j = 320
j = 320

In the above example, value of j depends on the value of i. You must re-execute expression each time you change the value of i; otherwise, value of j would not change based on the value of i.

Valid variable names:



int y;      
int _y;      
int x20;      

Invalid variable names:



int 6;      
int a b;      
int float;      

Multiple Variables in a Single Line



using System;
					
public class MyApplication
{
	public static void Main()
	{
		int i = 0, j = 10, k = 200;
		
		Console.WriteLine(j);
	}
}



OUTPUT

10

C# Identifiers

All C# variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Note: It is recommended to use descriptive names in order to create understandable and maintainable code:



using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      // Good
      int age = 30;

      // OK, but not so easy to understand what a actually is
      int a = 30;
      
      Console.WriteLine(age);
      Console.WriteLine(a);
    }
  }
}


OUTPUT
30
30

Note: C# is the strongly typed language. It means you can assign a value of the specified data type. You cannot assign an integer value to string type or vice-versa.

There are few example which create error by this declaration of variable



int num = "Hello";  // Cannot assign string to int type variable

int num;
num = 200;            //Variables can be declared first and initialized later.

int i;
int j = i;  //compile-time error: Use of unassigned local variable 'i'

S
Shubham Saini
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Mastering While Loops in C#: A Complete Guide with Examples
Dec 09, 2023
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Mastering Decision-Making Statements in Python: A Complete Guide
Dec 09, 2023
Understanding Call By Value in C#: A Complete Guide with Examples
Dec 09, 2023
Previous in Python
Break and Continue Statements Explained in Python with Examples
Next in Python
Mastering Decision-Making Statements in Python: A Complete Guide

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your Python interview with curated Q&As for all levels.

View Python Interview Q&As

More in Python

  • Realtime face detection aon web cam in Python using OpenCV 7365 views
  • Break and Continue Statements Explained in Python with Examp… 3064 views
  • Real-Time Model Deployment with TensorFlow Serving: A Compre… 31 views
  • Leveraging AI for SEO Optimization in Python 30 views
  • Mastering NumPy for Data Science: A Comprehensive Guide 29 views
View all Python posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor