Have been working with C# for a decade, it is time to learn a new language. Some options popping over my head, of course they must be different than C# – Java is excluded immediately, are Python, Ruby on Rails, Closure, … kind of scripting languages. I know some about JavaScript. So I definitely do not want to dig deeper into it.
I remember a statistic from StackOverflow 2018, Python has a good position. I also heard somewhere that Python is awesome. So here I come Python.
If one wishes to know what Python is, ask Google. It gives you all the awesome resources ranging from introduction to intermediate to advanced. The community is strong as well.
The post is my journey, my thoughts when I started to learn Python. It also serves as my documentation. If someone ask me how to get started to Python, I can give them the link. Well, at least that what I thought.
So let’s get started!
Environment
Each developer has their own favorite environments. I come from the .NET world so Visual Studio Code is my tool. Of course, I can use Visual Studio. But seems Visual Studio Code is a better choice.
- Visual Studio Code + Python Extension
- Install Python on Windows. Download and follow the instruction here at python org
Installing both Visual Studio Code and Python are fast. Everything is ready in a matter of minutes.
Quick Overview
Some notes about python:
- Python file extension is py Ex: hello-python.py.
- Can take a single Python file and run with python command line.
- Interactive shell is powerful. From the console/terminal/PowerShell, type the command python and the Python shell is ready.
- Dynamic type. The type is known at the execution time.
- Write "Hello World" program in a single line of code: print("Hello World") directly from the shell.
I can write functional code or object oriented code.
Everything in Python is an object. There are 2 functions that I find very useful – type() and id().
name = "Thai Anh Duc"
duplicatedname = "Thai Anh Duc"
# Want to know the type?
type(name)
# Want to know where it is store in the memory?
id(name)
# Are they equal? Are they the same?
name == duplicatedname
So name equals duplicatedname. But they are not the same. By using the id() function, we know that are store in different locations.
Materials
I learned from:
- Pluralsight Courses: It is always my first and default option when learning technologies.
- Tech Beamer: Very detail with examples, explanations.
Conventions
Python uses indentation for code block instead of square brackets ({}) in C#. There is a coding guidelines, standard PEP8. At the beginning, I do not worry too much about them. I just focus on some simple building blocks. And fortunately, the Visual Studio Code has helped me checked and suggested correction.
This block of code is enough for me to remember what are important
if(1 > 0):
print("Of course, it is.")
for index in [1, 2, 3, 4]:
print("hello {0}. This is a loop, display by string format".format(index))
print("Same code block")
def i_am_a_function(parameters):
# Comment what the function does
print(parameters)
Data Types
Date types are crucial to code, even with a dynamic type language. As a developer, you have to know what kind of data you are dealing with. Are we talking about a string, an integer, a big decimal, or a date, …
Refer to Tech Beamer for detail of each type. A special note is at the number types. There are 3 types:
- int – for integer value. There is no limit as far as the computer can hold it. Unlike C#, there are limit on each number type, such as Int16, Int32, Int64.
- float – for floating value. There is a limit of 15 decimal digits.
- complex – for other cases. For example, the operator 7/3, instead of returning the value 2.333333, it is represented as (2,1) – this is the complex type.
Because type is dynamic, sometimes it is a good idea to check the type first. Python has isinstance function
name = "Thai Anh Duc"
# It is a string
isinstance(name, str)
age = 36
# It is an int
isinstance(age, int)
bank_amount = 1.1
# It is a float
isinstance(bank_amount, float)
Function and Class
One might not need a class but function.
def append_lower(name):
# Lower case the name and then append " python" to the end
return name.lower() + " python"
class Python101():
# User-defined classes should be camel case
static_constant_field = "Static field, access at the class level"
def __init__(self, comment)
# Constructor
# :param self: this in C# language
# :param comment: constructor parameter. Which tells Python that it requires a parameter
self.comment = comment # Declare an instance field and assign value
def say_hi(self)
print(self.comment)
print(Python101.static_constant_field)
p101 = Python101("You are awesome")
p101.say_hi()
The code speaks for itself.
Summary
It is quite easy for a C# developer to get started with Python. With the above building blocks, I am ready to write some toy programs to play around with Python. There are many that I want to see how Python does. The initial list are:
- IO
- Network
- Threading
The list goes on as I learn. I am ready to go next.