There is a quote that I’ve always really liked:

If you can’t explain it simply, you don’t understand it well enough.

Quote often attributed to Albert Einstain, however I’ve not found any evidence he actually said it.

In this blog post, I’m going to see how well I understand Python by explaining the way if-statements, while loops and for loops work. I’m also going to compare some of the differences between C# and Python when using these functions.

General Differences

In the following examples, the user is asked to enter a number. The number is printed followed by the string ‘different number’.

Console.WriteLine("Enter 1, 2, or 3");
int num = Convert.ToInt32(Console.ReadLine());
string numString = "Different Number";

num = int(input("Enter 1, 2 or 3"))
num = "Different Number"
No More Semicolons (;)

For a lot of programming languages, a semi-colon at the end of each line is mandatory and C# is no exception to this rule. For Python, a semicolon is optional. It won’t break if you include them, but it won’t break if you leave them either. Leaving them out is an easy mistake to make, so its nice to not have to worry about them anymore.

Variable Types

In Python, we do not have to declare the variable type. This allows the variable types to change later in the program. In the example above, the variable ‘num’ was originally an integer, but it then changed to a string. In the C# code, a separate string had to be declared.

I’m not sure what I think about this. In my mind, a variable should have 1 purpose only. Declaring the variable as a specific type means that purpose is defined at the earliest opportunity. Personally, I prefer declaring a new variable rather than reusing existing ones – even if they are no longer required. Reusing variables for different purposes can make the code messy.

If Statements

If statements are functions used to ensure that certain code is only run if a condition has been met. In both Python and C#, the function starts with the word if, which is then followed by a conditional statement. If the conditional statement if true, the proceeding block of code is run.

The following code will ask the user to enter 1, 2 or 3. The output varies for each number. If none of these numbers are entered then an error message is printed.

//C# If Statement
Console.WriteLine("Enter 1, 2 or 3");
int num = Convert.ToInt32(Console.ReadLine());
if(num == 1)
	Console.WriteLine("You entered 1");
else if(num == 2)
	Console.WriteLine("You entered 2");
	Console.WriteLine("Well done");
else if(num == 3)
	Console.WriteLine("You entered 3");
	Console.WriteLine("Error Message");

#Python If Statement
num = int(input("Please enter 1, 2, or 3: "))
if num == 1:
    print("You have entered 1")
elif num == 2:
    print("You have entered 2")
    print("Well done")
elif num == 3:
    print("You have entered 3")
    print("Error Message")

Fewer Brackets, Forced Indents

This is my main criticism of Python – the lack of brackets. Why did they decide to stop using brackets? I love brackets. They clearly define the start and end point of a block of code.

In C#, when multiple lines of code have to be run, then the code needs to be inside a pair of curly brackets {}. If there is only a single line of code, then the curly brackets are optional.

In Python, indents are used instead. All indented lines of code that proceed the if statement are run if the statement is true. The block of code ends when a line of code is not indented. This is the case regardless of the number of lines of code that need to be run.

In C#, indents are used to make the code look cleaner and tidier. The program will still run the same way if they were not included. It is good practice to include them therefore indents should be used even if not mandatory. I prefer the use of brackets to mark out the block of code included in an if-statement, but indents are also recommended for tidiness.

Punctuation at the end of statement

In Python, brackets in the if statements are optional so there still needs to be a way to define the end of the statement. Instead of brackets, a colon : is used at the end of the statement.

Brackets or a colon it doesn’t matter which is used. Both offer a clarity so I have no issues with the change in syntax here. However, my most common python programming mistake has been forgetting to include a colon at the end of an if-statements. I’m sure I’ll get used to this when I’ve gained more Python programming experience.

Lazy Else If

‘Else if’ is an additional function that can be included after an if statement. The block of code that follows the ‘else if’ statement is run if the conditions are true, but only if the first if statement is false. There is no limit to the number of ‘else if’ statements that can be included. Once an if or else-if statement is true, then the remaining statements are not run.

In Python, the term ‘else if’ has been reduced to ‘elif’. This brings me to my next criticism of Python. Was it really necessary to reduce the term to a single word? In a lot of cases, fewer words can mean less complicated and easier to read code. However, in this case, its just made the code more confusing. ‘Else If’ are 2 word that clearly explain what happens the code is run. ‘Elif’ doesn’t really mean anything.

The second most common mistake I’m making when writing Python code is forgetting to type ‘elif’ instead of ‘else if’.

While Loops

While loops, like if-statements, are functions that indicate a block of code that is only run if the conditional statement is true. The different here is that this block of code is run repeatedly until the condition is no longer true.

While loops take a similar format for if-statements. The main difference being the user of the word ‘while’ instead of ‘if’. The issues that I’ve stated with if-statements also apply to while loops.

The following examples will ask the user to type in a number. If the number is less than 50 the user will be asked to type in another number. The user will continue to be prompted to enter a number until the sum of all numbers entered is greater than 50.

//C# While Loop
int num = 0;
while (num <= 50)
	Console.WriteLine("Please enter a number"); 
	num = num + Convert.ToInt32(Console.ReadLine());
	Console.WriteLine("New total = " + num.ToString());
Console.WriteLine("Total has reached 50");

#Python While Loop
num = 0
while num <=50:
    num = num + int(input("Please enter a number: "))
    print("New total = " + str(num))
print("Total has reached 50")

For Loops

A ‘for loop’ is used where a block of code needs to be run a set number of times. Unlike while loops, we know how many times the code will be run.

The following examples will print out all even numbers between 0 and 100.

//C# For Loop
for (int i = 0; i < 100; i = i+2)

#Python For Loop
for i in range(0,100,2):

In Python, the setup of a for loop is similar to if statements and while loops. The statement ends with a colon : and the block of code that needs to be run is indented instead of being contained in brackets like they are in C#.

General differences

The function setup in Python is a lot more straight forward than it is for C#. The use of the word ‘range’ makes it a lot more understandable to someone with less programming knowledge.

For both, an iteration number variable needs to be declared. This should be an integer, but for Python the variable type doesn’t need to be declared.

Range differences

In Python, 3 numbers need to be defined for the range function:

  • A starting number
  • An ending number
  • An amount the iteration number changes by after each iteration is run
    (This is optional in Python, and is set to 1 if not defined)

In C#, three statements are declared, separated by a semi colon:

  • A counter is declared as an integer and assigned a starting number
  • A conditional statement that defines when the for loop stops running
  • A statement that defines what change is made to the integer variable at the end of each iteration.

One issues I’ve found with Python, which doesn’t apply for C#, is that it is unclear if the range includes the starting and ending number of not. The starting number is always included, but the ending number is not. In C#, by using a condition (i<100 or i<=100), it is clear at what point the code stops running.

In the examples above, the number 100 will not be printed unless the following changes are made:

  • In C#, condition is changed from ‘i is less than 100’ to ‘i is less than or equal to 100’ (‘i<100’ to ‘i<=100’)
  • In Python, the end number needs to be increased to 101.

Final Thoughts…

I’ve found Python an easy language to learn, although I still need more practice to get fully used to the different syntax. It was once suggested to me that Python is a good starting point for someone who is new to programming. I can see why this would be the case. Python code is much simpler and less cluttered than C#, which also makes it easier to read.

I will be publishing more blog posts about my experiences while learning Python. Click here to see some of my blog posts that have already been published.

I’m currently working my way through the 150 challenges from Python By Example by Nichola Lacey. You can see the work I’ve done so far on my GitHub repository: