It has been a while I stopped learning Python from DataCamp due to my part time classes and assignment, and work commitment. It is not easy to keep track each of them everyday. On top of it, I still have my volunteer work with TechLadies and regularly have to meet up to brainstorm and updates each other.
Today’s topic is very much on Python, definitely. I want to concentrate on my writing in Python for next 2 weeks before I head off for an holiday. I am sure, I will be lazy after my break. It would be great if I can write up something to summarize or reorganize what I have been writing for the past few months on my Python’s learning using DataCamp and Udemy.
Remember my very first day I started learning Python using Udemy, it taught about the installation and I went on to install IntelliJ. Till to date, I hardly using it, most of my time, I am using the online version of Jupyter Notebook. I find it pretty easy to be used. I understood that there are many other IDEs in the market and there is no specific software to be used to code Python. For now, I will just keep it simple for my learning.
How to begin?
After the installation of the python 3, I open the terminal (in Linux) or command prompt (Windows) to go into the Python’s shell by typing the following command:
From the terminal or command prompt screen, I can see a return message from Python with the version number. There are Python 2 and Python 3. So, be clear on which version is being used on the machine because the syntax are slightly different from each others.
On the very first time, we always want to know if everything we installed for Python works or not. Checking the version, if it is updated, latest and correct version to be used is first time we might want to do with:
Simple 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:
Next, we always start with simple print statement using the built-in function named print() to print out some lines, most often we will print in our first line is “Hello World”. Really, most people who first started learning programming language will have this line printed. I use this function everywhere in my coding and it is very useful. It is just same as the PRINT statement from the SQL server, if you are coming from database background. Using single quote or double quote is not a matter.
print('Hello World!') print("Hello World!")
Variables and Types
Then, we touch on the variables and types, the important component in most programming languages. Variables and types are interrelated. I discussed about the characteristic of a variable in my first post. Let me have it here too!
- 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.
Then, there are plenty of different data types as well, yes, that is the types I meant here. Remember, different types have different behaviours. I wrote many posts about each of them before. I will link them up whenever we re-visit the topic.
- 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
- 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 empty string, 0 or False.
The simplest way to demonstrate how we can create a variable and assign a value to it.
height = 1.67 weight = 180 name = 'Joanne' gender = 'Female' isStudent = True
What is the difference between (=) and (==)?
The single equal sign (=) assigns the value on the right to a variable on the left whereas the double equal sign (==) tests if the two things have the same value. The two things can be a comparison of two variables or a variable with a math operator.
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 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 topics by its own as there are many interesting about them such as the use of (+) sign. It is concatenate sign which means it combines two or more variables of same type together. The way number and string use (+) sign also difference than each other. Also, we have to remember that in Python, string and integer cannot use of (+) sign together. It throws exception (error). 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.
Let us move into fundamental part two, Python List.
It is an interesting topic and important part in Python. Almost everyone of us will use Python List in our daily coding life 🙂 It is a collection of values and allows to have different types within the elements, one of the most simplest and easiest collections. When it comes to the word “collection”, Python has four type of collections.
You can read more about the basic of these collection here. Each of them has different characteristics, syntax, structure and usage. Along the way, we use different collections to explain the Python codes and concepts. Below is an example of how list looks like:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
Declaring a list is same as declare a variable, it just requires to follow 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]
We can use the lists above to work with control flows, going through the iteration and/or condition checking, then calculate a value and return a result. I think I will cover it in the next post.
Up to now, this portion is still a basic Python and does not involve any analytics or data science work if you are looking for one.