Getting started is the hardest part of learning Python. This is the first lesson in a series of tutorials designed to introduce Python as a programming language and how it can be used for business analysis.
After completing this tutorial, you will know:
Some use cases for Python:
Let’s get started.
This tutorial is divided into 5 parts:
[Video]
Python is a free open source scripting language. It is used for a wide range of tasks including website development and data analysis.
Python is an “object oriented” language which means that the basic unit of everything in Python is an object. We manipulate objects with methods or functions. An object could be a sentence, a list of numbers, an image, a spreadsheet, or something else.
Python is a multi-purpose programming language. It has been called the “second-best language for everything.” For example, R is better than Python for many statistics-focused tasks and JavaScript is better for web development than Python. However, if you only learn one language - Python is the best option.
Python is free and open source, but most people work with Python through an Integrated Development Environment (IDE). This project is built in a Jupyter Notebook. This is just an interface for writing Python code. Some people prefer other interfaces such as PyCharm, Visual Basic Studio, or Spyder.
Amazon SageMaker uses Jupyter Notebooks.
The hash sign is used for comments in Python. Anything following a hash sign will not be executed.
You can also create a block of comments using triple quotation marks. In Python, you can use one quote mark or the double quote mark - so long as you are consistent.
# https://github.com/rajathkmp/Python-Lectures
# jupyter notebooks are an interface for writing python code
# it is NOT a website
'''
here is comment
another comment
'''
print('Hello World')
A single equal sign is used to assign a value to an object.
Two equal signs in a row is used to test equality.
x = 2
y = 5
x+y
x==y
Integers and floating point numbers can be used interchangeably (in general)
val_int = 5
val_float = 5.0
print('The type of {0} is {1}'.format(val_int, type(val_int)))
print('The type of {0} is {1}'.format(val_float, type(val_float)))
A sting is a list of characters. It can be a random list of characters or words and sentences. There are special methods we can apply to strings such as manipulating the case or finding a letter.
a = 'Hello World'
a.upper()
# This will return the position of the letter 'W'. In Python (and most computer languages) counting begins with 0
a.find('W')
An object that stores any type of object in order. This is the storage structure to use most often. The list doesn’t have to be the same kind of object. You can mix intergers, floats, and strings. However, lists are often used for computation in which case you want all the objects in a list to be the same.
mylist = [3,6,1,6,3,7,3,7,3,99,3,'alpha']
mylist
# select 10th item (note 0-indexed)
mylist[9]
# slicing list
mylist[3:8]
list(range(10))
sum(list(range(10)))
# this will throw an error. Why doesn't it work?
sum(mylist)
Create a list of names of a few people on your team
Print the value of the first and third names in the list
Test if those two values are equal to each other (they are probably not)
### CODE FOR ABOVE EXERCISE ###
Dictionaries are key-value storage. Each item has a key (before the colon) and a value (after the colon). It can be used to store alias names, related objects, or even functions. Python uses dictionaries extensively but at the beginning these can be tough to use.
currency_map = {
'Canada': 'CAD',
'US': 'USD',
'United Kingdom': 'GBP',
}
currency_map
currency_map['Canada']
currency_map['China'] = 'CN'
currency_map['China']
restaurant_rating = {
"fast": 0,
"Italian": 3,
"Tex-Mex": 5
}
# By convention, we use k for the dictionary key and v for the dictionary value
for k, v in restaurant_rating.items():
print("I give {k} food a rating of {v}".format(k=k, v=v))
What is the currency code for the United Kingdom? Print it.
Add a key-value pair to the dictionary that maps Japan to its currency (JPY)
Print the currency code for Japan.
### CODE FOR ABOVE EXERCISE ###
Notice the indentations (tabs) after the “if” and “else” lines. Tabs are important in Python as their placement often affects how code is executed. In an if statement, the tabs determine when the if/else sections end.
x = 12
if x > 10:
# this code is executed only if the condition is True
print("hello")
else:
# otherwise, this code is executed
print("world")
mylist = [3,6,1,6,3,7,3,7,3,99,3,'alpha']
for e in mylist:
# this code executes multiple times, once for each element in the list
# on each iteration, the variable e changes to the next value
print(e)
List comprehensions are a convention in Python that allows us to avoid writing loops or conditions.
# The above condition could be written as a list comprehension this way
x = [12]
print("{word}".format(word = ["hello" if x > 10 else "world" for x in x]))
# The For Loop would be written this way
mylist = [3,6,1,6,3,7,3,7,3,99,3,'alpha']
print("{item}".format(item = mylist))
Tabs are important again here. Functions are defined based on the lines of code that are indented below the “def” line.
def firstfunc():
print("Hello World!")
print("This is my first function.")
firstfunc()
def times(x,y): # variables inside parenthesis are the "parameters"
z = x*y
return z
# note that x and y are defined local to function (in its "scope")
# Why does this line error?
a = times()
# run the function and capture the value it returns by assigning it to the variable c
c = times(4,5)
print(c)
This section lists some ideas for extending the tutorial that you may wish to explore.
This section provides more resources on the topic if you are looking to go deeper.
In this tutorial, you were introduced to the Python programming language. Specifically, you learned:
In the next section, you will Python to do basic data analysis with the Pandas Library.