Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benhosmer/3326906 to your computer and use it in GitHub Desktop.
Save benhosmer/3326906 to your computer and use it in GitHub Desktop.
An Introduction to Python

An Introduction to Python

Introduction

What is Python?

  • A programming language that is compiled when you run it. (Explain JIT vs. Compiled languages like C)

How can You use Python?

  • Sort files on your computer
  • Do complex math
  • Create websites
  • Make games

Who else uses Python?

  • NASA - Curiosity log parsing
  • bit.ly - Data analysis
  • Django Framework - rapid web application development (think of it as Rails for Python)
  • reddit.com - python is the backbone of one of the most visited sites in the world

Resources

The interpreter vs. running files

  • Use the interpreter ie. the python shell idle or others
  • Create a text file and run that

Commenting Code

  • This is important. It helps you and others figure out what you were trying to do.
  • Denote comments with #
  • # This is a comment, it won't be run

Arithmetic

  • Basic Operations

    +, -, /, *

  • More Complex Operations

    • Order

      (1 + 2) * 3 = 9

      1 + 2 * 3 = 7

Assignment vs. Equality

  • Assignment is by =
  • Comparison is by ==

Variables

  • Variables can be called anything, but it is best to be descriptive for you.
  • Must begin with an _ or letter
  • Case Sensitive
  • Some words are reserved: and, del, if, not, print etc.

x = 3

x = raw_input("Enter your name") Python 2

x = input("Enter your name") Python 3 use just input instead

Strings

  • Strings are for you not, for the computer.

  • 1 vs. '1'

    x = 1

    y = '1'

    print x

    1

    print y

    1

    3 + y

    Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str'

    3 + x

    4

  • String concatenation

    • Use it to join things

      ` string1 = 'Hi'

      ` string2 = 'there'

      print string1 + string2

Lists

  • Analogous to variables that hold multiple values. You might recognize them as arrays if you know another language.

  • Can be indexed by their position in the.

  • Indexes start with 0 not 1.

    list1 = ['a', 'b', 'c', 'd']

    print list1

    ['a', 'b', 'c', 'd']

  • Reference items in the list by their index.

    print list1[3]

    d

  • Use the built-in len function to count how many items are in a list.

    len(list1)

    4

  • Integers in lists

    list2 = [2, 4, 6, 8]

    print list2[0] + list2[1]

    6

  • Another type of list.

    list3 = ['apples', 'oranges', 'bananas']

    list3 = [0:2]

    'apples', 'oranges'

    [0:2] means a range not including the last index.

Iteration

  • This is where Python's indentation is very important.

  • IF something meets our criteria, then do this.

    x = 5

    y = 6

    if x + y == 11:

    print '5 + 6 DOES equal 11'

    5 + 6 does equal 11

Functions

  • Take the form

    def functionname(value1, value2, …)

    do something here

  • A function that adds two numbers together:

    def number_adder(x, y): print x + y

  • Utilize the function by supplying arguments:

    number_adder(5, 3)

    8

An exercise

  • Write a program that asks the user to enter their age, and name, prints it back to them.
  • Expand on this and add a friend's age. Now calculate the difference between your friend and your age and tell the user.
@kitplummer
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment