Python basic - How I learn basic python by single project

Hi! SK here, I am a self taught data scientist. In this series I'd like to share my experiences on learning python (and eventually get a position as computation / data scientist in a year!), hopefully this could help anyone out there who want to learn about programming / data science. =]

To begin with, I highly recommend the introduction course CS50 by Harvard University Link here, which provide a very solid fundation.

And to pratice coding in Python (or other languages), I start with a login system, which could eventually involve a lot of techniques and data structure concept. Surely you may also want to pratice through some platform like LeetCode or CodeWar, but I still think the most I could learn is to start and expend a complete project. So without furthere ado, Let's get started!

Week 1 - a basic login system

Consider we are going to make a platform (either game platform, shopping page or other things you are interested in), we want our customers to login with their user name and password. A simple function / script that could take user's input (on user name and password) and check would be good starting point.

In python, all variables (or pointer if you want to be a bit more precise) should not start with number (01USER_NAME is not valid), and a "=" sign is to assign a value to a variable. Variables with any assignment will raise a not define error.

And we can "call" the value stored in the variable by simply typing the variable

And yes, python variables are case sensitive, user_name is not the same as user_NAME or USER_name.

There are several types of basic data type in python, namely string, integer, float. String is anything within '' or "", integer is just numbers, while float is number with float point numbers. (note that float is actually a function in python, so we have to use float_ to avoid using the same name with the python default functions)

In our login system, we want to use string as user name and password (surely you can also use integer / float if you want), so how to get user input? Here let me introduce a default function - input

As you may notice here, first, we need to call a function by putting a () after function to call it, input() will ask user to input, and the output is a string (the output is within a ''!) We can also put some string within a input function to provide what information users are supposed to input by puting a string as the input of the function.

We can also assign the user input to a variable using = sign, and call it out when we need them

Another gear we are missing is to compare the user name and password with the "correct" one, in this situation the obvious choice is the logical operators in python. There are several logical operators, for details you can find it here, here we can use the "==" operators to compare strings. Python will return True if both side are identical and False when they are not.

and yes, space are also counted.

Another gear we have to learn is the if else statment, the format is as follow:

if the statment after 'if' is True, the code after the if statment will run (print('true')), and if the statment after 'if' is not True, we can choose do nothing (by not putting anything afterwards) or put a else to do something.

Combining all these gears, we can write a simple login system like the following:

As you may notice, it is not very user friendly to copy and paste whenever we want to try the code, in this case we could define a function and call it whenever we want to.

defining a funciton will not give any result, it will only run when we call it using function()

I think that should be enough for a week, try to play around defining your own functions doing fun stuff. happy learning =]