Analytics Vidhya
Published in

Analytics Vidhya

Day 1: Let Get Started with Python

I started learning Python programming language in 2018 after a friend of mine shared about the learning material that I can get from Udemy’s online learning website. One online course costs about SGD16.00 for the cheapest offer.

HOW TO BEGIN?

I installed Python 3 in my machine running on Windows operating system. The installation is quite simple, using the .exe file, and just following through the wizard. No hassle. Also, I tried to install Python 3 in another machine running on Ubuntu operating system because I wanted to try installing it through the terminal (command prompt).

CHECK VERSION

After the installation of python 3, I open the terminal (in Linux) or command prompt (Windows) to go into the Python’s shell by typing the following command:

python

I can see a return message from Python with the version number from the terminal or command prompt screen. There are Python 2 and Python 3. So, be clear on which version is being used on the machine because the syntax is slightly different from each other.

python --version

Simply open up your terminal or command line to type the above command on it. On the screen, it may return you the version info such as below:

Python 3.7.0

WHICH EDITOR TO USE?

I chose the IDE (integrated development environment) that I comfortable with. I liked to use Jupyter Notebook. I can use the online version or the installed version on my machine. In the Python: How to code in 5 minutes post, I wrote about how I installed Python, Anaconda and used the Jupyter Notebook to write my first Python codes after 2 years. Before I edited this post, I used to code Python using IntelliJ because I installed it when I learned how to code in Scala.

PRINT(‘HELLO WORLD’)

This is every programmer’s first step when learning a new language. For Python, it has version 2 and 3, the fundamentals are almost the same, but some syntax has to take note of. 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 that can contain letters, numbers and symbols. We can concatenate (combine) the texts using +.

print('Hello World!') print("Hello World!")

The above screenshot took from IntelliJ. When I wrote the codes on the left panel, the output displayed on the right panel.

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:
Plop!”””

Another usage of triple quotes (“””) as docstrings. Docstrings describe what the function does. It serves as documentation for the function, and it is placed immediately after the function’s header. For example,

def square (value): """Return a value of the square""" new_value = value ** 2 return(new_value)

This looks pretty easy to start off, right?

Error handling

I do not think it is the right time to talk into error handling and read the syntax error at this beginning stage. However, I would like to share how a syntax error can happen to be alert and careful when writing the codes. Most of the editor shows the SyntaxError to tell us where it goes wrong. Example, this error is due to missing the quotation marks.

I will not touch the error handling topic now, and let me continue with the basic programming.

VARIABLES AND TYPES

Then, I moved deeper into using the variables. When my colleague built web applications, they constantly dealt with changing data. I 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.

Variables

  • 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 a number.
  • Cannot use space and symbols in the name, use _ instead.
height = 1.67 weight = 180 name = 'Joanne' gender = 'Female' isStudent = True

The above shows the height and weight variables in float and int data type, then we have name and gender in a string and a variable called isStudent with a Boolean value. In Python, it does not require to declare a variable with any prefix in front of or behind the variable which we can see in Javascript or SQL Server, if you are familiar with those languages. Then, you may ask how does compiler (computer) know it is of what type of data types.

This is how we define the variable and assign the value. You can see how it assigns a date, a string of text and number.

What is the difference between (=) and (==)?

The single equal (=) assigns the value on the right to a variable on the left. The double equal (==) tests if the two things have the same value. The two things can be two variables or arithmetic operation to compare a variable.

Dynamic Typing

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, which this different from other programming languages that are statically-typed. Other than python, do you know what other programming languages have similar characteristic?

Types

So, what does Python call for these different types of values? Built-in Types:

  • Boolean operations: and, or, not (True, False). It is case sensitive
  • Numeric types: int, float, complex (number, decimal)
  • Text sequence type: str (string)
  • Sequence type: list, tuple, range
  • Mapping type: dict
  • Sets type: set
  • None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. It is a null value or no value at all which is different than an empty string, 0 or False.

It looks a little complex now but does 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 a different data type. More data type can be found on the Internet.

type()

type(height) type(weight) type(name) type(isStudent)

type() is a built-in function which allows us to check the data types of the variables we created with assigned values. type() helps to answer the above question.

That completed the fundamental and basic to code in Python. Now, you know how to do the following:

  • use the print() statement to print texts.
  • use of variables and data types.
  • use the type() statement to print out the data type of a variable.

Probably, now you want to know what is an integer, string, Boolean and etc. I have some links here to help out the basic explanation together with examples:

To talk about numbers and strings, it can be another topic on its own as there are many interesting about them such as the use of (+) sign. It is a concatenate sign which means it combines two or more variables of the same type together. The way number and string use (+) sign also different than each other. Also, we have to remember that in Python, string and integer cannot use of (+) sign together. It throws an exception (error). An exception is a programming jargon means error. There is a topic of exception handling in Python too. In this case, there is string formatting and integer formatting.

BASIC ARITHMETIC OPERATIONS

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 on the Internet.

Python follows BODMAS Rule

BODMAS is an acronym and it stands for Bracket, Order, Division, Multiplication, Addition, and Subtraction. In certain regions, PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, and Subtraction) is used when processing your operations.

The picture is extracted from https://byjus.com/maths/bodmas-rule/

I would like to cover a little a bit advanced basic in the entry before I conclude the day one sharing. It is the Python collections, it is something that I used quite often for data analytics or data science programming.

Python Collections

It is an interesting topic and an important part of Python. Almost everyone of us will use Python List in our daily coding life 🙂 It is a collection of values and allows us to have different types within the elements, one of the simplest and easiest collections. When it comes to the word “collection”, Python has four types of collections.

You can read more about the basic of these collections here. Each of them has different characteristics, syntax, structure and usage. Along the way, we use different collections to explain Python codes and concepts. Below is an example of how a list looks like:

fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']

Declaring a list is the same as declare a variable, it just requires following the list’s syntax to create one. As mentioned earlier, it can be any data types in a list. So, you can declare a list as below too:

family = ['Anna', 1.73, 'Eddie', 1.68, 'Mother', 1.71, 'Father', 1.89]

The lists above work with control flows, going through the iteration and/or condition checking, then calculate a value and return a result. More details will cover in another post, and I will update the link of the posts.

Up to now, this portion is still basic Python and does not involve any analytics or data science work if you are looking for one. If you wish to check out my previous write-ups, please visit this link. Hope you enjoy my sharing, please stay tuned for the next updates. Thank you.

Originally published at http://liyenz.wordpress.com on November 25, 2018.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Django?? It’s not Music, it’s all Web!!

EXCEL Advanced functions and formulas (PART-II)

Why Planning an App is important

Carrot: The Sadistic iPhone App That’ll Kick Your Life Into Shape

Data Ingestion Optimization in Apache Druid

ZKSwap daily briefing for April 30th, see you in May!

IOS Objective C UI WebView Target

Buffer overflow with cloudme 1.4.1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
LiYen Yoong

LiYen Yoong

Data Lover, Inspired Data Engineer

More from Medium

Object Oriented Concepts in Python

Linux Loops:

How to Convert Py to Exe using PyInstaller | Python

The superuser in Django