python-introduction



python-introduction

0 0


python-introduction

Slides for a Python Introduction Workshop for PyLadies Singapore in July 2015

On Github mbrochh / python-introduction

Welcome to Python

An introduction to setup your Computer for Python programming

Press SPACE or SHIFT+SPACE to move through the slides...

Who am I?

Thank you, PayPal Singapore ♡ ♡ ♡

Big thanks to Laurence Putra of PayPal Singapore for kindly offering to host this event and for sponsoring pizza and softdrinks.

We have a Slack.com community!

PyLadiesSG has a community on Slack. Myself and others are idling there permanently and we are happy to mentor anyone who needs help.

Visit pyladies-sg-slackin.herokuapp.com if you are based in Singapore and if you would like an invite.

What to expect from this tutorial

give a woman a fish and you feed her for a day; teach a woman to fish and you feed her for a lifetime

We will not write much Python code today. Instead, we will make sure that everyone has important tools installed, which will enable you to write a lot of Python code in the future!

The tools for today are: Python, virtualenv, Atom and ipdb.

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Introduction

  • No one can learn programming in a day
    • or in a week
    • or in a month
    • or in a year
  • If you want to become really good
    • you have to try things yourself
    • you should find a mentor or a user group to guide you
  • This tutorial is just a tiny step into the right direction
  • I hope that
    • it empowers you to take more steps on your own
    • you will come back for more :)

Follow along

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

A Command-Line Crash Crouse

  • The command line is a window where you can execute text-commands
  • On Windows it is called "PowerShell"
  • On OSX and Ubuntu it is called the "Terminal"
  • For simplicity, we will always call it the "Terminal"

Windows: The PowerShell

  • Open the Start-menu and search for "PowerShell"
  • You might want to drag it onto your Desktop
  • You will open this A LOT

OSX & Ubuntu: The Terminal

  • Open your computer search
  • That's Spotlight on OSX (press COMMAND+SPACE)
  • That's the Ubuntu icon at the top of the sidebar
  • Search for "Terminal"

Your Home Folder

  • Type pwd into the terminal:
    pwd
    
  • It will show which folder you are currently in
  • For a newly opened terminal, that is your home folder
  • Your home folder is also called "~" (tilde)
  • No matter where you are, you can type cd ~ to get into your home folder
    cd ~
    
  • By the way: pwd stands for print working directory

List The Current Folder

  • Hint: "folder" and "directory" is the same
  • I say folder, because it's shorter to write :)
  • Type ls to list the content of the current folder
    ls
    
  • By the way: ls stands for list

Move into another folder

  • Press TAB to autocomplete things
  • Type cd ~/Documents to move into the Documents folder in your home directory
    cd ~/Documents
    
  • By the way: cd stands for change directory

Create a new folder

  • Type cd ~ to move into your home folder
    cd ~
    
  • Type mkdir Projects to create a new folder called "Projects"
    mkdir Projects
    
  • How about creating a folder for our first project as well?
  • Type cd ~/Projects and then mkdir test
    cd ~/Projects
    mkdir test
    
  • By the way: mkdir stands for make directory

Create a new file

  • Type cd ~/Projects/test to move into your test project folder
    cd ~/Projects/test
    
  • On OSX/Ubuntu, type touch test.py to create a new file called "test.py"
    touch test.py
    
  • On Windows, type echo $null >> test.py
    echo $null >> test.py
    
  • So sorry, Windows doesn't have a nicer command for creating files :(
  • Type ls to see if your new file is there
    ls
    

High-Five!

  • This is just the tip of the iceberg
  • But it will get you through this tutorial :)
  • Whenever you see black boxes with commands in these slides, it means you should type something into your terminal:
    black boxes like this
    

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Install Python on OSX

