Python vs C#

I have read the article https://www.red-gate.com/simple-talk/development/dotnet-development/10-reasons-python-better-than-c-sharp

I think that deserves an answer –  so I take all points to answer to them. You can replicate the code in C# 10 / .NET 6 with a globalusings.cs with the following content:


global using static System.Console;
global using static System.Linq.Enumerable;
global using static System.IO.Directory;
global using static System.String;
global using static System.Math;

So let’s start:

Point 1 - Indented code blocks

//Point 1 - Indented code blocks
//With curly braces I can indent whatever way I want - I am not constrained

using System.Collections.Generic;

foreach (var i in Range(1,11))
{
if (i % 3 == 0)
WriteLine("fizz");
else if (i % 5 == 0)
WriteLine("buzz");

}

Also,when I want to comment something,with C# I know when the else if is
terminating without counting tabs. Just see where the { is closing with }

Point 2 -  Self-declaring Variables

Seriously? What is the difference between Python
//meaning_of_life = 42
and C# ? Just a var ?

//Point 2 - Self-declaring Variables
var meaning_of_life = 42;
Point 3 -– Those modules

It is the same code as Python

foreach (var file in EnumerateFiles(@"C:\")){
 WriteLine(file);
}
Point 4 – Simplicity of Data Structures

Quoting : Purists will say that you need all the different data types that C# provides,but trust me,you don’t.
I want to think about how to modelate a CallCenter waiting for phones – Stack or Queue ?
I want to think about how to  modelate a CallCenter waiting for phones with business consumer that has priority ?https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue

Point 5 - Slicing

Same code in C#

var sins = new[]{ "pride","envy","gluttony","greed","lust","sloth","wrath" };
//var selected_sins = sins[1..(sins.Length-1)];
var selected_sins = sins[1..^1];
WriteLine(Join(" ",selected_sins));
Point 6 – For loops

Same syntax in C#

foreach (var i in Range(0,5))
{
WriteLine(i);
}
// words in a string
string[] words = { "Simple","Talk" };
foreach (string word in words)
{
WriteLine(word);
}

Quoting : “(unlike in C#,where sometimes you use for and sometimes foreach)”
Please enumerate an array backwards in python without reversing

7 – List comprehensions

This is similar code in C#

var nr = Range(1,11).Where(it => it % 2 == 0).Select(it => Pow(it,3));
WriteLine(Join(" ",nr));
8 – Sets

Same example

//this contains some duplicates
var languages = new[] { "C#","Python","VB","Java","C#","Java","C#" };
//more dense than Python
languages = new HashSet<string>(languages).ToArray();
//this will give: ['C#','Java','Python','VB']
WriteLine(Join(" ",languages));

Second example

//create two sets: Friends characters and large Antarctic ice shelves
var friends = new[] { "Rachel","Phoebe","Chandler","Joey","Monica","Ross" };
var ice_shelves = new[] { "Ronnie-Filchner","Ross","McMurdo" };
// show the intersection (elements in both lists)
WriteLine(Join(" ",friends.Intersect(ice_shelves)));
//show the union (elements in either list)
WriteLine(Join(" ",friends.Union(ice_shelves)));
// show the friends who aren't ice shelves
WriteLine(Join(" ",friends.Except(ice_shelves)));
// elements in either set but not both -- more difficult
WriteLine(Join(" ",friends.Union(ice_shelves).Except(friends.Intersect(ice_shelves))));
9 – Working with files and folders
//# list of 3 people
var people = new[] { "Shadrach","Meshach","Abednego" };
//# write them to a file
File.WriteAllLines(@"C:\all\a.txt",people);
10 – The quality of online help

See https://docs.microsoft.com/ – good tutorials


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *