Python Intro

Quick list of Python concepts covered in the first 1-2 days of class:

  • We are using Enthought Canopy software as our IDE (integrated development environment). It is free to download, but it’s also possible to use the repl.it website for now. (Fall 2018 update: we are starting in repl.it and moving to Canopy later)
  • Canopy has two main screen areas: the editor window is where you type lines of code that can be saved as a file. If you put the Run button (Ctrl+R), it will run your program. The Python (or Interactive Python) window is where results and error messages are displayed. Python commands can also be typed into this window to experiment, check variable values, or call functions.
  • A variable is a memory storage location for a piece of data. To assign means to store a value in a variable.
  • When assigning to a variable, the variable name goes on the left, and the value to be stored goes on the right. (“x = 2” is good, but “2 = x” will not work.)
  • You can make one variable equal to another variable. However, Python won’t go back and make sure they are always equal. In the following example, the final value of x is 7, even though the value of a was changed to 100 (Try this in Python!)
  • a = 7
    x = a
    a = 100
    
  • Variables can store a variety of data types. Examples: “int” is integer (whole number), “float” is a decimal number, and “str” (string) is text data.
  • Comments (#single line comments  and ”’block comments”’) are ignored by the compiler. Programmers use comments to make their code easier to read. Also, you can “comment out” lines of code to temporarily disable them.
  • Text data is entered in python inside single or double quotes.
  • Text inside quotes is called a “string literal.”
  • Variable names are not enclosed in quotes.
  • String concatenation (combining pieces of text with +)
  • Math operations (+, -, *, /, **)
  • Increment assign (x += 1 is the same as x = x + 1)
  • if statements; in python, a colon (:) is used instead of the word “then”
  • The commands after a colon are always tabbed in one level. A colon also can mean “do the following”
  • If statements use conditions, which can only be True or False.
  • Use one equals sign for assigning a value (x = 5) and two equals signs to check for equality (if x == 5:).
  • input() is a command that asks the user to type in text
  • () means a function is being called; that is, python is being told to run or execute that function’s code.
  • input(‘Please enter some text: ‘) includes a prompt, or instructions that will be displayed for the user.
  • We can do something like “response = input(‘enter some text: ‘)” to save the user’s input in a variable. Otherwise Python will not remember what the user typed in.
  • If the user types a number, it will be saved as string (text). That means that we can’t do math or numerical comparisons unless we convert it to a number using int() or float(). Example: response = int(response) would convert “5” (in quotes because it’s a string) to just plain 5 (no quotes, integer type).
  • If you try to concatenate or add a string to a number, Python will throw an error message. Convert one of the items to the same type as the other using str(), int(), or float() to fix this issue.

Here is a way to create a trivia question in Python:

response = input('What color is the sky?')
if response == "blue":
    print("That is correct.")
else:
    print("Sorry, that is not correct.")
print("Thanks for taking this quiz.")

Notice that the lines with “if” and “else” end with a colon (:). Also notice that the lines after the colons are tabbed.

Python evaluates the condition response == "blue" and if it’s True, it executes the tabbed code after the if line. If it’s False, it executes the tabbed code after the else line. There can be multiple tabbed lines after the colon, and in that case all of the tabbed lines execute.

Also notice that the last line executes regardless of whether the condition is True or False, because that line is not tabbed.