C# Variables, Comments & Data Types

Advertisements

Hello everyone,

Michael here, and in today’s post, we’ll explore C# variables, comments (notes that developers leave on code to explain it to other developers), and data types.

For many of the other programming tools we’ve covered on this blog’s journey like Python and Java, C# has its own ways of dealing with variables, comments, and data types.

In Python, here are some common variables, comments, and data types:

Variables

a = 3
b = 2

print(a+b)

5

Comments (single hashtag)

# This will print out the sum of 3+2

a = 3
b = 2

print(a+b)

5

Data types

complex
int 
str
bool 
dict

Likewise, in Java, here are some common variables, comments, and data types

Variables

String name = "Michael";
int year = 2024;

Comments

// This is a code comment
System.out.println("Hello world")

Data types

int
float
String
char

The variables, comments, and data types (C# version)

Now that we’ve reviewed variables, comments, and data types in Java and Python, let’s explore how these concepts work in C#.

Take this simple C# script, filled with variables, comments and data types:

// This code is writing a simple sentence with both string and int variables
int year = 2024;
string name = "Michael";
string month = "December";

Console.WriteLine(name + " is writing this post in " + month + " " + year);

Let’s examine this script line-by-line:

  • The top line with the double backslashes // signifies a comment being added to the script. Like Java and Python, C# comments are single-line deals.
  • int year = 2024; indicates that the int value in this script will be 2024
  • The two string variables in this script indicate that the two string values I will use in this script are Michael and December, respectively.
  • The Console.WriteLine() section of the script will print out my output to a command line-in this case, the string I want to output is Michael is writing this post in December 2024.
  • Another thing worth noting-to concatenate values in C#, use the + (plus) sign to do so. As you can see, I used the plus sign several times to concatenate my string values with other text in order to generate the following line of code: name + " is writing this post in " + month + " " + year.

Now that we’ve explored some basic examples of variables, data types, and comments in C#, what are some other data types in C#:

  • long-think of this as the 8-bit version of the int data type in C# (int is a 4-bit data type) that stores a broader range of integers
  • float-a 4-bit decimal type that stores values up to 6-7 decimal places
  • double-an 8-bit decimal type that stores values up to 15 decimal places
  • bool-the C# boolean type which stores true or false values
  • char-the C# character type which stores single characters
  • string-the C# string type which stores string values

Variables, Data Types, and Comments in C#: Things to Note

What are some important things to note about variables, data types, and comments in C# that are either similar to or perhaps quite different from variables, data types, and comments in other languages we’ve covered throughout this blog’s run?

  • In C#, comments are always indicated with the // (double backslash) symbol.
  • Just like in Java, you must specify a variable’s data type in the script (like I did with int year = 2024; for example).
  • Unlike Python, C# has no data types to handle complex/imaginary numbers (like 3+5i for example).
  • The boolean truth table rules that apply to languages like Java and Python also apply to C# (e.g. individual statements A & B must be true for a boolean statement to be true).

Also, here’s my script from this lesson on my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Python Lesson 2: Variables & Number Types in Python (50th Post)

Advertisements

Hello everybody,

It’s Michael, and today’s lesson will be on rudimentary coding with Python. Since most of the previous post focused on the background and setup process of Python, this lesson will focus on some basic coding with Python.

First, let’s open up Anaconda and launch our Spyder* console:

*By the way, Spyder stands for Scientific PYthon Development Environment. I don’t know why I forgot to mention this in my last post.

Now, let’s start with a simple Python program. Here’s the code:

x = 14
y = x * 2
z = “14-Doubled”
print(x, y, z)

And here’s the output:

14 28 14-Doubled

In this simple program, I demonstrated the concept of variables in Python. They work quite similarly to variables in Java, but with one notable difference-there is no need to declare variable type in Python. So writing something like int x = 14 would be unnecessary in Python (and I think doing so will generate an error).

Keep in mind that, even though syntax might be different, variable naming rules for Python are just like those for Java. First of all, variable names must start with either a letter or underscore; they can NEVER start with a number. Also, variables names can only contain letters, numbers, and underscores, so don’t include special characters like &, *, or %. Finally, remember that, like in Java, variable names in Python are also case sensitive, so walk, Walk, and WALK would be three different variables.

Also, take a look at the print function. Notice that I used commas to combine the variables, not plus signs (which is what I would use to print several variables on one line in Java). However, you would use a plus sign to combine text and a variable, like this (I used z from the example I just discussed):

print(“Twenty eight is ” + z)

Twenty eight is 14-Doubled

One cool thing about Python variables is that you can assign values to several variables on the same line. Here’s how (along with the program output):

a, b, c, d = “ant”, “bee”, “cat”, “dog”
print(a)
print(b)
print(c)
print(d)

And here’s the output:

ant
bee
cat
dog

First, you would simply write each of the variable names, separated by a comma. After you insert an equal sign, write the values of the variables separated by a comma. You only need the pair of quotation marks if the values are Strings, otherwise they aren’t necessary.

  • Keep in mind that the variable names and their corresponding values are listed in order. In other words, the first variable a corresponds to the first value ant, the second variable b corresponds to bee, and so on.

Another interesting thing you can do with Python variables is assigning the same value to several variables on the same line. Here’s how (I included a program along with sample output):

a = b = c = d = “The quick brown fox jumps over the lazy dog”
print(a)
print(b)
print(c)
print(d)

And here’s the output

The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

The idea is similar to that of the previous program (assigning values to variables in the same line). However, there is one major difference (aside from assigning a single value to multiple variables)-each variable name is separated with equals signs, not commas. Equals signs are used because you’re assigning a single value to four variables, thus, each variable would have the same value as the others.

  • By the way, anybody recognize the sentence I used in this example?

One more topic I wanted to discuss was number types in Python, which are different from number types in Java. First of all, there are only three number types in Python (int,float, and complex) as opposed to the six number types in Java (int, byte, long, short, double, and float).

The fact that there are only three number types, as opposed to six, makes Python simpler in this regard. After all, even though I like using Java, I find that the several different number types are redundant. After all, byte, long, and short are all integers with the only difference between each number type is their range of possible values. Likewise, float and double are both decimals, but the only major difference is the same as with the integers types-their range of possible values is different. In Python, int is the only possible number type for integers and float is the only possible number type for decimals.

  • Keep in mind that float can be either a simple decimal (e.g. -1.04, 21.335) or a number in scientific notation with an e to indicate a power of 10 (e.g. 5.3e6, 4.55e-1).

Now, you probably have never seen the complex number type before. After all, Java doesn’t have this number type.

Complex represents complex numbers, which are the combination of real and imaginary numbers. Some examples of complex numbers include 3+7i, 4-2i and 15+10i. If you see a pattern in all of these numbers, it’s because they are all written in the form a+bi, where a is the real number and bi is the imaginary number. However, numbers such as -3j, 5j, and 32j are also complex numbers since they have both a real and imaginary part.

  • If you’ve taken Algebra 1, you might remember the concept of imaginary numbers. But if you don’t recognize or remember the concept of imaginary numbers, click this link to learn more-Imaginary Numbers.
  • The i is replaced with a j in Python. You must use a j when writing any complex numbers because you will get an error if you try to use another letter, including i.

One cool thing about Python is that you can easily convert from one number type to another. However, the one exception to this rule is that you can’t convert complex numbers into float or int.

Here’s an example of Python number conversion (the sample program along with the output):

x = 2019
y = 8.07
z = 20j

print(x)
print(y)
print(z)

a = float(x)
b = int(y)
c = complex(x)

print(a)
print(b)
print(c)

Here’s the output:

2019
8.07
20j
2019.0
8
(2019+0j)

The top three lines include variables x, y, and z, which are of type int, float, and long, respectively; these variables are printed on the first three lines of the output.

Pay attention to the seventh through ninth lines of code, since they contain the number type conversions (these are variables a, b, and c). A will convert the integer x into a float. B will convert the decimal y into an int. C will convert the integer x into a complex number. The converted values are then displayed in the second three lines of the output.

  • If you noticed, I didn’t use z in any of the conversions. This is because z is a complex number, and remember that complex numbers can’t be converted into anything else.

Thanks for reading,

Michael