It is already installed. If you ever want a "fresh" install, you can do this (but really don't need to for this tutorial): Install Homebrew: http://brew.sh Install Python
# Hint: You don't need to do this!
# This is just an tip for the future.
brew install python

Install Python on Windows

Download & install Python 2.7: https://www.python.org/downloads/ Leave the default install location at "C:\Python27" Make sure to select "Add Python.exe to Path" during the installation

Install Python on Ubuntu

It is already installed, but you need some extras Update software
sudo apt-get update && sudo apt-get upgrade
Install Python dependencies
sudo apt-get install python-dev
sudo apt-get install python-pip

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Starting Python on OSX & Ubuntu

To start the Python interpreter, type this into the terminal:
python
Exit with:
exit()

Starting Python on Windows

Open your PowerShell and do the following: First install useful modules
pip install pyreadline
pip install ipdb
Now start Python:
python
Exit with:
exit()

Important Hint

You are in the Python interpreter when you see>>> at the beginning of the line This means you are no longer in the normal terminal Terminal commands like ls and cd will not work here Only commands of the Python programming language will work here You will almost never need to start the Python interpreter You only start it if you want to test some Python commands

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Install Virtualenv on OSX & Ubuntu (I)

Virtualenv helps to keep each environment for each project clearly separated

Install pip
# On OSX,
# On Ubuntu you did this in an
# earlier slide already
sudo easy_install pip
Install virtualenv
sudo pip install virtualenv
Install virtualenvwrapper
sudo pip install virtualenvwrapper
to be continued...

Install Virtualenv on OSX & Ubuntu (II)

On OSX, execute the following commands in your Terminal:
echo "export WORKON_HOME=~/Envs" >> ~/.bash_profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
On Ubuntu, execute the following commands in your Terminal:
echo "export WORKON_HOME=~/Envs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

Install Virtualenv on Windows

Open the PowerShell and execute the following command
pip install virtualenvwrapper-win

What is this pip thing?

  • pip is a program that is written in Python
  • It allows you to download and install other programs which are also written in Python
  • You can search for available Python programs atwarehouse.python.org

Using Virtualenv

Close your Terminal and open a new one Create new virtualenv
mkvirtualenv test
Deactivate current virtualenv
deactivate
Activate existing virtualenv
workon test
Where is it?
cd ~/Envs/test

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Install & configure Atom.io editor

Download & install from https://atom.io Open Settings > Install Install "autocomplete-python" Install "atom-python-debugger" Install "symbols-tree-view" Install "linter" Install "linter-flake8" On OSX: Click at Atom > Install Shell Commands

Install flake8 globally

This is to make sure that "linter-flake8" works

Open a Terminal and execute:

# On Windows
pip install flake8

# On OSX & Ubuntu
sudo pip install flake8

What is this flake8 and linter thing?

  • A linter is a program that checks if the code of another program looks pretty
  • flake8 is a Python program that is able to lint your Python code for you.
  • There are many more linters, basically every programming language has it's own.
  • If you want to learn how Python code should be written grab some popcorn and read the famous PEP008 Not now! It's a long read ;)

Starting Atom

Open a new Terminal "cd" into the directory of your project, for example
cd ~/Projects/test
Execute
atom .
The dot means "this directory"

Important shortcuts

  • COMMAND+\: Show project files
  • COMMAND+P: Open any file
  • COMMAND+F: Search & replace in current file
  • COMMAND+SHIFT+F: Search & replace in whole project
  • CONTROL+ALT+O: Show symbols tree view
  • COMMAND+S: Save file
  • COMMAND+SPACE: Call auto-complete
  • COMMAND+,: Open settings
  • COMMAND+W: Close tab
  • COMMAND+TAB: Open next tab
  • COMMAND+SHIFT+TAB: Open previous tab

Hint: On Windows & Ubuntu, COMMAND is CONTROL

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Your first Python program!

Don't give up!

LPTHW is an awesome book You can actually finish all chapters in a few days It will give you a good idea of how to use Python If you have problems, join our Slack channel and just ask, we will make sure to get you through the book!

This is the plan

  • Introduction
  • A Command-Line Crash Course
  • Install Python 2.7
  • Starting Python from the Terminal
  • Install Virtualenv & Virtualenvwrapper
  • Install & configure Atom.io editor
  • Create your first Python program
  • Debugging

Debugging your program

  • This is an advanced topic
  • We will not have enough time to train this during the workshop
  • I'm putting this into the slides because it is the last crucial tool that you need for real software development
  • Instal ipdb

    # On OSX/Ubuntu:
    sudo pip install ipdb
    
    # On Windows:
    pip install ipdb
    

Place a breakpoint into your code

  • When debugging programs, you place so called breakpoints
  • For Python, you need to paste this into your code:
    import ipdb; ipdb.set_trace()
    
  • Luckily, Atom has a plugin for this, so you can just press F7 at the position where you want the breakpoint.
  • When you are done, press SHIFT+F7 to remove all breakpoints

Now run your program

  • After placing the breakpoint, run your program as usual
    python your_filename.py
    
  • Instead of executing normally, the program will stop at your breakpoint
  • Now you have all the time in the world to inspect your variables
  • You can also move to the next line in the program one step at a time

How to use ipdb

  • When you are inside of the ipdb debugger:
  • Press ? to get all possible commands
  • Press ? bt to get help for the bt command
  • l: Shows the current line
  • bt: Shows the full stack trace
  • n: Jumps to the next line
  • a: Shows all arguments of the current function
  • c: Continue execution until the program ends
  • Type in a variable name to see it's value
  • You can even change variables and continue the program

Recap

  • Python is already installed on OSX & Ubuntu
  • It's easy to install on Windows
    • Make sure to extend PATH environment variable
  • Atom editor can be installed easily
  • When you start a project:
    • Just create a folder
    • Then create a virtual environment
    • Create a "*.py" file inside that folder
    • Run "atom ." inside that folder
    • Execute your program and debug it using the Terminal

What next?

  • Learn the Python language
  • Learn to use all features of Atom
    • Memorize important keyboard shortcuts
    • Lean to use the vim-mode plugin
  • Learn to use your Terminal
  • Learn how to use git and GitHub
  • Find a small pet project for yourself and just do it!

Stay in touch!

Thank you and please come again :)

Welcome to Python An introduction to setup your Computer for Python programming Press SPACE or SHIFT+SPACE to move through the slides...