Skip to content

Instantly share code, notes, and snippets.

@dheaney
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save dheaney/c171561442d6069e2474 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/c171561442d6069e2474 to your computer and use it in GitHub Desktop.

Lesson 1: Basic Operations, Types, and Conditional Statements

Basic Operations

  1. Assignment

    Assignment is how we store stuff. It's kind of like x in algebra:

    x = 5
    greeting = "Greetings and salutations!"
    x = greeting
  2. Math

    Oh no! This is another math class, isn't it? NO! But we still use it!

    value1 = 5
    value2 = 10
    value3 = value1 + value2
    
    message1 = "'Sup?"
    message2 = message1 * value3

    The * (asterisk) acts as our multiplicaiton symbol. We can't use an x because the computer reserves x for us to define ourselves.

    • Multiplicaiton: *
    • Division: /
    • Addition: +
    • Subtraction: -
    • Modulo: %

Types

  1. Integers

    Ever had trouble classifying numbers? HA. Programming will fix you.

    x = 0        # Yes, zero is an integer
    x = 1        # Yes, one is an integer
    x = int(3.5) # x stores a 3 here
    x = int('4') # x stores a 4 here
  2. Floating-point numbers

    You remember these from Saxon, don't you? Well, you shouldn't! Programmers call rational numbers floating-poing numbers or floats for short. Basically everybody else uses different terminology. Just remember that no matter what, floats have a decimal point

    x = 1.0
    x = float('4.5')
    x = 12345.000121212

    Python has a quirky way of dealing with division that should be mentioned here.

    x = 1 / 3        # x is 0
    x = 2 / 3        # x is still 0
    x = 99 / 100     # x is STILL 0
    x = 100 / 100    # x is one
    x = float(1/3)   # x is 0.0
    x = float(1) / 3 # x is 0.3333333333333333

    That's called truncation, by the way.

  3. Strings

    You definitely shouldn't remember strings from Saxon. Strings are the little guys surrounded by quotation marks.

    x = 'Hi'                       # x is a string
    x = "Bye"                      # x is a string
    x = """What's up with this?""" # x is a string
    x = '5'                        # x is a string
    x = str(5)                     # x is a string
    x = str('5')                   # x is a string
    x = Hi                         # NameError

Conditional Statements

Conditional statements in Python are handled by the if keyword.

x = 5 # Assigns 5 to x

if x is 5: # "if x is equal to 5"
    print "x is 5"

if x == 5: # "if x is equal to 5"
    print "x is 5"

# Notice the `==` symbol. Remember that `==` is not the
# same as `=`. Here are some other conditional statements:

x = 6.0

if x > 5: # "If x is greather than 5"
    print "x is not equal to 5"
elif x < 5: # "Else if x is less than 5"
    print "x is less than 5"
else:
   print "x equals 5"

if x >= 6: # "If x is greater than OR equal to 6"
    print "x is greater than or equal to 6"

if x > 6 or x == 6:
    print "x is greater than or equal to 5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment