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!
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.
user_name
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-0bd178d57585> in <module> ----> 1 user_name NameError: name 'user_name' is not defined
And we can "call" the value stored in the variable by simply typing the variable
USER_NAME = 'sk'
USER_NAME
'sk'
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)
string = 'This is a string, and yes, space also considered as a string.'
print(type(string))
integer = 123
print(type(integer))
float_ = 123.5
print(type(float_))
<class 'str'> <class 'int'> <class 'float'>
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
input()
123
'123'
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.
input('Please input User name: ')
Please input User name: SK
'SK'
We can also assign the user input to a variable using = sign, and call it out when we need them
user_name = input('Please input User name: ')
Please input User name: sk
print(user_name)
sk
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.
'sk' == 'sk'
True
'sk' == 'sk '
False
and yes, space are also counted.
Another gear we have to learn is the if else statment, the format is as follow:
if True:
print('true')
true
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.
if False:
print('this row will not going to print')
else:
print('this row will print')
this row will print
Combining all these gears, we can write a simple login system like the following:
USER_NAME = 'sk'
PASSWORD = 'Python101yo'
user = input('User name: ')
password = input('Password: ')
if user == USER_NAME and password == PASSWORD:
print('Login success, welcome back.')
else:
print('Login Fail...')
User name: sk Password: Python101yo Login success, welcome back.
USER_NAME = 'sk'
PASSWORD = 'Python101yo'
user = input('User name: ')
password = input('Password: ')
if user == USER_NAME and password == PASSWORD:
print('Login success, welcome back.')
else:
print('Login Fail...')
User name: skk Password: Python101yo Login Fail...
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.
def login_system():
USER_NAME = 'sk'
PASSWORD = 'Python101yo'
user = input('User name: ')
password = input('Password: ')
if user == USER_NAME and password == PASSWORD:
print('Login success, welcome back.')
else:
print('Login Fail...')
defining a funciton will not give any result, it will only run when we call it using function()
login_system()
User name: sk Password: Python101yo Login success, welcome back.
login_system()
User name: sk Password: Python102yoyo Login Fail...
I think that should be enough for a week, try to play around defining your own functions doing fun stuff. happy learning =]