After attended the event and workshops organized by the CodingGirls, the interests to learn on Python, R, Tableau and SAS zoomed into my life. One of my colleagues gave some insights of what skills I should learn in order to move into data analytics field. My colleagues and myself intended to pick up the programming language through the Udemy’s online courses. We wanted to take advantage of the Black Friday’s Sales.
For a start, I did the python 3 installation on my machine. For Window, it is straight forward with using the .exe file, I just have to follow the steps and complete the installation without hassle. To make sure I have installed the python 3, I can easily check by opening the command prompt and type,
Let’s get started.
Regardless which website you start with, make sure complete the whole course to get the understanding of Python. Some online learning websites provide interactive python code editor where you can write the codes and run it from the website, while some others will teach you how to install applications to do it.
Eventually, we will write our codes and run it from an application. Choose one which you are comfortable to begin with. For my case, I am going to work with IntelliJ which is freeware and Jupyter’s Notebook online version.
Alright, back to my first lesson. I did a small, quick start with trying my hand on using the print statements. There are syntax and functionality difference between Python 2 and 3, the fundamentals are all the same. Let’s begin with a baby step.
While double-quotes (“) and single-quotes (‘) are both acceptable ways to define a string or a text. A string needs to be opened and closed by the same type of quote mark. Text in Python is considered a data type of string which it can contain letters, numbers and symbols. We can concatenate (combine) the texts using +.
We can use triple quotes (“””) for a string to span multiple lines and assign it to a variable. One of the examples I learned,
haiku = “””The old pond,
A frog jumps in:
Plop!, we expected: The old pond,
A frog jumps in:
Another usage of triple quotes (“””) as docstrings. Docstrings describe what the function does. It serves as a documentation for the function and it is placed immediately after the function’s header.
def square (value):
"""Return a value of the square"""
new_value = value ** 2
This looks pretty easy to start off, right?
Next, the errorhandling while we running the code, the editor shows the SyntaxError to tell us where it goes wrong. Example, this error is due missing the quotation marks.
Then, I moved deeper into using the variables. When my colleagues built web applications, they constantly dealt with changing of data. It found it irritating when I saw the source code hard-coded with data. It will turn out to be inconvenient if we need to constantly change the texts or data we coded into our script. Python uses variables to define things that are subject to change. Each variable that you derive can be used to store texts, numbers or dates.
Similarly to writing SQL scripts, wherever possible, I will use variables to define values subjected to change, in a way, we can dynamically use our script.
- Specific and case-sensitive name, best practice to use lowercase.
- Define things that are subject to change.
- Can be used to store texts, numbers or dates.
- Cannot start with number.
- Cannot use space and symbols in the name, use _ instead.
This is how we define the variable and assign the value. You can see how it assigns date, string of text and number.
Python uses dynamic typing. What does it mean?
It means we can reassign variables to different data types. It makes python easily assigning data types, this different than other programming languages that are statically-typed. Other than python, do you know what other programming languages have similar characteristic?
So, what do Python calls for these different types of values?
- Boolean operations: and, or, not (true, false)
- Numeric types: int, float, complex (number, decimal)
- Text sequence type: str (string)
- Sequence type: list, tuple, range
- Mapping type: dict
- Sets type: set
It looks a little complex now but do not worry about them. We will use them quite often later. Let us look at some samples of how to derive variables in Python with different data type. More data type can be found from the Internet.
Now, we can look into using arithmetic operations with variables. The variable will be used to hold the final result of each operation. Arithmetic operations follow the precedence of the operators. Detail of the precedence can be found from the Internet.
Summary of the day:
- installation of python 3.
- print() statement.
- single quote, double quotes, triple quotes.
- how does SyntaxError look like?
- how to define and use variable.
- built-in types.
- basic arithmetic operators.