Tutorial guide: Learn Python basics

Hi ok, here are my own notes on the basics of Python. I am working on a Udemy course on data science and got excited about Python(it has been while since I’ve done something cool with Python, so I am excited 🙂 ). This is based on the Udemy course https://www.udemy.com/data-science-and-machine-learning-with-python-hands-on

Where it goes, hope it helps.

You can run python code as scripts or as a python notebook.


Running a script from a command prompt:

python “your script file location and name”


Basic basics


List definition:

listOfNumbers = [1, 2, 3, 4, 5, 6]


Iteration through a list of items:

for number in listOfNumbers:

print number,

if (number % 2 == 0):

print “is even”


print “is odd”


#Notice: In python you differentiate between blocks of code by whitespace, or a tab. Not the same as let say Java or C# where the char { and } are used to differentiate a block of code. Pay attention to you formatting, indentation.


#Notice: The char , (comma) is used to tell that something is going to continue on the same print line, within the same block of code. See example above.


#Notice: Colons : differentiate clauses.


Importing modules


import numpy as np


A = np.random.normal(25.0, 5.0, 10)

print A


Data structures




Defining a list(Notice: The brackets [] define an mutable list):

x = [1, 2, 3, 4, 5, 6]


Printing the length of a list:

print len(x)


Sub setting lists:


First 3 elements(counting starts from zero):



Last 3 elements:



Last two elements from starting from the end of the list:



Extend the list with a new list :



Add a new item to the list:



Python is a weekly typed language which allows you to put whatever you want in a list:


Creating a multidimensional list:

y = [10, 11, 12]

listOfLists = [x, y]



Sort a list(descending):

z = [3, 2, 1]



Sort a list(ascending):





Are just like lists but immutable.

You can not extend them append them, sort them etc. You can not change them.




#Tuples are just immutable lists. Use () instead of []

x = (1, 2, 3)



y = (4, 5, 6)


listOfTuples = [x, y]


Tuples common usage for data science or data processing:

Is to use it to assign variables to input data that as it is read in.


This example creates variable with values from a “source” where data is split by the comma.

#Notice: It is important that you have the same about of variables in your tuple as you are retrieving/assigning from the data “source”.

(age, income) = “32,120000”.split(‘,’)

print age

print income




A way to define a “lookup” table:


# Like a map or hash table in other languages

captains = {}

captains[“Enterprise”] = “Kirk”

captains[“Enterprise D”] = “Picard”

captains[“Deep Space Nine”] = “Sisko”

captains[“Voyager”] = “Janeway”


print captains[“Voyager”]


print captains.get(“Enterprise”)


for ship in captains:

print ship + “: ” + captains[ship]


If something is not found the result will be none:

print captains.get(“NX-01”)




Let you repeat a set of operation over and over again with different parameters.


Notice: use def to define a function and  () chars to define the parameters and use the return keyword to return value from the function.


def SquareIt(x):

return x * x


print SquareIt(2)


Pass functions around as parameters

#Notice: You have to make sure that what you are typing is correct because there is no strong typing in Python. Typing the wrong function name will cause errors.


#You can pass functions around as parameters

def DoSomething(f, x):

return f(x)


print DoSomething(SquareIt, 3)


Lambda functions

Is Functional programming: You can inline a function into a function


#Notice: lambda keyword is telling that you are defining a inline function to be used where you put it. In the example below inside a function parameter named x followed by a colon : character followed by what the function actually does. To pass in multiple parameters to a lambda function use the comma , to separate the variables.


#Lambda functions let you inline simple functions

print DoSomething(lambda x: x * x * x, 3)


Boolean Expressions


Valye is false

print 1 == 3


Value is true(or keyword check which is true)

print (True or False)


Check if something is a certain value( Use the is keyword)

print 1 is 3


If else clauses


if 1 is 3:

print “How did that happen?”

elif 1 > 3:

print “Yikes”


print “All is well with the world”




Normal looping

for x in range(10):

print x,


Continue and Break


#Process only the first 5 items but skip the number one

for x in range(10):

if (x is 1):


if (x > 5):


print x,




x = 0

while (x < 10):

print x,

x += 1



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s