Python 101

Python is a powerful, easy to user, object oriented programming language.

History of Python

It’s performant meaning it runs on all operating systems and it’s extremely versatile.

Python’s versatility; scripts & programs, web development, data science

  • Command line calculators
  • UI to create desktop apps
  • Server side language
  • Full stack apps or REST APIs
  • Frameworks like Django and Flask
  • Gather, import and clean data
  • Machine Learning

Python has a clean and simple syntax. Has a “batteries included” approach and offers great documentation.

Python embraces objects, classes, inheritance and allows you to easily work with complex data structures.

Getting Started

Download package https://www.python.org/downloads/

Install package

Launch terminal then run: “python”.

Now you’re in the terminal REPL.

In terminal run: “2+2”

run: user_name = input(‘Please enter your name: ‘) 

To exit the REPL run “exit()”.

Read From A File

Create a directory in an easily accessible folder. Create a file called mypythonscript.py

cd ~/Python
print "My Python Script"

In terminal run “python mypythonscript.py”.

Your First Function

Add this to your mypythonscript.py, and run it.

def greet(name, age):
print('My name is ' + name + ', I am ' + age)

greet(name='Justin', age='26')

Your output should look like this:

List Comprehension

List comprehensions provide a concise way to create lists.

It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses.

The expressions can be anything, meaning you can put in all kinds of objects in lists.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

The list comprehension always returns a result list.

Turn the following code:

new_list = []
for i in old_list:
    if filter(i):
        new_list.append(expressions(i))

Into this with list comprehension:

new_list = [expression(i) for i in old_list if filter(i)]

Dictionary Comprehension

stats = [('age', 29), ('weight', 72), ('height', 178)]
dict_stats = {key: value for (key, value) in stats}