Variable declaration and initialization in C# vs Python

Computer programming can be informally defined as follows: we provide the computer with some data and a set of instructions on how to process that data so as to accomplish the task we have in mind.

Lets take an example and write our first program in C# and Python to kick things off. Suppose we want our program to calculate the product of two integers. The data in this case will be the two integers and the instructions will be to multiply the given integers and display the result.

C# code to multiply two integers.

Python code to multiply two integers.

There is an astounding similarity between the C# code from line 7 to 10 and the Python code. Let us focus on the part of C# code that corresponds directly to the Python.

C# code.

Python code.

Variable declaration and initialization

The first two lines of code perform variable declaration and initialization. In plain words, they free up the required space in the computer’s memory for our data, initialize the memory with our integers 2 and 30 and assigns labels (num1 and num2) to those memory locations for future use. These labels (or variable names) can then be used in place of the integers when calculating the product.

Notice how the keyword int precedes the variable names in C#. This informs the C# compiler that the data-type of this variable is integer i.e. it will store integer data. Similarly, the keyword float can be used to declare floating-point variables with 7 digit precision and the keyword double can be used for 15 digit precision. Python on the other hand does not require us to explicitly declare the data-types of our variables and recognizes the data-type from the provided data. Floating-point numbers in Python have double precision by default.

C# code.

The small f  after 2.5 in float declaration is required to denote float value in C#. If we omit the small f , the value 2.5 becomes double precision.

The third statement multiplies the two integer variables and stores the product in another integer variable called  result. Note that the keyword  int is not required for variables num1 and num2 while calculating the product since they have already been declared above. The  Console.WriteLine() function (in C#) or  print() function (in Python) is then used to display the result.

Variable naming

You must abide by the following rules while naming variables in C# or Python:

  1. Variables names can contain letters, numbers and underscore character.
  2. Variable names must NOT begin with a number.
  3. Variable names are case sensitive i.e. num1 and Num1 are two different variable names.
  4. (Optional) Use sensible names for variables so that they clearly represent the stored data. For example, if you want to store the age of a person, you should name your variable  age instead of  xyz.

The Main() function in C#

In addition to what we have discussed, C# has these mysterious statements followed by parenthesis that encapsulate our entire code.

For now, just keep in mind that these are part of every C# program. When we run a C# program, the operating system looks for the Main() function to transfer the control of our computer to our program. The Main() function then contains the statements that instruct the computer to perform whatever task the program is supposed to perform. We will have a better understanding of this mandatory code in C# after we’ve discussed functions and classes.

What we learnt

  1. Data is stored in variables and variable names can then be used in place of data.
  2. Data-type of a variable must be explicitly declared in C# while it is not required in Python.
  3. The statements in C# terminate with a semi-colon.

< Introduction – C# vs Python               Arithmetic operators >

You may also like: