Sunday, June 18, 2017

Summer of code 2017: Python, Day 1


As explained in my Summer of code 2017: Python post I decided to pick up Python

Today is officially day 1... but I really started yesterday

I decided to start with the PluralSight course: Python Fundamentals

This course is presented by  Austin Bingham and Robert Smallshire

This is the description for that course

Python Fundamentals gets you started with Python, a dynamic language popular for web development, big data, science, and scripting. What’s so great about Python? Python is powerful. The Python language is expressive and productive, it comes with a great standard library, and it’s the center of a huge universe of wonderful third-party libraries. With Python you can build everything from simple scripts to complex applications, you can do it quickly, and you can do it with fewer lines of code than you might think possible. But for many people those reasons take back-seat to something more important: Python is fun! Python’s readable style, quick edit-and-run development cycle, and “batteries included” philosophy mean that you can sit down and enjoy writing code rather than fighting compilers and thorny syntax. As your experiments become prototypes and your prototypes become products, Python makes the experience of writing software not just easier but truly enjoyable. In the words of Randall Munroe, "Come join us! Programming is fun again!"

Long integers
Python supports really long integers, most languages I worked with support up to 64 bit integers

In Python you can store 2 to the power of 900 in an integer without a problem

That looks like this in scientific notation

8.452712498170644e+270

Or if you were to print this out as an integer

8452712498170643941637436558664265704301557216577944354047371344426782440907597751590676094202515006314790319892114058862117560952042968596008623655407033230534186943984081346699704282822823056848387726531379014466368452684024987821414350380272583623832617294363807973376

That is 271 digits

Here is what it looks like in the console

>>> x = math.pow(2,900)
>>> print ( int(x))
84527124981706439416374365586642657043015572
16577944354047371344426782440907597751590676
09420251500631479031989211405886211756095204
29685960086236554070332305341869439840813466
99704282822823056848387726531379014466368452
68402498782141435038027258362383261729436380
7973376
>>> print ( len(str(x)))
22
>>> print ( len(str(int(x))))
271
>>> 




Python Enhancement Proposal (PEP)

Python's development is conducted largely through the Python Enhancement Proposal (PEP) process. The PEP process is the primary mechanism for proposing major new features, for collecting community input on an issue, and for documenting the design decisions that have gone into Python.

And index with all the PEPs can be found here: https://www.python.org/dev/peps/

PEP number 8 Style Guide for Python Code is an important one, all Python developers should keep that in mind. In this PEP you will see that you should use spaces not tabs for indentation, You should use 4 spaces per indentation level.


Zen of Python

This is another PEP, the Zen of Python is PEP number 20

The Zen of Python is a collection of 20 software principles that influences the design of Python Programming Language, 19 of these are written down, you can see them by entering the statement import this in the Python interpreter


>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

That output is the Zen of Python


None

The equivalent of NULL in languages like c#, Java or SQL Server in Python is None

If you assign the None value to a variable and then tried to print it, you won't get anything back

You can however check if a variable has no value by checking for None


>>> y = None
>>> y

>>> 
if y == None:
    print("is none")
else:
    print("is none")
    
is none


I also messed around and struggled to install PyGame, at the end it was pretty simple, you can read that here:  Installing Pygame on windows



No comments: