Why do I love Python / Generating Random Strings with C# and Python

Hi all,

Today I will talk about why I love Python. First of all, let me say that C# is the primary language that I am mostly developing. The main reason is that C# and .Net framework made parallelization easier than any other programming language; allowing me to write my OR algorithms (course projects, personal research etc.) in parallel with minimum effort. Also, coming with C# 5, asynchronism will become much easier.

Now, let us come to what makes me love Python:) Python, mostly I use for small scripts for the problems that I face in real life. Python command line is pinned to my start menu and I even use it as a calculator most of the times.

I needed a random string generator for a business project. Here are the C# and Python codes for that:

Random _rng = new Random();
string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char[] buffer = new char[20];
for (int i = 0; i < _chars.Length; i++)
buffer[i] = _chars[_rng.Next(_chars.Length)];

import random
alphabet = 'ABCDEFGHIJKLMNPRSTVUYZ123456789'
newkey = random.sample(alphabet, 20)

Well, an important thing to remember is that the C# code must be wrapped into some class brackets and the main function. On the other hand, the Python code would run as is, even in the Python command line. 7+ lines vs 3 lines; good right?

Two more code examples, both does not belong to me: The first one is summing all numbers divisible by 3 or 5 from 1 to 1000.
sum([x for x in range(1000) if x % 3== 0 or x % 5== 0]) 

And the last example today is calculating 99999999999999999999^9999:


Simple, simple, simple
That's all Python is about.

Keep reading and take good care

ps: The result for the last one comes up in around 17 seconds:)

1 yorum:

Turan Murat Güvenç dedi ki...

Nice stuff. I can see why you love Python now :) You're gonna have to teach me this language eventually...

Yorum Gönderme