Intro to Python
Class 1

@gdiannarbor   |   #GDIA2   |   #IntroToPython

What we will cover today

  • Why Python?
  • What is programming?
  • Variables and arithmetic
  • Statements and Error Messages
  • Development Environment Setup

Scary things we'll be facing

  • Command line
  • Writing code
  • Learning new things
  • Code that won't run
  • Code not working
  • Asking questions in a room full of people
  • Asking "experts" beginner questions
  • Not getting it

Why Python?

  • Suitable for beginners, yet used by professionals
  • Readable, maintainable code
  • Rapid rate of development
  • Few "magical" side-effects
  • Variety of applications

What is Python used for?

  • System Administration (Fabric, Salt, Ansible)
  • 3D animation and image editing (Maya, Blender, Gimp)
  • Scientific computing (numpy, scipy)
  • Web development (Django, Flask)
  • Game Development (Civilization 4, EVE Online)

Who is using Python?

  • Disney
  • Dropbox
  • Canonical and Red Hat
  • Google
  • NASA

What is programming?

  • Teaching the computer to do a task
  • A program is made of one or more files of code, each of which solve part of the overall task.
  • The code that you program is human readable but also needs to exist in a form that the computer can run directly. This form is not human readable.
  • Don't focus on what's "under the hood" for now. We will "drive the car" first.
  • In other words, there are many layers to the onion. We start at one layer and slowly move toward layers that are beneath or above us.

Command line, Python Shell, Text Editors

Terminal A program that has a command line interface and issues commands to the operating system.
Python Shell A command line program that runs inside of the terminal, takes Python code as input, interprets it, and prints out any results.
Text Editor A program that opens text files and allows the user to edit and save them. (Different than a word processor).

Example Text Editors

Linux Gedit, Jedit, Kate
MacOSX TextMate, TextWrangler
Windows Notepad++
All Sublime Text, Vim, Emacs

Let's Develop It!
Working in the Python Shell

Open up your terminal and type python3

Follow along with the examples in the upcoming slides.
Just type them right in!

Feel free to explore, as well.
You will not accidentally break things!

When you're done, type exit() to close the Python shell

Variables and Arithmetic


3 + 4
2 * 4
6 - 2
4 / 2
            

a = 2
b = 3
print(a + b)
c = a + b
print(c * 2)
            

a = 0
a = a + .5
print(a)
            

Strings


a = 'Hello '
b = 'World'
c = a + b
print(c)
            

a = "Spam "
b = a * 4
print(b)
            

a = 'spam '
b = 'eggs'
c = a * 4 + b
print(c)
            

Data Types

  • Variables are used to store data
  • Among other things, variables are used to represent something that can't be known until the program is run
  • Data always has a "type"
  • The type of a piece of data helps define what it can do
  • The type can be found using: type()
  • type() is a function. We call it by using parenthesis and pass it an object by placing the object inside the parenthesis
    
    a = 4
    print(type(a))
    print(type(4))
    
    print(type(3.14))
    
    b = 'spam, again'
    print(type(b))
    print(type("But I don't like spam"))
                

Data types - continued ...

  • Data values can be used with a set of operators
  • An "int" or "float" can be used with any of: +, -, *, /
  • A "string" can be used with any of: +, *
  • What happens if we try to use division or subtraction with a string?

print("Spam" - "am")
a = 'Spam and eggs'
print(a / 'hashbrowns')
print(a / 6)
            

Errors

  • There are different kinds of errors that can occur.
    We've seen a few already.
  • A "runtime error" results in an Exception, which has several types.
    Each type gives us some information about the nature of the error and how to correct it
  • One type of exception is a SyntaxError. This results when our code can not be evaluated because it is incorrect at a syntactic level.
    In other words, we are not following the "rules" of the language.
  • Some other examples are the TypeError and NameError exceptions.

Errors - continued ...


# SyntaxError - Doesn't conform to the rules of Python.
# This statement isn't meaningful to the computer
4spam)eggs(garbage) + 10

# NameError - Using a name that hasn't been defined yet
a = 5
print(d)
d = 10

# TypeError - Using an object in a way that its type does not support
'string1' - 'string2'
          

There are also semantic errors.
These are harder to catch because the computer can't catch them for us.

Let's Develop It

We'll practice what we've learned in the shell.

Review the slides on your computer and practice entering any commands you didn't fully understand before.

Ask the teacher, TAs, and students around you for help!

Using the Terminal (OSX) or PowerShell (Windows)

Try each of the following commands in turn:

Command Short for Description
pwd Print working directory Displays what folder you are in.
ls List Lists the files and folders in the current folder
cd Change directory Change to another folder. Takes the folder name as an argument. 'cd ..' goes up a directory
cat Concatenate Prints the contents of a file. Takes a filename as an argument

Creating a folder

We need a folder to save our work in.

The ->'s below indicate the expected output of the previous command.


pwd
-> /home/username
mkdir Projects
cd Projects
mkdir gdi-intro-python
cd gdi-intro-python
pwd
-> /home/username/Projects/gdi-intro-python
            

Now that the folders are made, we only have to use
cd Projects/gdi-intro-python in the future.

The Text Editor

Open sublime text.

  • Click "File", then "Open". Navigate to the gdi-intro-python folder we just created and click "Open"
  • In the text editor, enter the following:
    
    print('I am a Python program!')
                
  • Click "File", then "Save As...". Type "class1.py" and click "Save".
  • Open a terminal and navigate to the gdi-intro-python folder.
    If you don't already have this folder open in a terminal.
  • Type python3 class1.py
  • You should see the terminal print "I am a Python program!"

User Input

To obtain user input, use input()

Change the class1.py text to the following and run it again


input_value = input("Enter a radius:")
radius = float(input_value)
area = 3.14159 * radius * radius
print("The area of a circle with radius " + input_value + " is:")
print(area)
            

The user's input is a string, so we use float() to make it a number

Let's Develop It!

Write your own program that uses input
and does something else with the value

You can use float() to treat the input as a number if you need a number,
or use the input directly as a string.

Questions?

Homework

Write a program that could replace the initial conversation you have at the checkout line. It should do the following:


>> Did you find everything ok?
(wait for input)

>> What's your name?
(read in a name from the user)

>> Hi, (User's Name) do you want paper or plastic?
(wait for input)

>> What is your total before tax?
(read in a total)

>> Your total with sales tax is: (total with sales tax)
(wait for input before exiting)
          

Want help?

Intro to Python
Class 2 »

@gdiannarbor   |   #GDIA2   |   #IntroToPython