Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  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 3150
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
Buy me a pizza

Comments

On this page

๐ŸŽฏ

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 7437 views
  • Break and Continue Statements Explained in Python with Examp… 3084 views
  • Deep Dive into Modules and Packages in Python: Structure and… 60 views
  • FastAPI Tutorial: Building Modern APIs with Python for High … 58 views
  • Mastering Web Scraping with Python and BeautifulSoup: A Comp… 54 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 | 1770
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
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • 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